Skip to content

Commit

Permalink
helm: support plugin completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Apr 12, 2023
1 parent 97455e4 commit 9a4e485
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 75 deletions.
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var completionCmd = &cobra.Command{
Use: "completion",
Short: "generate autocompletion scripts for the specified shell",
Run: func(cmd *cobra.Command, args []string) {},
Use: "completion",
Short: "generate autocompletion scripts for the specified shell",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var createCmd = &cobra.Command{
Use: "create",
Short: "create a new chart with the given name",
Run: func(cmd *cobra.Command, args []string) {},
Use: "create",
Short: "create a new chart with the given name",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var dependencyCmd = &cobra.Command{
Use: "dependency",
Short: "manage a chart's dependencies",
Run: func(cmd *cobra.Command, args []string) {},
Use: "dependency",
Short: "manage a chart's dependencies",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var diffCmd = &cobra.Command{
Use: "diff",
Short: "Preview helm upgrade changes as a diff",
Run: func(cmd *cobra.Command, args []string) {},
Use: "diff",
Short: "Preview helm upgrade changes as a diff",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var envCmd = &cobra.Command{
Use: "env",
Short: "helm client environment information",
Run: func(cmd *cobra.Command, args []string) {},
Use: "env",
Short: "helm client environment information",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var getCmd = &cobra.Command{
Use: "get",
Short: "download extended information of a named release",
Run: func(cmd *cobra.Command, args []string) {},
Use: "get",
Short: "download extended information of a named release",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var helpCmd = &cobra.Command{
Use: "help",
Short: "Help about any command",
Run: func(cmd *cobra.Command, args []string) {},
Use: "help",
Short: "Help about any command",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var historyCmd = &cobra.Command{
Use: "history",
Short: "fetch release history",
Run: func(cmd *cobra.Command, args []string) {},
Use: "history",
Short: "fetch release history",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var installCmd = &cobra.Command{
Use: "install",
Short: "install a chart",
Run: func(cmd *cobra.Command, args []string) {},
Use: "install",
Short: "install a chart",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var lintCmd = &cobra.Command{
Use: "lint",
Short: "examine a chart for possible issues",
Run: func(cmd *cobra.Command, args []string) {},
Use: "lint",
Short: "examine a chart for possible issues",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var listCmd = &cobra.Command{
Use: "list",
Short: "list releases",
Run: func(cmd *cobra.Command, args []string) {},
Use: "list",
Short: "list releases",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var packageCmd = &cobra.Command{
Use: "package",
Short: "package a chart directory into a chart archive",
Run: func(cmd *cobra.Command, args []string) {},
Use: "package",
Short: "package a chart directory into a chart archive",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var pluginCmd = &cobra.Command{
Use: "plugin",
Short: "install, list, or uninstall Helm plugins",
Run: func(cmd *cobra.Command, args []string) {},
Use: "plugin",
Short: "install, list, or uninstall Helm plugins",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var pullCmd = &cobra.Command{
Use: "pull",
Short: "download a chart from a repository and (optionally) unpack it in local directory",
Run: func(cmd *cobra.Command, args []string) {},
Use: "pull",
Short: "download a chart from a repository and (optionally) unpack it in local directory",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var repoCmd = &cobra.Command{
Use: "repo",
Short: "add, list, remove, update, and index chart repositories",
Run: func(cmd *cobra.Command, args []string) {},
Use: "repo",
Short: "add, list, remove, update, and index chart repositories",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var rollbackCmd = &cobra.Command{
Use: "rollback",
Short: "roll back a release to a previous revision",
Run: func(cmd *cobra.Command, args []string) {},
Use: "rollback",
Short: "roll back a release to a previous revision",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
43 changes: 43 additions & 0 deletions completers/helm_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/spf13/cobra"
)

Expand All @@ -20,6 +23,11 @@ func Execute() error {
func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.AddGroup(
&cobra.Group{ID: "main", Title: "Main Commands"},
&cobra.Group{ID: "plugin", Title: "Plugin Commands"},
)

rootCmd.PersistentFlags().Bool("add-dir-header", false, "If true, adds the file directory to the header of the log messages")
rootCmd.PersistentFlags().Bool("alsologtostderr", false, "log to standard error as well as files")
rootCmd.PersistentFlags().Bool("debug", false, "enable verbose output")
Expand Down Expand Up @@ -59,4 +67,39 @@ func init() {
"registry-config": carapace.ActionFiles(),
"repository-cache": carapace.ActionFiles(),
})

carapace.Gen(rootCmd).PreRun(func(cmd *cobra.Command, args []string) {
if _, _, err := cmd.Find(args); len(args) > 1 && err == nil {
return // core command - skip plugin commands
}
addPluginCommands()
})
}

func addPluginCommands() {
if output, err := (carapace.Context{}).Command("helm", "plugin", "list").Output(); err == nil {
lines := strings.Split(string(output), "\n")

if len(lines) < 2 {
return
}

for _, line := range lines[1 : len(lines)-1] {
if splitted := strings.SplitN(line, "\t", 3); len(splitted) == 3 {
pluginCmd := &cobra.Command{
Use: splitted[0],
Short: splitted[2],
Run: func(cmd *cobra.Command, args []string) {},
GroupID: "plugin",
DisableFlagParsing: true,
}

carapace.Gen(pluginCmd).PositionalAnyCompletion(
bridge.ActionCarapaceBin("helm-" + splitted[0]),
)

rootCmd.AddCommand(pluginCmd)
}
}
}
}
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var searchCmd = &cobra.Command{
Use: "search",
Short: "search for a keyword in charts",
Run: func(cmd *cobra.Command, args []string) {},
Use: "search",
Short: "search for a keyword in charts",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var showCmd = &cobra.Command{
Use: "show",
Short: "show information of a chart",
Run: func(cmd *cobra.Command, args []string) {},
Use: "show",
Short: "show information of a chart",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

var statusCmd = &cobra.Command{
Use: "status",
Short: "display the status of the named release",
Run: func(cmd *cobra.Command, args []string) {},
Use: "status",
Short: "display the status of the named release",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

var templateCmd = &cobra.Command{
Use: "template",
Short: "locally render templates",
Run: func(cmd *cobra.Command, args []string) {},
Use: "template",
Short: "locally render templates",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var testCmd = &cobra.Command{
Use: "test",
Short: "run tests for a release",
Run: func(cmd *cobra.Command, args []string) {},
Use: "test",
Short: "run tests for a release",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "uninstall a release",
Run: func(cmd *cobra.Command, args []string) {},
Use: "uninstall",
Short: "uninstall a release",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "upgrade a release",
Run: func(cmd *cobra.Command, args []string) {},
Use: "upgrade",
Short: "upgrade a release",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
7 changes: 4 additions & 3 deletions completers/helm_completer/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

var verifyCmd = &cobra.Command{
Use: "verify",
Short: "verify that a chart at the given path has been signed and is valid",
Run: func(cmd *cobra.Command, args []string) {},
Use: "verify",
Short: "verify that a chart at the given path has been signed and is valid",
GroupID: "main",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
Expand Down
Loading

0 comments on commit 9a4e485

Please sign in to comment.