MCPcopy
hub / github.com/cilium/cilium / NewWithViper

Function NewWithViper

hubble/cmd/root.go:36–106  ·  view source on GitHub ↗

NewWithViper creates a new root command with the given viper.

(vp *viper.Viper)

Source from the content-addressed store, hash-verified

34
35// NewWithViper creates a new root command with the given viper.
36func NewWithViper(vp *viper.Viper) *cobra.Command {
37 // Initialize must be called after the sub-commands are all added
38 defer template.Initialize()
39
40 rootCmd := &cobra.Command{
41 Use: "hubble",
42 Short: "CLI",
43 Long: `Hubble is a utility to observe and inspect recent Cilium routed traffic in a cluster.`,
44 SilenceErrors: true, // this is being handled in main, no need to duplicate error messages
45 SilenceUsage: true,
46 Version: pkg.Version,
47 PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
48 if err := validate.Flags(cmd, vp); err != nil {
49 return err
50 }
51 return conn.Init(vp)
52 },
53 }
54
55 cobra.OnInitialize(func() {
56 if cfg := vp.GetString(config.KeyConfig); cfg != "" { // enable ability to specify config file via flag
57 vp.SetConfigFile(cfg)
58 }
59 // if a config file is found, read it in.
60 err := vp.ReadInConfig()
61 // initialize the logger after all the config parameters get loaded to viper.
62 logger.Initialize(newLogHandler(vp))
63 if err == nil {
64 logger.Logger.Debug("Using config file", logfields.ConfigFile, vp.ConfigFileUsed())
65 }
66
67 username := vp.GetString(config.KeyBasicAuthUsername)
68 password := vp.GetString(config.KeyBasicAuthPassword)
69 if username != "" && password != "" {
70 optFunc := func(*viper.Viper) (grpc.DialOption, error) {
71 return conn.WithBasicAuth(username, password), nil
72 }
73 conn.GRPCOptionFuncs = append(conn.GRPCOptionFuncs, optFunc)
74 }
75 })
76
77 flags := rootCmd.PersistentFlags()
78 // config.GlobalFlags can be used with any command
79 flags.AddFlagSet(config.GlobalFlags)
80 // config.ServerFlags is added to the root command's persistent flags
81 // so that "hubble --server foo observe" still works
82 flags.AddFlagSet(config.ServerFlags)
83 vp.BindPFlags(flags)
84
85 // config.ServerFlags is only useful to a subset of commands so do not
86 // add it by default in the help template
87 // config.GlobalFlags is always added to the help template as it's global
88 // to all commands
89 template.RegisterFlagSets(rootCmd)
90 rootCmd.SetUsageTemplate(template.Usage)
91
92 rootCmd.SetErr(os.Stderr)
93 rootCmd.SetVersionTemplate("{{with .Name}}{{printf \"%s \" .}}{{end}}{{printf \"%s\" .Version}}\r\n")

Callers 1

NewFunction · 0.85

Calls 15

InitializeFunction · 0.92
FlagsFunction · 0.92
InitFunction · 0.92
InitializeFunction · 0.92
WithBasicAuthFunction · 0.92
RegisterFlagSetsFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…