Skip to content

Commit d275818

Browse files
committed
Start implementing 'config --save'
Instead of having to write out the 'zb' config file yourself, make it possible for 'zb' to write the current settings to file so that it can be edited from a starting point rather than having to start from scratch.
1 parent 150661e commit d275818

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

cmd/config.go

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66

7+
"github.com/msp301/zb/config"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
910
)
@@ -16,11 +17,20 @@ var configCmd = &cobra.Command{
1617
if configFile != "" {
1718
fmt.Printf("Config file used: %s\n\n", viper.ConfigFileUsed())
1819
}
20+
1921
jsonStr, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
2022
fmt.Println(string(jsonStr))
23+
24+
save, _ := cmd.Flags().GetBool("save")
25+
if save {
26+
viper.WriteConfig()
27+
fmt.Println("Configuration saved to", viper.ConfigFileUsed())
28+
}
2129
},
2230
}
2331

2432
func init() {
33+
configCmd.PersistentFlags().BoolP("save", "s", false, "Write current configuration to file")
34+
viper.BindPFlag("save", configCmd.PersistentFlags().Lookup("save"))
2535
rootCmd.AddCommand(configCmd)
2636
}

0 commit comments

Comments
 (0)