Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/cloudoperators/greenhouse v0.8.0
github.com/onsi/gomega v1.38.3
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.21.0
k8s.io/apimachinery v0.35.0
k8s.io/client-go v0.35.0
Expand Down Expand Up @@ -65,7 +66,6 @@ require (
github.com/sagikazarmark/locafero v0.12.0 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
Expand Down
57 changes: 56 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/cloudoperators/cloudctl/cmd"
Expand All @@ -18,15 +20,68 @@ import (
)

func main() {
var (
config string
)

// It is assumed there that $HOME is always set to correct value
home := os.Getenv("HOME")

Comment on lines +28 to +29
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already noted in TODO section of PR description.

// Optionally read environment variables, config files, etc.
viper.SetEnvPrefix("CLOUDCTL")
viper.AutomaticEnv()

pflag.StringVar(&config, "config", "", "Path to configuration file")
pflag.Parse()

viper.SetConfigType("yaml")

if len(config) > 0 {
// First we are trying config provided as command line parameter.
viper.SetConfigFile(config)
} else {
// Then we are searching for ".cloudctl.yaml" in current or home directory
viper.AddConfigPath(".")
viper.AddConfigPath(home)
viper.SetConfigName(".cloudctl")
}

viper.BindPFlags(pflag.CommandLine)
err := viper.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// If reading config in above described locations failed, we are looking for configuration
// in these locations:
// ./cloudctl.yaml
// $HOME/cloudclt.yaml
// $XDG_CONFIG_HOME/cloudctl/cloudctl.yaml
// $XDG_CONFIG_HOME/cloudctl.yaml
// $HOME/.config/cloudctl/cloudctl.yaml
// $HOME/.config/cloudctl.yaml
viper.SetConfigName("cloudctl")
if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); len(xdgConfig) > 0 {
viper.AddConfigPath(filepath.Join(xdgConfig, "cloudctl"))
viper.AddConfigPath(xdgConfig)
} else {
viper.AddConfigPath(filepath.Join(home, ".config", "cloudctl"))
viper.AddConfigPath(filepath.Join(home, ".config"))
}
err = viper.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
err = nil
}
}
Comment on lines +39 to +72
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already noted in TODO section of this Work In Progress PR description.


// Show error message and exit if config file was found but failed to read.
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

// Graceful cancellation on SIGINT/SIGTERM
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

if err := cmd.Execute(ctx); err != nil {
if err = cmd.Execute(ctx); err != nil {
// Print errors to stderr
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down