&cobra.Command{ PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { // env zap.L().Debug("load env vars") viper.AutomaticEnv() // config file zap.L().Debug("load config files") // ... // flags zap.L().Debug("bind flags") // ... zap.L().Debug("unmarshal config") return viper.Unmarshal(&cfg) }, RunE: func(_ *cobra.Command, args []string) error { zap.L().Info("loaded config", zap.Any("config", cfg)) return nil }, } // ... var debug, verbose bool cobra.OnInitialize(func() { var zapCfg zap.Config switch { ‣ ίʔυதʹదʹϩάΛ͢ ‣ ϑϥάΛ༩͑ͳ͍ʢ[BQΛॳظԽ͠ͳ͍ʣͱ
OPPQMPHHFSʹॻ͖ࠐ·ΕΔ Α͏͢ΔʹԿ͠ͳ͍ ςετʹӨڹͳ͍ͷͰɺΨϯΨϯ͢͠ :) % USER=admin PASS=pass go run . --port 8080 -v 2019-05-20T06:24:57.227+0900 INFO awesomeapp/main.go:50 loaded config {"config": {"Port": 8080,"Host":"localhost","Scheme":"http","User":"admin","Pass":"pass"}} :) % USER=admin PASS=pass go run . --port 8080 --debug {"level":"debug","ts":1558301103.629214,"caller":"awesomeapp/main.go:26","msg":"load env vars"} {"level":"debug","ts":1558301103.629287,"caller":"awesomeapp/main.go:30","msg":"load config files"} {"level":"debug","ts":1558301103.63246,"caller":"awesomeapp/main.go:40","msg":"bind flags"} {"level":"debug","ts":1558301103.632509,"caller":"awesomeapp/main.go:46","msg":"unmarshal config"} {"level":"info","ts":1558301103.632673,"caller":"awesomeapp/main.go:50","msg":"loaded config","config":{"Port": 8080,"Host":"localhost","Scheme":"http","User":"admin","Pass":"pass"}}