Skip to content

Commit

Permalink
add kube-context
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojan295 committed Feb 21, 2024
1 parent 8fb6138 commit 526e394
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 13 deletions.
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/get_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func init() {
getCmd.AddCommand(get_allCmd)

carapace.Gen(get_allCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/get_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func init() {
getCmd.AddCommand(get_hooksCmd)

carapace.Gen(get_hooksCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/get_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func init() {
getCmd.AddCommand(get_manifestCmd)

carapace.Gen(get_manifestCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/get_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func init() {
getCmd.AddCommand(get_notesCmd)

carapace.Gen(get_notesCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/get_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func init() {
})

carapace.Gen(get_valuesCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func init() {
})

carapace.Gen(historyCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func init() {
rootCmd.AddCommand(rollbackCmd)

carapace.Gen(rollbackCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionRevisions(c.Args[0])
}),
Expand Down
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func init() {
})

carapace.Gen(statusCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func init() {
rootCmd.AddCommand(testCmd)

carapace.Gen(testCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func init() {
rootCmd.AddCommand(uninstallCmd)

carapace.Gen(uninstallCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
)
}
7 changes: 6 additions & 1 deletion completers/helm_completer/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func init() {
})

carapace.Gen(upgradeCmd).PositionalCompletion(
helm.ActionReleases(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return helm.ActionReleases(helm.ReleasesOpts{
Namespace: rootCmd.Flag("namespace").Value.String(),
KubeContext: rootCmd.Flag("kube-context").Value.String(),
})
}),
carapace.Batch(
carapace.ActionFiles(),
helm.ActionRepositoryCharts().Unless(condition.CompletingPath),
Expand Down
18 changes: 16 additions & 2 deletions pkg/actions/tools/helm/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,23 @@ type release struct {
AppVersion string `json:"app_version"`
}

type ReleasesOpts struct {
KubeContext string
Namespace string
}

// ActionReleases completes releases
func ActionReleases() carapace.Action {
return carapace.ActionExecCommand("helm", "list", "--output", "json")(func(output []byte) carapace.Action {
func ActionReleases(opts ReleasesOpts) carapace.Action {
args := []string{"list", "--output", "json"}
if opts.KubeContext != "" {
args = append(args, "--kube-context", opts.KubeContext)
}

if opts.Namespace != "" {
args = append(args, "--namespace", opts.Namespace)
}

return carapace.ActionExecCommand("helm", args...)(func(output []byte) carapace.Action {
var releases []release
if err := json.Unmarshal(output, &releases); err != nil {
return carapace.ActionMessage(err.Error())
Expand Down

0 comments on commit 526e394

Please sign in to comment.