Simplify node, profile, and image completions

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-09 16:38:27 -07:00
parent d95f9c8c46
commit 3b963ee5a6
27 changed files with 54 additions and 56 deletions

View File

@@ -51,37 +51,25 @@ func ProfileKernelVersion(cmd *cobra.Command, args []string, toComplete string)
return kernelVersions, cobra.ShellCompDirectiveNoFileComp
}
func Images(num int) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if num <= 0 || len(args) < num {
func Images(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if sources, err := image.ListSources(); err != nil {
return sources, cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
}
func Nodes(num int) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if num <= 0 || len(args) < num {
func Nodes(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if registry, err := node.New(); err == nil {
return registry.ListAllNodes(), cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
}
func Profiles(num int) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if num <= 0 || len(args) < num {
func Profiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if registry, err := node.New(); err == nil {
return registry.ListAllProfiles(), cobra.ShellCompDirectiveNoFileComp
}
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
}
func Overlays(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

View File

@@ -14,7 +14,7 @@ var (
Long: "This command lists the kernels that are available in the imported images.",
RunE: CobraRunE,
Aliases: []string{"kernel"},
ValidArgsFunction: completions.Images(0), // no limit
ValidArgsFunction: completions.Images,
}
)

View File

@@ -23,7 +23,7 @@ func GetCommand() *cobra.Command {
Long: "This command will show you the images that are imported into Warewulf.",
RunE: CobraRunE(&vars),
Aliases: []string{"ls"},
ValidArgsFunction: completions.Images(0), // no limit
ValidArgsFunction: completions.Images,
}
baseCmd.PersistentFlags().BoolVarP(&vars.full, "long", "l", false, "show all")
baseCmd.PersistentFlags().BoolVarP(&vars.kernel, "kernel", "k", false, "show kernel version")

View File

@@ -13,7 +13,12 @@ var baseCmd = &cobra.Command{
Long: "This command will rename an existing image.",
RunE: CobraRunE,
Args: cobra.ExactArgs(2),
ValidArgsFunction: completions.Images(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completions.Images(cmd, args, toComplete)
}
return completions.None(cmd, args, toComplete)
},
}
var SetBuild bool

View File

@@ -13,8 +13,13 @@ var (
Long: `Shows the base directory for the chroot of the given image.
More information about the image can be shown with the '-a' option.`,
RunE: CobraRunE,
ValidArgsFunction: completions.Images(0), // no limit
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completions.Images(cmd, args, toComplete)
}
return completions.None(cmd, args, toComplete)
},
}
ShowAll bool
)

View File

@@ -31,7 +31,7 @@ func GetCommand() *cobra.Command {
vars.nodeAdd.CreateAddFlags(baseCmd)
flags.AddContainer(baseCmd, &(vars.nodeConf.Profile.ImageName))
// register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images(0)); err != nil { // no limit
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images); err != nil { // no limit
panic(err)
}
if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.NodeKernelVersion); err != nil {
@@ -43,7 +43,7 @@ func GetCommand() *cobra.Command {
if err := baseCmd.RegisterFlagCompletionFunc("wwinit", completions.OverlayList); err != nil {
panic(err)
}
if err := baseCmd.RegisterFlagCompletionFunc("profile", completions.Profiles(0)); err != nil { // no limit
if err := baseCmd.RegisterFlagCompletionFunc("profile", completions.Profiles); err != nil { // no limit
panic(err)
}

View File

@@ -13,7 +13,7 @@ var (
Long: "Start a new IPMI console for NODENAME.",
Args: cobra.MinimumNArgs(1),
RunE: CobraRunE,
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
)

View File

@@ -14,7 +14,7 @@ var (
Args: cobra.MinimumNArgs(1),
RunE: CobraRunE,
Aliases: []string{"rm", "del", "remove"},
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
SetYes bool
SetForce bool // no hash checking, so always using force

View File

@@ -12,7 +12,7 @@ var (
Short: "Edit node(s) with editor",
Long: "This command opens an editor for the given nodes.",
RunE: CobraRunE,
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
NoHeader bool
)

View File

@@ -12,7 +12,7 @@ var (
Short: "Export nodes as yaml to stdout",
Long: "This command exports the given nodes as yaml to stdout.",
RunE: CobraRunE,
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
NoHeader bool
)

View File

@@ -24,7 +24,7 @@ func GetCommand() *cobra.Command {
"nodes matching a PATTERN.",
RunE: CobraRunE(&vars),
Aliases: []string{"ls"},
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
baseCmd.PersistentFlags().BoolVarP(&vars.showNet, "net", "n", false, "Show node network configurations")
baseCmd.PersistentFlags().BoolVarP(&vars.showIpmi, "ipmi", "i", false, "Show node IPMI configurations")

View File

@@ -21,7 +21,7 @@ func GetCommand() *cobra.Command {
Long: "Show IPMI sensor information for nodes matching PATTERN.",
Args: cobra.MinimumNArgs(1),
RunE: CobraRunE(&vars),
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Full, "full", "F", false, "show detailed output.")
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")

View File

@@ -27,7 +27,7 @@ func GetCommand() *cobra.Command {
Aliases: []string{"modify"},
Args: cobra.MinimumNArgs(1), // require pattern as a mandatory arg
RunE: CobraRunE(&vars),
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
vars.nodeConf.CreateFlags(baseCmd)
@@ -38,7 +38,7 @@ func GetCommand() *cobra.Command {
baseCmd.PersistentFlags().BoolVarP(&vars.setYes, "yes", "y", false, "Set 'yes' to all questions asked")
baseCmd.PersistentFlags().BoolVarP(&vars.setForce, "force", "f", false, "Force configuration (even on error)")
// register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images(0)); err != nil { // no limit
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images); err != nil { // no limit
panic(err)
}
if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.NodeKernelVersion); err != nil {
@@ -50,7 +50,7 @@ func GetCommand() *cobra.Command {
if err := baseCmd.RegisterFlagCompletionFunc("wwinit", completions.OverlayList); err != nil {
panic(err)
}
if err := baseCmd.RegisterFlagCompletionFunc("profile", completions.Profiles(0)); err != nil { // no limit
if err := baseCmd.RegisterFlagCompletionFunc("profile", completions.Profiles); err != nil { // no limit
panic(err)
}

View File

@@ -12,7 +12,7 @@ var (
Short: "View the provisioning status of nodes",
Long: "View and monitor the status of nodes as they are provisioned and check in.",
RunE: CobraRunE,
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
SetWatch bool
SetUpdate int

View File

@@ -14,7 +14,7 @@ var (
Short: "(Re)build node overlays",
Long: "This command builds overlays for given nodes.",
RunE: CobraRunE,
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
OverlayNames []string
OverlayDir string

View File

@@ -20,7 +20,7 @@ func GetCommand() *cobra.Command {
Long: "This command cycles power for a set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Nodes(0), // no limit
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")

View File

@@ -20,7 +20,7 @@ func GetCommand() *cobra.Command {
Long: "This command will shutdown power to a set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Nodes(0), // no limit,
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")

View File

@@ -19,7 +19,7 @@ func GetCommand() *cobra.Command {
Long: "This command will power on a set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Nodes(0), // no limit,
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")

View File

@@ -20,7 +20,7 @@ func GetCommand() *cobra.Command {
Long: "This command will issue a reset to a set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Nodes(0), // no limit,
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")

View File

@@ -20,7 +20,7 @@ func GetCommand() *cobra.Command {
Long: "This command uses the operating system to shut down the set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Nodes(0), // no limit,
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")

View File

@@ -20,7 +20,7 @@ func GetCommand() *cobra.Command {
Long: "This command displays the power status of a set of nodes specified by PATTERN.",
RunE: CobraRunE(&vars),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Nodes(0), // no limit,
ValidArgsFunction: completions.Nodes,
}
powerCmd.PersistentFlags().BoolVarP(&vars.Showcmd, "show", "s", false, "only show command which will be executed")
powerCmd.PersistentFlags().IntVar(&vars.Fanout, "fanout", 50, "how many command should be executed in parallel")

View File

@@ -29,7 +29,7 @@ func GetCommand() *cobra.Command {
vars.profileAdd.CreateAddFlags(baseCmd)
flags.AddContainer(baseCmd, &(vars.profileConf.ImageName))
// register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images(0)); err != nil { // no limit
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images); err != nil { // no limit
panic(err)
}
if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.ProfileKernelVersion); err != nil {

View File

@@ -14,7 +14,7 @@ var (
Aliases: []string{"remove", "rm", "del"},
RunE: CobraRunE,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completions.Profiles(0), // no limit
ValidArgsFunction: completions.Profiles,
}
SetYes bool
)

View File

@@ -12,7 +12,7 @@ var (
Short: "Edit node(s) with editor",
Long: "This command opens an editor for the given profiles.",
RunE: CobraRunE,
ValidArgsFunction: completions.Profiles(0),
ValidArgsFunction: completions.Profiles,
}
NoHeader bool
)

View File

@@ -21,7 +21,7 @@ func GetCommand() *cobra.Command {
Long: "This command will display configurations for PROFILE.",
RunE: CobraRunE(&vars),
Aliases: []string{"ls"},
ValidArgsFunction: completions.Profiles(0), // no limit
ValidArgsFunction: completions.Profiles,
}
baseCmd.PersistentFlags().BoolVarP(&vars.showAll, "all", "a", false, "Show all profile configurations")
baseCmd.PersistentFlags().BoolVarP(&vars.showYaml, "yaml", "y", false, "Show profile configurations via yaml format")

View File

@@ -34,7 +34,7 @@ func GetCommand() *cobra.Command {
"Note: use the string 'UNSET' to remove a configuration",
Aliases: []string{"modify"},
RunE: CobraRunE(&vars),
ValidArgsFunction: completions.Profiles(0), // no limit
ValidArgsFunction: completions.Profiles,
}
vars.profileConf.CreateFlags(baseCmd)
vars.profileDel.CreateDelFlags(baseCmd)
@@ -42,7 +42,7 @@ func GetCommand() *cobra.Command {
flags.AddContainer(baseCmd, &(vars.profileConf.ImageName))
baseCmd.PersistentFlags().BoolVarP(&vars.setYes, "yes", "y", false, "Set 'yes' to all questions asked")
// register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images(0)); err != nil { // no limit
if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images); err != nil { // no limit
panic(err)
}
if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.ProfileKernelVersion); err != nil {

View File

@@ -15,7 +15,7 @@ var (
Args: cobra.MinimumNArgs(2),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completions.Nodes(1)(cmd, args, toComplete)
return completions.Nodes(cmd, args, toComplete)
}
return completions.None(cmd, args, toComplete)
},