wwctl profile args and completions

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-09 13:42:16 -07:00
parent 32b9696b71
commit 3fbc2fc75c
6 changed files with 20 additions and 61 deletions

View File

@@ -1,14 +1,10 @@
package add package add
import ( import (
"log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/app/wwctl/completions" "github.com/warewulf/warewulf/internal/app/wwctl/completions"
"github.com/warewulf/warewulf/internal/app/wwctl/flags" "github.com/warewulf/warewulf/internal/app/wwctl/flags"
"github.com/warewulf/warewulf/internal/pkg/image"
"github.com/warewulf/warewulf/internal/pkg/node" "github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
) )
type variables struct { type variables struct {
@@ -27,32 +23,23 @@ func GetCommand() *cobra.Command {
Long: "This command adds a new named PROFILE.", Long: "This command adds a new named PROFILE.",
Aliases: []string{"new", "create"}, Aliases: []string{"new", "create"},
RunE: CobraRunE(&vars), RunE: CobraRunE(&vars),
Args: cobra.ExactArgs(1), ValidArgsFunction: completions.None,
} }
vars.profileConf.CreateFlags(baseCmd) vars.profileConf.CreateFlags(baseCmd)
vars.profileAdd.CreateAddFlags(baseCmd) vars.profileAdd.CreateAddFlags(baseCmd)
flags.AddContainer(baseCmd, &(vars.profileConf.ImageName)) flags.AddContainer(baseCmd, &(vars.profileConf.ImageName))
// register the command line completions // register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("image", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images(0)); err != nil { // no limit
list, _ := image.ListSources() panic(err)
return list, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
log.Println(err)
} }
if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.ProfileKernelVersion); err != nil { if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.ProfileKernelVersion); err != nil {
log.Println(err) panic(err)
} }
if err := baseCmd.RegisterFlagCompletionFunc("runtime", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := baseCmd.RegisterFlagCompletionFunc("runtime", completions.Overlays); err != nil {
list := overlay.FindOverlays() panic(err)
return list, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
log.Println(err)
} }
if err := baseCmd.RegisterFlagCompletionFunc("wwinit", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := baseCmd.RegisterFlagCompletionFunc("wwinit", completions.Overlays); err != nil {
list := overlay.FindOverlays() panic(err)
return list, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
log.Println(err)
} }
return baseCmd return baseCmd
} }

View File

@@ -37,9 +37,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if np == r { if np == r {
wwlog.Verbose("Removing profile from node %s: %s", n.Id(), r) wwlog.Verbose("Removing profile from node %s: %s", n.Id(), r)
n.Profiles = append(n.Profiles[:i], n.Profiles[i+1:]...) n.Profiles = append(n.Profiles[:i], n.Profiles[i+1:]...)
if err != nil {
return fmt.Errorf("failed to update node: %w", err)
}
} }
} }
} }

View File

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

View File

@@ -2,7 +2,7 @@ package edit
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/node" "github.com/warewulf/warewulf/internal/app/wwctl/completions"
) )
var ( var (
@@ -12,18 +12,7 @@ var (
Short: "Edit node(s) with editor", Short: "Edit node(s) with editor",
Long: "This command opens an editor for the given profiles.", Long: "This command opens an editor for the given profiles.",
RunE: CobraRunE, RunE: CobraRunE,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: completions.Profiles(0),
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
nodeDB, _ := node.New()
profiles, _ := nodeDB.FindAllProfiles()
var p_names []string
for _, profile := range profiles {
p_names = append(p_names, profile.Id())
}
return p_names, cobra.ShellCompDirectiveNoFileComp
},
} }
NoHeader bool NoHeader bool
) )

View File

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

View File

@@ -1,14 +1,10 @@
package set package set
import ( import (
"log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/app/wwctl/completions" "github.com/warewulf/warewulf/internal/app/wwctl/completions"
"github.com/warewulf/warewulf/internal/app/wwctl/flags" "github.com/warewulf/warewulf/internal/app/wwctl/flags"
"github.com/warewulf/warewulf/internal/pkg/image"
"github.com/warewulf/warewulf/internal/pkg/node" "github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
) )
type variables struct { type variables struct {
@@ -37,9 +33,8 @@ func GetCommand() *cobra.Command {
Long: "This command sets configuration properties for the node PROFILE(s).\n\n" + Long: "This command sets configuration properties for the node PROFILE(s).\n\n" +
"Note: use the string 'UNSET' to remove a configuration", "Note: use the string 'UNSET' to remove a configuration",
Aliases: []string{"modify"}, Aliases: []string{"modify"},
Args: cobra.MinimumNArgs(0),
RunE: CobraRunE(&vars), RunE: CobraRunE(&vars),
ValidArgsFunction: completions.Profiles, ValidArgsFunction: completions.Profiles(0), // no limit
} }
vars.profileConf.CreateFlags(baseCmd) vars.profileConf.CreateFlags(baseCmd)
vars.profileDel.CreateDelFlags(baseCmd) vars.profileDel.CreateDelFlags(baseCmd)
@@ -47,26 +42,17 @@ func GetCommand() *cobra.Command {
flags.AddContainer(baseCmd, &(vars.profileConf.ImageName)) flags.AddContainer(baseCmd, &(vars.profileConf.ImageName))
baseCmd.PersistentFlags().BoolVarP(&vars.setYes, "yes", "y", false, "Set 'yes' to all questions asked") baseCmd.PersistentFlags().BoolVarP(&vars.setYes, "yes", "y", false, "Set 'yes' to all questions asked")
// register the command line completions // register the command line completions
if err := baseCmd.RegisterFlagCompletionFunc("image", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := baseCmd.RegisterFlagCompletionFunc("image", completions.Images(0)); err != nil { // no limit
list, _ := image.ListSources() panic(err)
return list, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
log.Println(err)
} }
if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.ProfileKernelVersion); err != nil { if err := baseCmd.RegisterFlagCompletionFunc("kernelversion", completions.ProfileKernelVersion); err != nil {
log.Println(err) panic(err)
} }
if err := baseCmd.RegisterFlagCompletionFunc("runtime", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := baseCmd.RegisterFlagCompletionFunc("runtime", completions.Overlays); err != nil {
list := overlay.FindOverlays() panic(err)
return list, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
log.Println(err)
} }
if err := baseCmd.RegisterFlagCompletionFunc("wwinit", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := baseCmd.RegisterFlagCompletionFunc("wwinit", completions.Overlays); err != nil {
list := overlay.FindOverlays() panic(err)
return list, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
log.Println(err)
} }
return baseCmd return baseCmd
} }