Skip to content

Commit

Permalink
Merge pull request #2258 from Trojan295/feat/helm/add-namespace-and-c…
Browse files Browse the repository at this point in the history
…ontext-completions

helm, kubectl: add namespace and kube-context completions
  • Loading branch information
rsteube authored Feb 21, 2024
2 parents f038a18 + 0c8b774 commit 62e1d9e
Show file tree
Hide file tree
Showing 39 changed files with 147 additions and 39 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
13 changes: 8 additions & 5 deletions completers/helm_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/rsteube/carapace-bin/pkg/actions/tools/kubectl"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -59,11 +60,13 @@ func init() {
"kube-as-group": os.ActionGroups(),
"kube-as-user": os.ActionUsers(),
"kube-ca-file": carapace.ActionFiles(),
// TODO "kube-context"
"kubeconfig": carapace.ActionFiles(),
"log-dir": carapace.ActionDirectories(),
"log-file": carapace.ActionFiles(),
// TODO namespace
"kube-context": kubectl.ActionContexts(),
"kubeconfig": carapace.ActionFiles(),
"log-dir": carapace.ActionDirectories(),
"log-file": carapace.ActionFiles(),
"namespace": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{Types: "namespaces", Context: rootCmd.Flag("kube-context").Value.String()})
}),
"registry-config": carapace.ActionFiles(),
"repository-cache": carapace.ActionFiles(),
})
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
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {
carapace.ActionValues("deployments", "replicasets", "replicationcontrollers"),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/clusterInfo_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func init() {
if !clusterInfoCmd.Flag("all-namespaces").Changed {
namespace = rootCmd.Flag("namespace").Value.String()
}
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: namespace, Types: "namespaces"})
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: namespace,
Types: "namespaces",
})
}),
"output": kubectl.ActionOutputFormats(),
"output-directory": carapace.ActionDirectories(),
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
carapace.Gen(cordonCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
12 changes: 10 additions & 2 deletions completers/kubectl_completer/cmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ func ActionPathOrContainer() carapace.Action {
carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: "", Types: "namespaces"}).Invoke(c).Suffix("/").ToA()
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: "",
Types: "namespaces",
}).Invoke(c).Suffix("/").ToA()
case 1:
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: c.Parts[0], Types: "pods"}).Invoke(c).Suffix(":").ToA()
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: c.Parts[0],
Types: "pods",
}).Invoke(c).Suffix(":").ToA()
default:
return carapace.ActionValues()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
carapace.Gen(create_clusterrolebindingCmd).FlagCompletion(carapace.ActionMap{
"clusterrole": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "clusterrole",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/create_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
"dry-run": kubectl.ActionDryRunModes(),
"from": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "cronjobs",
})
Expand Down
2 changes: 2 additions & 0 deletions completers/kubectl_completer/cmd/create_rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
carapace.Gen(create_rolebindingCmd).FlagCompletion(carapace.ActionMap{
"clusterrole": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "clusterroles",
})
Expand All @@ -43,6 +44,7 @@ func init() {
"output": kubectl.ActionOutputFormats(),
"role": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "roles",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
kubectl.ActionApiResources().UniqueList(","),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
10 changes: 5 additions & 5 deletions completers/kubectl_completer/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func init() {

carapace.Gen(describeCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var namespace string
if !describeCmd.Flag("all-namespaces").Changed {
namespace = rootCmd.Flag("namespace").Value.String()
}
return kubectl.ActionResources(kubectl.ResourceOpts{Namespace: namespace, Types: c.Args[0]}).Filter(c.Args[1:]...)
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
}).Filter(c.Args[1:]...)
}),
)
}
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
carapace.Gen(drainCmd).PositionalCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "nodes",
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func init() {
carapace.ActionValues("pod", "service", "replicationcontroller", "deployment", "replicaset"),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func init() {
carapace.Gen(getCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
}).Filter(c.Args[1:]...)
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
12 changes: 9 additions & 3 deletions completers/kubectl_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ func init() {
"certificate-authority": carapace.ActionFiles(),
"client-certificate": carapace.ActionFiles(),
"client-key": carapace.ActionFiles(),
"context": kubectl.ActionContexts(),
"kubeconfig": carapace.ActionFiles(),
"namespace": kubectl.ActionResources(kubectl.ResourceOpts{Namespace: "", Types: "namespaces"}),
"profile": carapace.ActionValues("none", "cpu", "heap", "goroutine", "threadcreate", "block", "mutex"),
"profile-output": carapace.ActionFiles(),
"namespace": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Types: "namespaces",
})
}),
"profile": carapace.ActionValues("none", "cpu", "heap", "goroutine", "threadcreate", "block", "mutex"),
"profile-output": carapace.ActionFiles(),
// TODO add completions for kubeconfig based flags
})

Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
return carapace.ActionValues("deployments/", "replicasets/", "replicationcontrollers/").NoSpace()
case 1:
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Parts[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/set_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {
carapace.ActionValues("pod", "service", "replicationcontroller", "deployment", "replicaset").UniqueList(","),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
1 change: 1 addition & 0 deletions completers/kubectl_completer/cmd/set_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand Down
3 changes: 3 additions & 0 deletions completers/kubectl_completer/cmd/set_serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if set_serviceaccountCmd.Flag("filename").Changed {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "serviceaccounts",
})
Expand All @@ -56,6 +57,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: c.Args[0],
})
Expand All @@ -66,6 +68,7 @@ func init() {
return carapace.ActionValues()
} else {
return kubectl.ActionResources(kubectl.ResourceOpts{
Context: rootCmd.Flag("context").Value.String(),
Namespace: rootCmd.Flag("namespace").Value.String(),
Types: "serviceaccounts",
})
Expand Down
Loading

0 comments on commit 62e1d9e

Please sign in to comment.