Skip to content

Commit 04cccb2

Browse files
committed
feat: Support for config file reading
Add support for configuration files as described in #49
1 parent 9120804 commit 04cccb2

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/cloudoperators/greenhouse v0.8.0
77
github.com/onsi/gomega v1.38.3
88
github.com/spf13/cobra v1.10.2
9+
github.com/spf13/pflag v1.0.10
910
github.com/spf13/viper v1.21.0
1011
k8s.io/apimachinery v0.35.0
1112
k8s.io/client-go v0.35.0
@@ -65,7 +66,6 @@ require (
6566
github.com/sagikazarmark/locafero v0.12.0 // indirect
6667
github.com/spf13/afero v1.15.0 // indirect
6768
github.com/spf13/cast v1.10.0 // indirect
68-
github.com/spf13/pflag v1.0.10 // indirect
6969
github.com/subosito/gotenv v1.6.0 // indirect
7070
github.com/x448/float16 v0.8.4 // indirect
7171
go.yaml.in/yaml/v2 v2.4.3 // indirect

main.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"fmt"
99
"os"
1010
"os/signal"
11+
"path"
1112
"syscall"
1213

14+
"github.com/spf13/pflag"
1315
"github.com/spf13/viper"
1416

1517
"github.com/cloudoperators/cloudctl/cmd"
@@ -18,15 +20,69 @@ import (
1820
)
1921

2022
func main() {
23+
var (
24+
config string
25+
)
26+
27+
// It is assmued there that $HOME is always set to correct value
28+
home := os.Getenv("HOME")
29+
2130
// Optionally read environment variables, config files, etc.
2231
viper.SetEnvPrefix("CLOUDCTL")
2332
viper.AutomaticEnv()
2433

34+
viper.BindPFlags(pflag.CommandLine)
35+
36+
pflag.StringVarP(&config, "config", "c", "", "Path to configuration file")
37+
pflag.Parse()
38+
39+
viper.SetConfigType("yaml")
40+
41+
if len(config) > 0 {
42+
// First we are trying config provided as command line parameter.
43+
viper.SetConfigFile(config)
44+
} else {
45+
// Then we are searching for ".cloudctl.yaml" in current or home directory
46+
viper.AddConfigPath(".")
47+
viper.AddConfigPath(home)
48+
viper.SetConfigName(".cloudctl")
49+
}
50+
51+
err := viper.ReadInConfig()
52+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
53+
// If reading config in above described locations failed, we are looking for configuration
54+
// in these locations:
55+
// ./cloudctl.yaml
56+
// $HOME/cloudclt.yaml
57+
// $XDG_CONFIG_HOME/cloudctl/cloudctl.yaml
58+
// $XDG_CONFIG_HOME/cloudctl.yaml
59+
// $HOME/.config/cloudctl/cloudctl.yaml
60+
// $HOME/.config/cloudctl.yaml
61+
viper.SetConfigName("cloudctl")
62+
if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); len(xdgConfig) > 0 {
63+
viper.AddConfigPath(path.Join(xdgConfig, "cloudctl"))
64+
viper.AddConfigPath(xdgConfig)
65+
} else {
66+
viper.AddConfigPath(path.Join(home, ".config", "cloudctl"))
67+
viper.AddConfigPath(path.Join(home, ".config"))
68+
}
69+
err = viper.ReadInConfig()
70+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
71+
err = nil
72+
}
73+
}
74+
75+
// Show error message and exit if config file was found but failed to read.
76+
if err != nil {
77+
fmt.Fprintln(os.Stderr, err)
78+
os.Exit(1)
79+
}
80+
2581
// Graceful cancellation on SIGINT/SIGTERM
2682
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
2783
defer stop()
2884

29-
if err := cmd.Execute(ctx); err != nil {
85+
if err = cmd.Execute(ctx); err != nil {
3086
// Print errors to stderr
3187
fmt.Fprintln(os.Stderr, err)
3288
os.Exit(1)

0 commit comments

Comments
 (0)