-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): generate shell completion for
--node
flag
Signed-off-by: budimanjojo <budimanjojo@gmail.com>
- Loading branch information
1 parent
a42fbf9
commit 60f15f8
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |