Torben Nehmer
7dd11a0879
- NFT and DNS Update skeletons - Integrated viper configuration management - Integrated cobra CLI management - Basic webservice skeleton
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
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", "conf-default.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)
|
|
}
|