Skip to content

Instantly share code, notes, and snippets.

@nak3
Created November 26, 2017 05:27
Show Gist options
  • Save nak3/78a32817a8a3950ae48f239a44cd3663 to your computer and use it in GitHub Desktop.
Save nak3/78a32817a8a3950ae48f239a44cd3663 to your computer and use it in GitHub Desktop.
test of viper and glog combination
package main
import (
"flag"
"fmt"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
RootCmd = &cobra.Command{
Use: "test",
Run: func(cmd *cobra.Command, args []string) {
glog.Infof("glog info level")
fmt.Printf("get a value from viper: %s\n", viper.GetString("viper-test"))
},
}
)
func init() {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
// For https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
flag.CommandLine.Parse([]string{})
RootCmd.PersistentFlags().String("viper-test", "foo", "some flag for viper test")
viper.BindPFlag("viper-test", RootCmd.PersistentFlags().Lookup("viper-test"))
}
func main() {
RootCmd.Execute()
}
/*
$ go run main.go -h
Usage:
test [flags]
Flags:
--alsologtostderr log to standard error as well as files
-h, --help help for test
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory
--logtostderr log to standard error instead of files
--stderrthreshold severity logs at or above this threshold go to stderr (default 2)
-v, --v Level log level for V logs
--viper-test string some flag for viper test (default "foo")
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
$ go run main.go --viper-test=bar --stderrthreshold=info
I1126 14:24:55.255628 1150 main.go:17] glog info level
get a value from viper: bar
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment