Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: initial run of adc configure does not throw error #85

Merged
merged 5 commits into from
Oct 30, 2023
Merged
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
5 changes: 4 additions & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ func saveConfiguration(cmd *cobra.Command) error {
viper.Set("insecure", rootConfig.Insecure)

if overwrite {
err = viper.WriteConfig()
// because WriteConfig fails to write if the file does not exist
// and WriteConfigAs does write even if the file does not exist
// see: https://github.com/spf13/viper/issues/433
err = viper.WriteConfigAs(cfgFile)
} else {
err = viper.SafeWriteConfig()
}
Expand Down
18 changes: 11 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,21 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
}

if _, err := os.Stat(os.ExpandEnv(cfgFile)); err != nil {
color.Yellow("Config file not found at %s. Creating...", cfgFile)
_, err := os.Create(os.ExpandEnv(cfgFile))
if err != nil {
color.Red("Failed to initialize configuration file: %s", err.Error())
_, err := os.Stat(os.ExpandEnv(cfgFile))

if err != nil {
if os.IsNotExist(err) {
color.Yellow("Configuration file %s doesn't exist.", cfgFile)
return
} else {
color.Red("Error reading configuration file: %s", err.Error())
return
}
}

err := viper.ReadInConfig()
err = viper.ReadInConfig()
if err != nil {
color.Red("Failed to read configuration file, please run `adc configure` first to configure ADC.")
color.Red("Failed to read configuration file: %s", err.Error())
return
}

Expand Down