diff --git a/cmd/root.go b/cmd/root.go index 5f84ae72b..bebd73b07 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,7 +5,6 @@ import ( "github.com/creasty/defaults" "github.com/kubeshark/kubeshark/config" - "github.com/kubeshark/kubeshark/kubeshark/version" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -32,13 +31,11 @@ func init() { rootCmd.PersistentFlags().StringSlice(config.SetCommandName, []string{}, fmt.Sprintf("Override values using --%s", config.SetCommandName)) rootCmd.PersistentFlags().String(config.ConfigFilePathCommandName, defaultConfig.ConfigFilePath, fmt.Sprintf("Override config file path using --%s", config.ConfigFilePathCommandName)) - rootCmd.PersistentFlags().Bool("debug", false, "Enable debug mode.") + rootCmd.PersistentFlags().BoolP("debug", "d", false, "Enable debug mode.") } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the tapCmd. func Execute() { - go version.CheckNewerVersion() - cobra.CheckErr(rootCmd.Execute()) } diff --git a/cmd/version.go b/cmd/version.go index 623d264f2..a696d9afa 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,11 +1,11 @@ package cmd import ( + "fmt" "strconv" "time" - "github.com/creasty/defaults" - "github.com/kubeshark/kubeshark/config/configStructs" + "github.com/kubeshark/kubeshark/config" "github.com/kubeshark/kubeshark/kubeshark" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -16,24 +16,20 @@ var versionCmd = &cobra.Command{ Short: "Print version info", RunE: func(cmd *cobra.Command, args []string) error { timeStampInt, _ := strconv.ParseInt(kubeshark.BuildTimestamp, 10, 0) - log.Info(). - Str("version", kubeshark.Ver). - Str("branch", kubeshark.Branch). - Str("commit-hash", kubeshark.GitCommitHash). - Time("build-time", time.Unix(timeStampInt, 0)). - Send() + if config.DebugMode { + log.Info(). + Str("version", kubeshark.Ver). + Str("branch", kubeshark.Branch). + Str("commit-hash", kubeshark.GitCommitHash). + Time("build-time", time.Unix(timeStampInt, 0)). + Send() + } else { + fmt.Println(kubeshark.Ver) + } return nil }, } func init() { rootCmd.AddCommand(versionCmd) - - defaultVersionConfig := configStructs.VersionConfig{} - if err := defaults.Set(&defaultVersionConfig); err != nil { - log.Error().Err(err).Send() - } - - versionCmd.Flags().BoolP(configStructs.DebugInfoVersionName, "d", defaultVersionConfig.DebugInfo, "Provide all information about version") - } diff --git a/config/config.go b/config/config.go index f4b7705f9..e8bc04156 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/creasty/defaults" + "github.com/kubeshark/kubeshark/kubeshark/version" "github.com/kubeshark/kubeshark/utils" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -27,20 +28,28 @@ const ( ) var ( - Config = ConfigStruct{} - cmdName string + Config = ConfigStruct{} + DebugMode bool + cmdName string ) func InitConfig(cmd *cobra.Command) error { - debugMode, err := cmd.Flags().GetBool(DebugFlag) + var err error + DebugMode, err = cmd.Flags().GetBool(DebugFlag) if err != nil { log.Error().Err(err).Msg(fmt.Sprintf("Can't recieve '%s' flag", DebugFlag)) } - if debugMode { + if DebugMode { zerolog.SetGlobalLevel(zerolog.DebugLevel) } + if cmd.Use == "version" { + return nil + } + + go version.CheckNewerVersion() + Config.Hub = HubConfig{ PortForward{ 8898, diff --git a/config/configStruct.go b/config/configStruct.go index aff3e1dfc..9a6804d91 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -55,24 +55,23 @@ func CreateDefaultConfig() ConfigStruct { } type ConfigStruct struct { - Hub HubConfig `yaml:"hub"` - Front FrontConfig `yaml:"front"` - Tap configStructs.TapConfig `yaml:"tap"` - Check configStructs.CheckConfig `yaml:"check"` - Version configStructs.VersionConfig `yaml:"version"` - View configStructs.ViewConfig `yaml:"view"` - Logs configStructs.LogsConfig `yaml:"logs"` - Config configStructs.ConfigConfig `yaml:"config,omitempty"` - ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"` - ResourcesNamespace string `yaml:"resources-namespace" default:"kubeshark"` - DumpLogs bool `yaml:"dump-logs" default:"false"` - KubeConfigPathStr string `yaml:"kube-config-path"` - KubeContext string `yaml:"kube-context"` - ConfigFilePath string `yaml:"config-path,omitempty" readonly:""` - HeadlessMode bool `yaml:"headless" default:"false"` - LogLevelStr string `yaml:"log-level,omitempty" default:"info" readonly:""` - ServiceMap bool `yaml:"service-map" default:"true"` - OAS models.OASConfig `yaml:"oas"` + Hub HubConfig `yaml:"hub"` + Front FrontConfig `yaml:"front"` + Tap configStructs.TapConfig `yaml:"tap"` + Check configStructs.CheckConfig `yaml:"check"` + View configStructs.ViewConfig `yaml:"view"` + Logs configStructs.LogsConfig `yaml:"logs"` + Config configStructs.ConfigConfig `yaml:"config,omitempty"` + ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"` + ResourcesNamespace string `yaml:"resources-namespace" default:"kubeshark"` + DumpLogs bool `yaml:"dump-logs" default:"false"` + KubeConfigPathStr string `yaml:"kube-config-path"` + KubeContext string `yaml:"kube-context"` + ConfigFilePath string `yaml:"config-path,omitempty" readonly:""` + HeadlessMode bool `yaml:"headless" default:"false"` + LogLevelStr string `yaml:"log-level,omitempty" default:"info" readonly:""` + ServiceMap bool `yaml:"service-map" default:"true"` + OAS models.OASConfig `yaml:"oas"` } func (config *ConfigStruct) validate() error { diff --git a/config/configStructs/versionConfig.go b/config/configStructs/versionConfig.go deleted file mode 100644 index fd09a4747..000000000 --- a/config/configStructs/versionConfig.go +++ /dev/null @@ -1,9 +0,0 @@ -package configStructs - -const ( - DebugInfoVersionName = "debug" -) - -type VersionConfig struct { - DebugInfo bool `yaml:"debug" default:"false"` -}