github.com/cilium/cilium@v1.16.2/cilium-health/cmd/root.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"github.com/spf13/cobra"
    12  	"github.com/spf13/cobra/doc"
    13  	"github.com/spf13/viper"
    14  
    15  	clientPkg "github.com/cilium/cilium/pkg/health/client"
    16  	"github.com/cilium/cilium/pkg/logging"
    17  	"github.com/cilium/cilium/pkg/logging/logfields"
    18  	"github.com/cilium/cilium/pkg/option"
    19  )
    20  
    21  const targetName = "cilium-health"
    22  
    23  var (
    24  	client    *clientPkg.Client
    25  	cmdRefDir string
    26  	log       = logging.DefaultLogger.WithField(logfields.LogSubsys, targetName)
    27  	logOpts   = make(map[string]string)
    28  )
    29  
    30  // rootCmd represents the base command when called without any subcommands
    31  var rootCmd = &cobra.Command{
    32  	Use:   targetName,
    33  	Short: "Cilium Health Client",
    34  	Long:  `Client for querying the Cilium health status API`,
    35  	Run:   run,
    36  }
    37  
    38  // Fatalf prints the Printf formatted message to stderr and exits the program
    39  // Note: os.Exit(1) is not recoverable
    40  func Fatalf(msg string, args ...interface{}) {
    41  	fmt.Fprintf(os.Stderr, "Error: %s\n", fmt.Sprintf(msg, args...))
    42  	os.Exit(1)
    43  }
    44  
    45  // Execute adds all child commands to the root command sets flags appropriately.
    46  // This is called by main.main(). It only needs to happen once to the rootCmd.
    47  func Execute() {
    48  	if err := rootCmd.Execute(); err != nil {
    49  		fmt.Println(err)
    50  		os.Exit(1)
    51  	}
    52  }
    53  
    54  func init() {
    55  	cobra.OnInitialize(initConfig)
    56  	flags := rootCmd.PersistentFlags()
    57  	flags.BoolP("debug", "D", false, "Enable debug messages")
    58  	flags.StringP("host", "H", "", "URI to cilium-health server API")
    59  	flags.StringSlice("log-driver", []string{}, "Logging endpoints to use for example syslog")
    60  	flags.Var(option.NewNamedMapOptions("log-opts", &logOpts, nil),
    61  		"log-opt", "Log driver options for cilium-health e.g. syslog.level=info,syslog.facility=local5,syslog.tag=cilium-agent")
    62  	viper.BindPFlags(flags)
    63  
    64  	flags.StringVar(&cmdRefDir, "cmdref", "", "Path to cmdref output directory")
    65  	flags.MarkHidden("cmdref")
    66  }
    67  
    68  // initConfig reads in config file and ENV variables if set.
    69  func initConfig() {
    70  	viper.SetEnvPrefix("cilium")
    71  	viper.SetConfigName(".cilium-health") // name of config file (without extension)
    72  	viper.AddConfigPath("$HOME")          // adding home directory as first search path
    73  
    74  	if viper.GetBool("debug") {
    75  		log.Level = logrus.DebugLevel
    76  	} else {
    77  		log.Level = logrus.InfoLevel
    78  	}
    79  
    80  	if cl, err := clientPkg.NewClient(viper.GetString("host")); err != nil {
    81  		Fatalf("Error while creating client: %s\n", err)
    82  	} else {
    83  		client = cl
    84  	}
    85  }
    86  
    87  func run(cmd *cobra.Command, args []string) {
    88  	// Logging should always be bootstrapped first. Do not add any code above this!
    89  	if err := logging.SetupLogging(viper.GetStringSlice("log-driver"), logging.LogOptions(logOpts), "cilium-health", viper.GetBool("debug")); err != nil {
    90  		log.Fatal(err)
    91  	}
    92  
    93  	if cmdRefDir != "" {
    94  		// Remove the line 'Auto generated by spf13/cobra on ...'
    95  		cmd.DisableAutoGenTag = true
    96  		if err := doc.GenMarkdownTreeCustom(cmd, cmdRefDir, filePrepend, linkHandler); err != nil {
    97  			log.Fatal(err)
    98  		}
    99  		os.Exit(0)
   100  	}
   101  
   102  	cmd.Help()
   103  }
   104  
   105  func linkHandler(s string) string {
   106  	return s
   107  }
   108  
   109  func filePrepend(s string) string {
   110  	// Prepend a HTML comment that this file is autogenerated. So that
   111  	// users are warned before fixing issues in the Markdown files.  Should
   112  	// never show up on the web.
   113  	return fmt.Sprintf("%s\n\n", "<!-- This file was autogenerated via cilium-agent --cmdref, do not edit manually-->")
   114  }