Skip to content

Commit

Permalink
fix: adc configure initializes configuration file (#50)
Browse files Browse the repository at this point in the history
Signed-off-by: Navendu Pottekkat <navendu@apache.org>
  • Loading branch information
pottekkat committed Sep 20, 2023
1 parent 81a1d01 commit 3940ba8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func saveConfiguration() error {
// use viper to save the configuration
viper.Set("server", rootConfig.Server)
viper.Set("token", rootConfig.Token)
if err := viper.SafeWriteConfig(); err != nil {
if err := viper.WriteConfig(); err != nil {
color.Red("Failed to configure ADC")
return err
}
Expand Down
17 changes: 12 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,22 @@ func Execute() {
}

func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
// set default config file $HOME/.adc.yaml
viper.SetConfigFile("$HOME/.adc.yaml")
if cfgFile == "" {
cfgFile = "$HOME/.adc.yaml"
}
viper.SetConfigFile(cfgFile)
viper.SetConfigName(".adc")
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/")

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)
}
}

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

0 comments on commit 3940ba8

Please sign in to comment.