From bcc4164cb6e4760861fde568acb0d800ce7dc9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20MATHIEU?= Date: Thu, 5 Sep 2024 00:22:18 +0200 Subject: [PATCH 1/5] Add --context flag --- CHANGELOG.md | 2 ++ cmd/root.go | 14 ++++++++++++++ internal/global/config.go | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29532ad..3a92ce9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- [#214](https://github.com/deviceinsight/kafkactl/pull/214) Add `--context` option to set command's context ## 5.3.0 - 2024-08-14 ### Added diff --git a/cmd/root.go b/cmd/root.go index 15c402c..ac36390 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,8 @@ package cmd import ( "fmt" + "github.com/spf13/viper" + "github.com/deviceinsight/kafkactl/v5/internal/global" "github.com/deviceinsight/kafkactl/v5/cmd/alter" @@ -54,6 +56,18 @@ func NewKafkactlCommand(streams output.IOStreams) *cobra.Command { rootCmd.PersistentFlags().StringVarP(&globalFlags.ConfigFile, "config-file", "C", "", fmt.Sprintf("config file. default locations: %v", globalConfig.DefaultPaths())) rootCmd.PersistentFlags().BoolVarP(&globalFlags.Verbose, "verbose", "V", false, "verbose output") + rootCmd.PersistentFlags().StringVar(&globalFlags.Context, "context", "", "The name of the context to use") + + err := rootCmd.RegisterFlagCompletionFunc("context", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + var contexts []string + for k := range viper.GetStringMap("contexts") { + contexts = append(contexts, k) + } + return contexts, cobra.ShellCompDirectiveDefault + }) + if err != nil { + panic(err) + } k8s.KafkaCtlVersion = Version diff --git a/internal/global/config.go b/internal/global/config.go index e7f77cd..5484cd5 100644 --- a/internal/global/config.go +++ b/internal/global/config.go @@ -2,6 +2,7 @@ package global import ( "errors" + "fmt" "os" "path/filepath" "runtime" @@ -13,6 +14,7 @@ import ( type Flags struct { ConfigFile string + Context string Verbose bool } @@ -52,7 +54,20 @@ func GetFlags() Flags { } func GetCurrentContext() string { - return configInstance.currentContext() + var context = configInstance.Flags().Context + if context != "" { + contexts := viper.GetStringMap("contexts") + + // check if it is an existing context + if _, ok := contexts[context]; !ok { + output.Fail(fmt.Errorf("not a valid context: %s", context)) + } + + return context + } else { + return configInstance.currentContext() + } + } func SetCurrentContext(contextName string) error { From b97f3160fe09651c3f81665a4eda4bcd6dff47ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20MATHIEU?= Date: Thu, 5 Sep 2024 21:59:33 +0200 Subject: [PATCH 2/5] Fix PR number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a92ce9..0d0f92f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- [#214](https://github.com/deviceinsight/kafkactl/pull/214) Add `--context` option to set command's context +- [#215](https://github.com/deviceinsight/kafkactl/pull/215) Add `--context` option to set command's context ## 5.3.0 - 2024-08-14 ### Added From e91d8baeda552fdd8c072839ad7e1ae8e3a2dd65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20MATHIEU?= Date: Thu, 5 Sep 2024 22:10:44 +0200 Subject: [PATCH 3/5] Fix linter issues --- cmd/root.go | 2 +- internal/global/config.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ac36390..9f52f69 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,7 +58,7 @@ func NewKafkactlCommand(streams output.IOStreams) *cobra.Command { rootCmd.PersistentFlags().BoolVarP(&globalFlags.Verbose, "verbose", "V", false, "verbose output") rootCmd.PersistentFlags().StringVar(&globalFlags.Context, "context", "", "The name of the context to use") - err := rootCmd.RegisterFlagCompletionFunc("context", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + err := rootCmd.RegisterFlagCompletionFunc("context", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { var contexts []string for k := range viper.GetStringMap("contexts") { contexts = append(contexts, k) diff --git a/internal/global/config.go b/internal/global/config.go index 5484cd5..22bc737 100644 --- a/internal/global/config.go +++ b/internal/global/config.go @@ -64,10 +64,9 @@ func GetCurrentContext() string { } return context - } else { - return configInstance.currentContext() } + return configInstance.currentContext() } func SetCurrentContext(contextName string) error { From 4fe575c53990773421ca95cff24a5e8848d5484e Mon Sep 17 00:00:00 2001 From: Dirk Wilden Date: Thu, 28 Nov 2024 08:47:23 +0100 Subject: [PATCH 4/5] refactor listing available contexts --- cmd/config/useContext.go | 13 +------------ cmd/root.go | 8 +------- internal/global/config.go | 12 ++++++++++++ 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/cmd/config/useContext.go b/cmd/config/useContext.go index 887bbaf..c524100 100644 --- a/cmd/config/useContext.go +++ b/cmd/config/useContext.go @@ -1,10 +1,7 @@ package config import ( - "sort" - "github.com/deviceinsight/kafkactl/v5/internal/global" - "github.com/deviceinsight/kafkactl/v5/internal/output" "github.com/pkg/errors" @@ -42,15 +39,7 @@ func newUseContextCmd() *cobra.Command { return nil, cobra.ShellCompDirectiveNoFileComp } - contextMap := viper.GetStringMap("contexts") - contexts := make([]string, 0, len(contextMap)) - for k := range contextMap { - contexts = append(contexts, k) - } - - sort.Strings(contexts) - - return contexts, cobra.ShellCompDirectiveNoFileComp + return global.ListAvailableContexts(), cobra.ShellCompDirectiveNoFileComp }, } diff --git a/cmd/root.go b/cmd/root.go index 9f52f69..5d23cba 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,8 +3,6 @@ package cmd import ( "fmt" - "github.com/spf13/viper" - "github.com/deviceinsight/kafkactl/v5/internal/global" "github.com/deviceinsight/kafkactl/v5/cmd/alter" @@ -59,11 +57,7 @@ func NewKafkactlCommand(streams output.IOStreams) *cobra.Command { rootCmd.PersistentFlags().StringVar(&globalFlags.Context, "context", "", "The name of the context to use") err := rootCmd.RegisterFlagCompletionFunc("context", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { - var contexts []string - for k := range viper.GetStringMap("contexts") { - contexts = append(contexts, k) - } - return contexts, cobra.ShellCompDirectiveDefault + return global.ListAvailableContexts(), cobra.ShellCompDirectiveNoFileComp }) if err != nil { panic(err) diff --git a/internal/global/config.go b/internal/global/config.go index 22bc737..3f3270e 100644 --- a/internal/global/config.go +++ b/internal/global/config.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "sort" "strings" "github.com/deviceinsight/kafkactl/v5/internal/output" @@ -53,6 +54,17 @@ func GetFlags() Flags { return configInstance.flags } +func ListAvailableContexts() []string { + + var contexts []string + for k := range viper.GetStringMap("contexts") { + contexts = append(contexts, k) + } + + sort.Strings(contexts) + return contexts +} + func GetCurrentContext() string { var context = configInstance.Flags().Context if context != "" { From 76996bbd714bf655e100e83f1236df0f1f32a705 Mon Sep 17 00:00:00 2001 From: Dirk Wilden Date: Thu, 28 Nov 2024 08:47:35 +0100 Subject: [PATCH 5/5] add tests for --context param --- cmd/root_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/cmd/root_test.go b/cmd/root_test.go index 47cf35c..aa5c310 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -3,8 +3,11 @@ package cmd_test import ( "fmt" "os" + "strings" "testing" + "github.com/deviceinsight/kafkactl/v5/internal/output" + "github.com/deviceinsight/kafkactl/v5/internal/global" "github.com/deviceinsight/kafkactl/v5/internal/testutil" @@ -125,3 +128,53 @@ func TestEnvironmentVariableLoadingAliases(t *testing.T) { testutil.AssertEquals(t, "WaitForAll", viper.GetString("contexts.default.producer.requiredAcks")) testutil.AssertEquals(t, "1234", viper.GetString("contexts.default.producer.maxMessageBytes")) } + +func TestContextFlag(t *testing.T) { + + testutil.StartUnitTest(t) + + kafkaCtl := testutil.CreateKafkaCtlCommand() + + if _, err := kafkaCtl.Execute("version", "--context", "sasl-user"); err != nil { + t.Fatalf("failed to execute command: %v", err) + } + + testutil.AssertEquals(t, "sasl-user", global.GetCurrentContext()) +} + +func TestContextFlagProvidesUnknownContext(t *testing.T) { + + testutil.StartUnitTest(t) + + kafkaCtl := testutil.CreateKafkaCtlCommand() + + if _, err := kafkaCtl.Execute("version", "--context", "unknown-context"); err != nil { + t.Fatalf("failed to execute command: %v", err) + } + + var failError error + + output.Fail = func(err error) { + failError = err + } + + global.GetCurrentContext() + + testutil.AssertErrorContains(t, "not a valid context: unknown-context", failError) +} + +func TestContextFlagAutoCompletion(t *testing.T) { + + testutil.StartUnitTest(t) + + kafkaCtl := testutil.CreateKafkaCtlCommand() + kafkaCtl.Verbose = false + + if _, err := kafkaCtl.Execute("__complete", "--context", ""); err != nil { + t.Fatalf("failed to execute command: %v", err) + } + + outputLines := strings.Split(strings.TrimSpace(kafkaCtl.GetStdOut()), "\n") + + testutil.AssertArraysEquals(t, []string{"default", "k8s-mock", "no-avro", "sasl-admin", "sasl-user"}, outputLines[:len(outputLines)-1]) +}