Skip to content

Commit 3940ba8

Browse files
authored
fix: adc configure initializes configuration file (#50)
Signed-off-by: Navendu Pottekkat <[email protected]>
1 parent 81a1d01 commit 3940ba8

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

cmd/configure.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func saveConfiguration() error {
5151
// use viper to save the configuration
5252
viper.Set("server", rootConfig.Server)
5353
viper.Set("token", rootConfig.Token)
54-
if err := viper.SafeWriteConfig(); err != nil {
54+
if err := viper.WriteConfig(); err != nil {
5555
color.Red("Failed to configure ADC")
5656
return err
5757
}

cmd/root.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,22 @@ func Execute() {
5555
}
5656

5757
func initConfig() {
58-
if cfgFile != "" {
59-
viper.SetConfigFile(cfgFile)
60-
} else {
61-
// set default config file $HOME/.adc.yaml
62-
viper.SetConfigFile("$HOME/.adc.yaml")
58+
if cfgFile == "" {
59+
cfgFile = "$HOME/.adc.yaml"
6360
}
61+
viper.SetConfigFile(cfgFile)
6462
viper.SetConfigName(".adc")
6563
viper.SetConfigType("yaml")
6664
viper.AddConfigPath("$HOME/")
65+
66+
if _, err := os.Stat(os.ExpandEnv(cfgFile)); err != nil {
67+
color.Yellow("Config file not found at %s. Creating...", cfgFile)
68+
_, err := os.Create(os.ExpandEnv(cfgFile))
69+
if err != nil {
70+
color.Red("Failed to initialize configuration file: %s", err)
71+
}
72+
}
73+
6774
err := viper.ReadInConfig()
6875
if err != nil {
6976
color.Red("Failed to read configuration file, please run `adc configure` first to configure ADC.")

0 commit comments

Comments
 (0)