2021-08-15 19:37:11 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdWriteDefaultConfig = &cobra.Command{
|
|
|
|
Use: "write-default-config",
|
|
|
|
Short: "Write default config to file",
|
|
|
|
Long: `Writes the default configuration to a new configuration file, which
|
|
|
|
can be further customized by you.`,
|
|
|
|
// Run is defined inline in init to capture Flag variables in the closure
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(cmdWriteDefaultConfig)
|
2022-06-28 17:13:05 +00:00
|
|
|
filename := cmdWriteDefaultConfig.Flags().StringP("filename", "f", "default-config.yml",
|
2021-08-15 19:37:11 +00:00
|
|
|
"The config filename to use, defaults to conf-default.yml")
|
|
|
|
|
|
|
|
cmdWriteDefaultConfig.Run = func(cmd *cobra.Command, args []string) {
|
|
|
|
writeDefaultConfig(*filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeDefaultConfig(filename string) {
|
|
|
|
if filepath.Ext(filename) != ".yml" {
|
|
|
|
log.Fatalln("The default config filename must end in .yml")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.Reset()
|
|
|
|
setAllConfigDefaults()
|
|
|
|
err := viper.WriteConfigAs(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not write default configuration file %s: %s", filename, err)
|
|
|
|
}
|
|
|
|
log.Printf("Default configuration written as %s", filename)
|
|
|
|
}
|