From dfd39ae663d81f1305a6646e5ef66ece118a2593 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Thu, 17 Mar 2022 12:42:34 +0100 Subject: [PATCH] shell completion for wwctl voerlay build --- internal/app/wwctl/overlay/build/root.go | 27 +++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/app/wwctl/overlay/build/root.go b/internal/app/wwctl/overlay/build/root.go index 8560670f..7437f52b 100644 --- a/internal/app/wwctl/overlay/build/root.go +++ b/internal/app/wwctl/overlay/build/root.go @@ -1,6 +1,12 @@ package build -import "github.com/spf13/cobra" +import ( + "log" + + "github.com/hpcng/warewulf/internal/pkg/node" + "github.com/hpcng/warewulf/internal/pkg/overlay" + "github.com/spf13/cobra" +) var ( baseCmd = &cobra.Command{ @@ -9,6 +15,19 @@ var ( Short: "(Re)build node overlays", Long: "This command builds overlays for given nodes.", RunE: CobraRunE, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + + nodeDB, _ := node.New() + nodes, _ := nodeDB.FindAllNodes() + var node_names []string + for _, node := range nodes { + node_names = append(node_names, node.Id.Get()) + } + return node_names, cobra.ShellCompDirectiveNoFileComp + }, } BuildHost bool BuildNodes bool @@ -20,6 +39,12 @@ func init() { baseCmd.PersistentFlags().BoolVarP(&BuildHost, "host", "H", false, "Build overlays only for the host") baseCmd.PersistentFlags().BoolVarP(&BuildNodes, "nodes", "N", false, "Build overlays only for the nodes") baseCmd.PersistentFlags().StringVarP(&OverlayName, "overlay", "O", "", "Build only specific overlay") + if err := baseCmd.RegisterFlagCompletionFunc("overlay", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }); err != nil { + log.Println(err) + } baseCmd.PersistentFlags().StringVarP(&OverlayDir, "output", "o", "", `Do not create an overlay image, for distribution but write to the given directory. An overlay must also be ge given to use this option.`)