zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/exporter/cli/cli.go (about) 1 //go:build !metrics 2 // +build !metrics 3 4 package cli 5 6 import ( 7 "github.com/mitchellh/mapstructure" 8 "github.com/rs/zerolog/log" 9 "github.com/spf13/cobra" 10 "github.com/spf13/viper" 11 12 zerr "zotregistry.dev/zot/errors" 13 "zotregistry.dev/zot/pkg/exporter/api" 14 ) 15 16 // metadataConfig reports metadata after parsing, which we use to track 17 // errors. 18 func metadataConfig(md *mapstructure.Metadata) viper.DecoderConfigOption { 19 return func(c *mapstructure.DecoderConfig) { 20 c.Metadata = md 21 } 22 } 23 24 func NewExporterCmd() *cobra.Command { 25 config := api.DefaultConfig() 26 27 // "config" 28 configCmd := &cobra.Command{ 29 Use: "config <config_file>", 30 Aliases: []string{"config"}, 31 Short: "`config` node exporter properties", 32 Long: "`config` node exporter properties", 33 Run: func(cmd *cobra.Command, args []string) { 34 if len(args) > 0 { 35 loadConfiguration(config, args[0]) 36 } 37 38 c := api.NewController(config) 39 c.Run() 40 }, 41 } 42 43 // "node_exporter" 44 exporterCmd := &cobra.Command{ 45 Use: "zxp", 46 Short: "`zxp`", 47 Long: "`zxp`", 48 Run: func(cmd *cobra.Command, args []string) { 49 _ = cmd.Usage() 50 cmd.SilenceErrors = false 51 }, 52 } 53 54 exporterCmd.AddCommand(configCmd) 55 56 return exporterCmd 57 } 58 59 func loadConfiguration(config *api.Config, configPath string) { 60 viper.SetConfigFile(configPath) 61 62 if err := viper.ReadInConfig(); err != nil { 63 log.Panic().Err(err).Str("config", configPath).Msg("failed to read configuration") 64 } 65 66 metaData := &mapstructure.Metadata{} 67 if err := viper.Unmarshal(&config, metadataConfig(metaData)); err != nil { 68 log.Panic().Err(err).Str("config", configPath).Msg("failed to unmarshal config") 69 } 70 71 if len(metaData.Keys) == 0 { 72 log.Panic().Err(zerr.ErrBadConfig).Str("config", configPath).Msg("bad configuration") 73 } 74 75 if len(metaData.Unused) > 0 { 76 log.Panic().Err(zerr.ErrBadConfig).Interface("unknown fields", metaData.Unused). 77 Str("config", configPath).Msg("bad configuration") 78 } 79 }