dyndns/cmd/write-default-config.go

42 lines
1.1 KiB
Go
Raw Normal View History

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)
filename := cmdWriteDefaultConfig.Flags().StringP("filename", "f", "default-config.yml",
"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)
}