Skip to content

Commit

Permalink
feat(cmd): generate shell completion for --node flag
Browse files Browse the repository at this point in the history
Signed-off-by: budimanjojo <budimanjojo@gmail.com>
  • Loading branch information
budimanjojo committed Sep 25, 2024
1 parent a42fbf9 commit 60f15f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/gencommand.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/budimanjojo/talhelper/v3/cmd/helpers"
"github.com/spf13/cobra"
)

Expand All @@ -24,4 +25,5 @@ func init() {
gencommandCmd.PersistentFlags().StringSliceVar(&gencommandEnvFile, "env-file", []string{"talenv.yaml", "talenv.sops.yaml", "talenv.yml", "talenv.sops.yml"}, "List of files containing env variables for config file")
gencommandCmd.PersistentFlags().StringSliceVar(&gencommandExtraFlags, "extra-flags", []string{}, "List of additional flags that will be injected into the generated commands.")
gencommandCmd.PersistentFlags().StringVarP(&gencommandNode, "node", "n", "", "A specific node to generate the command for. If not specified, will generate for all nodes.")
_ = helpers.MakeNodeCompletion(gencommandCmd)
}
3 changes: 3 additions & 0 deletions cmd/genurl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/budimanjojo/talhelper/v3/cmd/helpers"
"github.com/budimanjojo/talhelper/v3/pkg/config"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -34,4 +35,6 @@ func init() {
genurlCmd.PersistentFlags().StringSliceVarP(&genurlKernelArgs, "kernel-arg", "k", []string{}, "Kernel arguments to be passed to the image kernel (ignored when talconfig.yaml is found)")
genurlCmd.PersistentFlags().BoolVar(&genurlOfflineMode, "offline-mode", false, "Generate schematic ID without doing POST request to image-factory")
genurlCmd.PersistentFlags().BoolVar(&genurlSecureboot, "secure-boot", false, "Whether to generate Secure Boot enabled URL")

_ = helpers.MakeNodeCompletion(genurlCmd)
}
27 changes: 27 additions & 0 deletions cmd/helpers/completions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package helpers

import (
"github.com/budimanjojo/talhelper/v3/pkg/config"
"github.com/spf13/cobra"
)

// MakeNodeCompletion is a wrapper for `cobra.Command.RegisterFlagCompletionFunc`
// to reuse in commands that want to have `--node` flag completion
func MakeNodeCompletion(cmd *cobra.Command) error {
return cmd.RegisterFlagCompletionFunc("node", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
var nodes []string
thCfg, _ := cmd.Flags().GetString("config-file")
thEnvFiles, _ := cmd.Flags().GetStringSlice("env-file")

cfg, err := config.LoadAndValidateFromFile(thCfg, thEnvFiles, false)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

for i := range cfg.Nodes {
nodes = append(nodes, cfg.Nodes[i].Hostname)
nodes = append(nodes, cfg.Nodes[i].IPAddress)
}
return nodes, cobra.ShellCompDirectiveNoFileComp
})
}

0 comments on commit 60f15f8

Please sign in to comment.