Skip to content

Commit ef0fa19

Browse files
authored
Change config handling: only create config file & directory if needed (#214)
* Change config handling: only create config file & directory if needed * Changes from code review * Fix config read * correctly handle f.Close error
1 parent 3de5783 commit ef0fa19

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

internal/cmd/config/list/list.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func NewCmd(p *print.Printer) *cobra.Command {
3737
"$ stackit config list"),
3838
),
3939
RunE: func(cmd *cobra.Command, args []string) error {
40-
err := viper.ReadInConfig()
41-
if err != nil {
42-
return fmt.Errorf("read config file: %w", err)
43-
}
44-
4540
configData := viper.AllSettings()
4641

4742
// Sort the config options by key
@@ -84,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8479
table.AddRow(key, valueString)
8580
table.AddSeparator()
8681
}
87-
err = table.Display(p)
82+
err := table.Display(p)
8883
if err != nil {
8984
return fmt.Errorf("render table: %w", err)
9085
}

internal/cmd/config/set/set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ func NewCmd(p *print.Printer) *cobra.Command {
8080
viper.Set(config.ProjectNameKey, "")
8181
}
8282

83-
err = viper.WriteConfig()
83+
err = config.Write()
8484
if err != nil {
85-
return fmt.Errorf("write new config to file: %w", err)
85+
return fmt.Errorf("write config to file: %w", err)
8686
}
8787
return nil
8888
},

internal/cmd/config/unset/unset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ func NewCmd() *cobra.Command {
144144
viper.Set(config.SKECustomEndpointKey, "")
145145
}
146146

147-
err := viper.WriteConfig()
147+
err := config.Write()
148148
if err != nil {
149-
return fmt.Errorf("write updated config to file: %w", err)
149+
return fmt.Errorf("write config to file: %w", err)
150150
}
151151
return nil
152152
},

internal/pkg/auth/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func GetAuthField(key authFieldKey) (string, error) {
136136
var errFallback error
137137
value, errFallback = getAuthFieldFromEncodedTextFile(key)
138138
if errFallback != nil {
139-
return "", fmt.Errorf("write to keyring failed (%w), tried write to encoded text file: %w", err, errFallback)
139+
return "", fmt.Errorf("read from keyring: %w, read from encoded file as fallback: %w", err, errFallback)
140140
}
141141
}
142142
return value, nil

internal/pkg/config/config.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,42 @@ var ConfigKeys = []string{
7070
SKECustomEndpointKey,
7171
}
7272

73+
var folderPath string
74+
7375
func InitConfig() {
7476
home, err := os.UserHomeDir()
7577
cobra.CheckErr(err)
7678
configFolderPath := filepath.Join(home, configFolder)
7779
configFilePath := filepath.Join(configFolderPath, fmt.Sprintf("%s.%s", configFileName, configFileExtension))
7880

79-
viper.SetConfigName(configFileName)
80-
viper.SetConfigType(configFileExtension)
81-
viper.AddConfigPath(configFolderPath)
81+
// Write config dir path to global variable
82+
folderPath = configFolderPath
8283

83-
err = createFolderIfNotExists(configFolderPath)
84-
cobra.CheckErr(err)
85-
err = createFileIfNotExists(configFilePath)
86-
cobra.CheckErr(err)
84+
// This hack is required to allow creating the config file with `viper.WriteConfig`
85+
// see https://github.com/spf13/viper/issues/851#issuecomment-789393451
86+
viper.SetConfigFile(configFilePath)
87+
88+
f, err := os.Open(configFilePath)
89+
if !os.IsNotExist(err) {
90+
if err := viper.ReadConfig(f); err != nil {
91+
cobra.CheckErr(err)
92+
}
93+
}
94+
defer func() {
95+
if f != nil {
96+
if err := f.Close(); err != nil {
97+
cobra.CheckErr(err)
98+
}
99+
}
100+
}()
87101

88-
err = viper.ReadInConfig()
89-
cobra.CheckErr(err)
90102
setConfigDefaults()
91103

92104
viper.AutomaticEnv()
93105
viper.SetEnvPrefix("stackit")
94106
}
95107

96-
func createFolderIfNotExists(folderPath string) error {
108+
func createFolderIfNotExists() error {
97109
_, err := os.Stat(folderPath)
98110
if os.IsNotExist(err) {
99111
err := os.MkdirAll(folderPath, os.ModePerm)
@@ -106,17 +118,12 @@ func createFolderIfNotExists(folderPath string) error {
106118
return nil
107119
}
108120

109-
func createFileIfNotExists(filePath string) error {
110-
_, err := os.Stat(filePath)
111-
if os.IsNotExist(err) {
112-
err := viper.SafeWriteConfigAs(filePath)
113-
if err != nil {
114-
return err
115-
}
116-
} else if err != nil {
117-
return err
121+
// Write saves the config file (wrapping `viper.WriteConfig`) and ensures that its directory exists
122+
func Write() error {
123+
if err := createFolderIfNotExists(); err != nil {
124+
return fmt.Errorf("create config directory: %w", err)
118125
}
119-
return nil
126+
return viper.WriteConfig()
120127
}
121128

122129
// All config keys should be set to a default value so that they can be set as an environment variable

internal/pkg/projectname/project_name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func GetProjectName(ctx context.Context, cmd *cobra.Command, p *print.Printer) (
4343
// (So next time we can just pull it from there)
4444
if !isProjectIdSetInFlags(cmd) {
4545
viper.Set(config.ProjectNameKey, projectName)
46-
err = viper.WriteConfig()
46+
err = config.Write()
4747
if err != nil {
4848
return "", fmt.Errorf("write new config to file: %w", err)
4949
}

0 commit comments

Comments
 (0)