Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --context flag #215

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- [#215](https://github.com/deviceinsight/kafkactl/pull/215) 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(_ *cobra.Command, _ []string, _ 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

Expand Down
14 changes: 14 additions & 0 deletions 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,6 +54,18 @@ func GetFlags() Flags {
}

func GetCurrentContext() string {
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
}

return configInstance.currentContext()
}

Expand Down
Loading