Skip to content

Commit

Permalink
Add --context flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cykl committed Sep 5, 2024
1 parent 293ab5c commit bcc4164
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {

Check warning on line 61 in cmd/root.go

View workflow job for this annotation

GitHub Actions / Lint

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)
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

Expand Down
17 changes: 16 additions & 1 deletion internal/global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package global

import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
Expand All @@ -13,6 +14,7 @@ import (

type Flags struct {
ConfigFile string
Context string
Verbose bool
}

Expand Down Expand Up @@ -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 {

Check warning on line 67 in internal/global/config.go

View workflow job for this annotation

GitHub Actions / Lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return configInstance.currentContext()
}

}

func SetCurrentContext(contextName string) error {
Expand Down

0 comments on commit bcc4164

Please sign in to comment.