github.com/noironetworks/cilium-net@v1.6.12/cilium-health/cmd/root.go (about) 1 // Copyright 2017-2018 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "fmt" 19 "os" 20 "strings" 21 22 clientPkg "github.com/cilium/cilium/pkg/health/client" 23 "github.com/cilium/cilium/pkg/logging" 24 "github.com/cilium/cilium/pkg/logging/logfields" 25 "github.com/cilium/cilium/pkg/option" 26 27 "github.com/sirupsen/logrus" 28 "github.com/spf13/cobra" 29 "github.com/spf13/cobra/doc" 30 "github.com/spf13/viper" 31 ) 32 33 const targetName = "cilium-health" 34 35 var ( 36 cfgFile string 37 client *clientPkg.Client 38 cmdRefDir string 39 log = logging.DefaultLogger.WithField(logfields.LogSubsys, targetName) 40 logOpts = make(map[string]string) 41 ) 42 43 // rootCmd represents the base command when called without any subcommands 44 var rootCmd = &cobra.Command{ 45 Use: targetName, 46 Short: "Cilium Health Client", 47 Long: `Client for querying the Cilium health status API`, 48 Run: run, 49 } 50 51 // Fatalf prints the Printf formatted message to stderr and exits the program 52 // Note: os.Exit(1) is not recoverable 53 func Fatalf(msg string, args ...interface{}) { 54 fmt.Fprintf(os.Stderr, "Error: %s\n", fmt.Sprintf(msg, args...)) 55 os.Exit(-1) 56 } 57 58 // Execute adds all child commands to the root command sets flags appropriately. 59 // This is called by main.main(). It only needs to happen once to the rootCmd. 60 func Execute() { 61 if err := rootCmd.Execute(); err != nil { 62 fmt.Println(err) 63 os.Exit(-1) 64 } 65 } 66 67 func init() { 68 cobra.OnInitialize(initConfig) 69 flags := rootCmd.PersistentFlags() 70 flags.BoolP("debug", "D", false, "Enable debug messages") 71 flags.StringP("host", "H", "", "URI to cilium-health server API") 72 flags.StringSlice("log-driver", []string{}, "Logging endpoints to use for example syslog") 73 flags.Var(option.NewNamedMapOptions("log-opts", &logOpts, nil), 74 "log-opt", "Log driver options for cilium-health") 75 viper.BindPFlags(flags) 76 77 flags.StringVar(&cmdRefDir, "cmdref", "", "Path to cmdref output directory") 78 flags.MarkHidden("cmdref") 79 } 80 81 // initConfig reads in config file and ENV variables if set. 82 func initConfig() { 83 viper.SetEnvPrefix("cilium") 84 viper.SetConfigName(".cilium-health") // name of config file (without extension) 85 viper.AddConfigPath("$HOME") // adding home directory as first search path 86 87 if viper.GetBool("debug") { 88 log.Level = logrus.DebugLevel 89 } else { 90 log.Level = logrus.InfoLevel 91 } 92 93 if cl, err := clientPkg.NewClient(viper.GetString("host")); err != nil { 94 Fatalf("Error while creating client: %s\n", err) 95 } else { 96 client = cl 97 } 98 } 99 100 func run(cmd *cobra.Command, args []string) { 101 // Logging should always be bootstrapped first. Do not add any code above this! 102 logging.SetupLogging(viper.GetStringSlice("log-driver"), logOpts, "cilium-health", viper.GetBool("debug")) 103 104 if cmdRefDir != "" { 105 // Remove the line 'Auto generated by spf13/cobra on ...' 106 cmd.DisableAutoGenTag = true 107 if err := doc.GenMarkdownTreeCustom(cmd, cmdRefDir, filePrepend, linkHandler); err != nil { 108 log.Fatal(err) 109 } 110 os.Exit(0) 111 } 112 113 cmd.Help() 114 } 115 116 func linkHandler(s string) string { 117 // The generated files have a 'See also' section but the URL's are 118 // hardcoded to use Markdown but we only want / have them in HTML 119 // later. 120 return strings.Replace(s, ".md", ".html", 1) 121 } 122 123 func filePrepend(s string) string { 124 // Prepend a HTML comment that this file is autogenerated. So that 125 // users are warned before fixing issues in the Markdown files. Should 126 // never show up on the web. 127 return fmt.Sprintf("%s\n\n", "<!-- This file was autogenerated via cilium-agent --cmdref, do not edit manually-->") 128 }