Skip to content

Commit

Permalink
Support overriding context via the --context flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmicansaglam committed Apr 23, 2024
1 parent d91f551 commit b914f64
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func init() {
initUsernameFlag()
initPasswordFlag()

rootCmd.PersistentFlags().StringVar(&shared.Context, "context", "", "Override context")
rootCmd.PersistentFlags().BoolVar(&shared.Debug, "debug", false, "Enable debug mode")

rootCmd.AddCommand(config.Cmd())
Expand All @@ -58,13 +59,20 @@ func readContextFromConfig(conf config.Config) {
fmt.Println("Error: No contexts defined in the configuration.")
os.Exit(1)
}
if conf.CurrentContext == "" {
conf.CurrentContext = conf.Contexts[0].Name

var context string

if shared.Context != "" {
context = shared.Context
} else if conf.CurrentContext != "" {
context = conf.CurrentContext
} else {
context = conf.Contexts[0].Name
}

clusterFound := false
for _, cluster := range conf.Contexts {
if cluster.Name == conf.CurrentContext {
if cluster.Name == context {
shared.ElasticsearchProtocol = cluster.Protocol
if shared.ElasticsearchProtocol == "" {
shared.ElasticsearchProtocol = constants.DefaultElasticsearchProtocol
Expand Down
1 change: 1 addition & 0 deletions shared/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shared

var (
Context string
ElasticsearchProtocol string
ElasticsearchUsername string
ElasticsearchPassword string
Expand Down

0 comments on commit b914f64

Please sign in to comment.