From 4b2c678fa302950e507ac4f1d920369549f6f9b6 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Sun, 29 Jan 2023 23:52:49 +0300 Subject: [PATCH] :sparkles: Add `community`, `edition` and `upgrade` commands --- cmd/community.go | 37 +++++++++++++++++++++++++++++++++++++ cmd/config.go | 2 +- cmd/edition.go | 23 +++++++++++++++++++++++ cmd/upgrade.go | 37 +++++++++++++++++++++++++++++++++++++ config/config.go | 12 ++++++++---- config/configStruct.go | 1 + 6 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 cmd/community.go create mode 100644 cmd/edition.go create mode 100644 cmd/upgrade.go diff --git a/cmd/community.go b/cmd/community.go new file mode 100644 index 000000000..aff4870f5 --- /dev/null +++ b/cmd/community.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/kubeshark/kubeshark/config" + "github.com/kubeshark/kubeshark/misc" + "github.com/kubeshark/kubeshark/utils" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var communityCmd = &cobra.Command{ + Use: "community", + Short: fmt.Sprintf("Use %s Community Edition. (default)", misc.Software), + RunE: func(cmd *cobra.Command, args []string) error { + edition := "community" + config.Config.Edition = edition + if err := config.WriteConfig(&config.Config); err != nil { + log.Error().Err(err).Msg("Failed writing config.") + return nil + } + + log.Info().Msgf("%s edition has been set to: %s", misc.Software, strings.Title(edition)) + + log.Warn(). + Str("command", fmt.Sprintf("%s tap", misc.Program)). + Msg(fmt.Sprintf(utils.Yellow, "Now you can run:")) + + return nil + }, +} + +func init() { + rootCmd.AddCommand(communityCmd) +} diff --git a/cmd/config.go b/cmd/config.go index 15ac9bd8a..e7cf618fc 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -36,7 +36,7 @@ var configCmd = &cobra.Command{ return nil } - log.Debug().Str("template", template).Msg("Writing template config...") + log.Debug().Str("template", template).Msg("Printing template config...") fmt.Printf("%v", template) } diff --git a/cmd/edition.go b/cmd/edition.go new file mode 100644 index 000000000..980fdc99d --- /dev/null +++ b/cmd/edition.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/kubeshark/kubeshark/config" + "github.com/kubeshark/kubeshark/misc" + "github.com/spf13/cobra" +) + +var editionCmd = &cobra.Command{ + Use: "edition", + Short: fmt.Sprintf("Print the current edition of %s.", misc.Software), + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println(strings.Title(config.Config.Edition)) + return nil + }, +} + +func init() { + rootCmd.AddCommand(editionCmd) +} diff --git a/cmd/upgrade.go b/cmd/upgrade.go new file mode 100644 index 000000000..7edb721e7 --- /dev/null +++ b/cmd/upgrade.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/kubeshark/kubeshark/config" + "github.com/kubeshark/kubeshark/misc" + "github.com/kubeshark/kubeshark/utils" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var upgradeCmd = &cobra.Command{ + Use: "upgrade", + Short: fmt.Sprintf("Use %s Pro Edition.", misc.Software), + RunE: func(cmd *cobra.Command, args []string) error { + edition := "pro" + config.Config.Edition = edition + if err := config.WriteConfig(&config.Config); err != nil { + log.Error().Err(err).Msg("Failed writing config.") + return nil + } + + log.Info().Msgf("%s edition has been set to: %s", misc.Software, strings.Title(edition)) + + log.Warn(). + Str("command", fmt.Sprintf("%s tap", misc.Program)). + Msg(fmt.Sprintf(utils.Yellow, "Now you can run:")) + + return nil + }, +} + +func init() { + rootCmd.AddCommand(upgradeCmd) +} diff --git a/config/config.go b/config/config.go index f5acd350a..e93afda1a 100644 --- a/config/config.go +++ b/config/config.go @@ -48,7 +48,9 @@ func InitConfig(cmd *cobra.Command) error { return nil } - go version.CheckNewerVersion() + if cmd.Use != "edition" { + go version.CheckNewerVersion() + } Config = CreateDefaultConfig() cmdName = cmd.Name() @@ -59,7 +61,7 @@ func InitConfig(cmd *cobra.Command) error { configFilePathFlag := cmd.Flags().Lookup(ConfigFilePathCommandName) configFilePath := configFilePathFlag.Value.String() - if err := loadConfigFile(configFilePath, &Config); err != nil { + if err := loadConfigFile(configFilePath, &Config, cmd.Use != "edition"); err != nil { if configFilePathFlag.Changed || !os.IsNotExist(err) { return fmt.Errorf("invalid config, %w\n"+ "you can regenerate the file by removing it (%v) and using `kubeshark config -r`", err, configFilePath) @@ -99,7 +101,7 @@ func WriteConfig(config *ConfigStruct) error { return nil } -func loadConfigFile(configFilePath string, config *ConfigStruct) error { +func loadConfigFile(configFilePath string, config *ConfigStruct, logPath bool) error { reader, openErr := os.Open(configFilePath) if openErr != nil { return openErr @@ -114,7 +116,9 @@ func loadConfigFile(configFilePath string, config *ConfigStruct) error { return err } - log.Info().Str("path", configFilePath).Msg("Found config file!") + if logPath { + log.Info().Str("path", configFilePath).Msg("Found config file!") + } return nil } diff --git a/config/configStruct.go b/config/configStruct.go index 21a0be613..88f70b080 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -35,6 +35,7 @@ type ConfigStruct struct { DumpLogs bool `yaml:"dumplogs" default:"false"` ConfigFilePath string `yaml:"configpath,omitempty" readonly:""` HeadlessMode bool `yaml:"headless" default:"false"` + Edition string `yaml:"edition" default:"community"` } func (config *ConfigStruct) SetDefaults() {