Skip to content

Instantly share code, notes, and snippets.

@toddlers
Created December 28, 2019 13:10
Show Gist options
  • Save toddlers/2fa8fcb1d36e9e132b4275f2d748f39f to your computer and use it in GitHub Desktop.
Save toddlers/2fa8fcb1d36e9e132b4275f2d748f39f to your computer and use it in GitHub Desktop.
klog-with-cobra
package main
import (
"flag"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog"
)
// rootCmd represents the base command when called without any subcommands
var (
// cfgFile string
str = "hello World"
rootCmd = &cobra.Command{
Use: "cbrtest",
Short: "A short description of the application",
Long: "A longer description",
Run: func(cmd *cobra.Command, args []string) {
klog.Infof("echo=%v", str)
//klog.V(1).Info("Test Logging")
//print("This is test")
},
}
)
//func Execute() {
// if err := rootCmd.Execute(); err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
//}
func init() {
//cobra.OnInitialize(initConfig)
rootCmd.Flags().SortFlags = false
klog.InitFlags(nil)
// Make cobra aware of select glog flags
// Enabling all flags causes unwanted deprecation warnings
// from glog to always print in plugin mode
pflag.CommandLine.AddGoFlag(flag.CommandLine.Lookup("v"))
pflag.CommandLine.AddGoFlag(flag.CommandLine.Lookup("logtostderr"))
pflag.CommandLine.Set("logtostderr", "true")
// Here you will define your flags and configurations settings
// Cobra supports persistent flags, if defined here,
// will be global for your application
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobratest.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().StringVar(&str, "str", str, "string to print")
}
// initConfig reads in config file and ENV variables if set
//func initConfig() {
// if cfgFile != "" {
// // Use config file from the flag
// viper.SetConfigFile(cfgFile)
// } else {
// // Find home directory
// home, err := homedir.Dir()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
//
// // Search coinfig in home directory with name ".cobratest" ( without extension).
//
// viper.AddConfigPath(home)
// viper.SetConfigName(".cobratest")
// }
//
// // if a config file is found, read it in
// if err := viper.ReadInConfig(); err == nil {
// fmt.Println("Using config file: ", viper.ConfigFileUsed())
// }
//}
//func init() {
// rootCmd.Flags().SortFlags = false
// rootCmd.AddCommand(RunCmd())
// klog.InitFlags(nil)
// goflag.Parse()
// flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
//}
//
//func RunCmd() *cobra.Command {
// runcmd := &cobra.Command{
// Use: "run",
// Short: "run command",
// Long: "Run command",
// Run: func(cmd *cobra.Command, args []string) {
// klog.Infof("echo=%v", str)
// },
// }
// runcmd.Flags().SortFlags = false
// runcmd.Flags().StringVar(&str, "str", str, "string to print")
// return runcmd
//}
// Execute adds all child commands to the root command and
// sets flags appropriately.
// This is called by main.main(). It only happens once to rootCmd
//func Execute() {
// if err := rootCmd.Execute(); err != nil {
// klog.Fatalf(err)
// os.Exit(1)
// }
//}
func main() {
if err := rootCmd.Execute(); err != nil {
klog.Fatalf("root cmd execute failed, err=%v", err)
}
//Execute()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment