🐛 Fix the version command

This commit is contained in:
M. Mert Yildiran 2022-11-29 04:19:56 +03:00
parent f31ad4f9f0
commit fa0fb62e07
No known key found for this signature in database
GPG Key ID: DA5D6DCBB758A461
5 changed files with 43 additions and 51 deletions

View File

@ -5,7 +5,6 @@ import (
"github.com/creasty/defaults" "github.com/creasty/defaults"
"github.com/kubeshark/kubeshark/config" "github.com/kubeshark/kubeshark/config"
"github.com/kubeshark/kubeshark/kubeshark/version"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "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().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().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. // 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. // This is called by main.main(). It only needs to happen once to the tapCmd.
func Execute() { func Execute() {
go version.CheckNewerVersion()
cobra.CheckErr(rootCmd.Execute()) cobra.CheckErr(rootCmd.Execute())
} }

View File

@ -1,11 +1,11 @@
package cmd package cmd
import ( import (
"fmt"
"strconv" "strconv"
"time" "time"
"github.com/creasty/defaults" "github.com/kubeshark/kubeshark/config"
"github.com/kubeshark/kubeshark/config/configStructs"
"github.com/kubeshark/kubeshark/kubeshark" "github.com/kubeshark/kubeshark/kubeshark"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -16,24 +16,20 @@ var versionCmd = &cobra.Command{
Short: "Print version info", Short: "Print version info",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
timeStampInt, _ := strconv.ParseInt(kubeshark.BuildTimestamp, 10, 0) timeStampInt, _ := strconv.ParseInt(kubeshark.BuildTimestamp, 10, 0)
if config.DebugMode {
log.Info(). log.Info().
Str("version", kubeshark.Ver). Str("version", kubeshark.Ver).
Str("branch", kubeshark.Branch). Str("branch", kubeshark.Branch).
Str("commit-hash", kubeshark.GitCommitHash). Str("commit-hash", kubeshark.GitCommitHash).
Time("build-time", time.Unix(timeStampInt, 0)). Time("build-time", time.Unix(timeStampInt, 0)).
Send() Send()
} else {
fmt.Println(kubeshark.Ver)
}
return nil return nil
}, },
} }
func init() { func init() {
rootCmd.AddCommand(versionCmd) 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")
} }

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/creasty/defaults" "github.com/creasty/defaults"
"github.com/kubeshark/kubeshark/kubeshark/version"
"github.com/kubeshark/kubeshark/utils" "github.com/kubeshark/kubeshark/utils"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -28,19 +29,27 @@ const (
var ( var (
Config = ConfigStruct{} Config = ConfigStruct{}
DebugMode bool
cmdName string cmdName string
) )
func InitConfig(cmd *cobra.Command) error { func InitConfig(cmd *cobra.Command) error {
debugMode, err := cmd.Flags().GetBool(DebugFlag) var err error
DebugMode, err = cmd.Flags().GetBool(DebugFlag)
if err != nil { if err != nil {
log.Error().Err(err).Msg(fmt.Sprintf("Can't recieve '%s' flag", DebugFlag)) log.Error().Err(err).Msg(fmt.Sprintf("Can't recieve '%s' flag", DebugFlag))
} }
if debugMode { if DebugMode {
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
if cmd.Use == "version" {
return nil
}
go version.CheckNewerVersion()
Config.Hub = HubConfig{ Config.Hub = HubConfig{
PortForward{ PortForward{
8898, 8898,

View File

@ -59,7 +59,6 @@ type ConfigStruct struct {
Front FrontConfig `yaml:"front"` Front FrontConfig `yaml:"front"`
Tap configStructs.TapConfig `yaml:"tap"` Tap configStructs.TapConfig `yaml:"tap"`
Check configStructs.CheckConfig `yaml:"check"` Check configStructs.CheckConfig `yaml:"check"`
Version configStructs.VersionConfig `yaml:"version"`
View configStructs.ViewConfig `yaml:"view"` View configStructs.ViewConfig `yaml:"view"`
Logs configStructs.LogsConfig `yaml:"logs"` Logs configStructs.LogsConfig `yaml:"logs"`
Config configStructs.ConfigConfig `yaml:"config,omitempty"` Config configStructs.ConfigConfig `yaml:"config,omitempty"`

View File

@ -1,9 +0,0 @@
package configStructs
const (
DebugInfoVersionName = "debug"
)
type VersionConfig struct {
DebugInfo bool `yaml:"debug" default:"false"`
}