From 60872bd42ed9e7b8bdb95e1c0ca78895ab3457a0 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 5 Feb 2025 21:48:07 +0100 Subject: [PATCH 1/2] Add completion for profile list - Closes: #1696 Signed-off-by: Christian Goll --- CHANGELOG.md | 1 + internal/app/wwctl/profile/list/root.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b2e98b..88cac6d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Document defining kernel args that include commas. #1679 - Recommend installing ipmitool with Warewulf package. #970 +- Add completion for profile list. #1695 ### Changed diff --git a/internal/app/wwctl/profile/list/root.go b/internal/app/wwctl/profile/list/root.go index e0317b28..ff63b599 100644 --- a/internal/app/wwctl/profile/list/root.go +++ b/internal/app/wwctl/profile/list/root.go @@ -1,6 +1,9 @@ package list -import "github.com/spf13/cobra" +import ( + "github.com/spf13/cobra" + "github.com/warewulf/warewulf/internal/pkg/node" +) type variables struct { showAll bool @@ -18,6 +21,13 @@ func GetCommand() *cobra.Command { Long: "This command will display configurations for PROFILE.", RunE: CobraRunE(&vars), Aliases: []string{"ls"}, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + nodeDB, _ := node.New() + return nodeDB.ListAllProfiles(), cobra.ShellCompDirectiveNoFileComp + }, } 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") From b05e384560225758dc3c1a8ad730f47b9606de61 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Wed, 5 Feb 2025 18:56:16 -0700 Subject: [PATCH 2/2] Refactor to completions.Profiles Signed-off-by: Jonathon Anderson --- internal/app/wwctl/completions/completions.go | 8 ++++++++ internal/app/wwctl/profile/delete/root.go | 10 ++-------- internal/app/wwctl/profile/list/root.go | 10 ++-------- internal/app/wwctl/profile/set/root.go | 16 ++++------------ 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/internal/app/wwctl/completions/completions.go b/internal/app/wwctl/completions/completions.go index 2dfbb2fa..19adb05f 100644 --- a/internal/app/wwctl/completions/completions.go +++ b/internal/app/wwctl/completions/completions.go @@ -52,3 +52,11 @@ func Images(cmd *cobra.Command, args []string, toComplete string) ([]string, cob sources, _ := image.ListSources() return sources, cobra.ShellCompDirectiveNoFileComp } + +func Profiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + registry, _ := node.New() + return registry.ListAllProfiles(), cobra.ShellCompDirectiveNoFileComp +} diff --git a/internal/app/wwctl/profile/delete/root.go b/internal/app/wwctl/profile/delete/root.go index 7d30b979..6c6e0df4 100644 --- a/internal/app/wwctl/profile/delete/root.go +++ b/internal/app/wwctl/profile/delete/root.go @@ -2,7 +2,7 @@ package delete import ( "github.com/spf13/cobra" - "github.com/warewulf/warewulf/internal/pkg/node" + "github.com/warewulf/warewulf/internal/app/wwctl/completions" ) var ( @@ -14,13 +14,7 @@ var ( Aliases: []string{"remove", "rm", "del"}, RunE: CobraRunE, Args: cobra.MinimumNArgs(1), - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - if len(args) != 0 { - return nil, cobra.ShellCompDirectiveNoFileComp - } - nodeDB, _ := node.New() - return nodeDB.ListAllProfiles(), cobra.ShellCompDirectiveNoFileComp - }, + ValidArgsFunction: completions.Profiles, } SetYes bool ) diff --git a/internal/app/wwctl/profile/list/root.go b/internal/app/wwctl/profile/list/root.go index ff63b599..86468943 100644 --- a/internal/app/wwctl/profile/list/root.go +++ b/internal/app/wwctl/profile/list/root.go @@ -2,7 +2,7 @@ package list import ( "github.com/spf13/cobra" - "github.com/warewulf/warewulf/internal/pkg/node" + "github.com/warewulf/warewulf/internal/app/wwctl/completions" ) type variables struct { @@ -21,13 +21,7 @@ func GetCommand() *cobra.Command { Long: "This command will display configurations for PROFILE.", RunE: CobraRunE(&vars), Aliases: []string{"ls"}, - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - if len(args) != 0 { - return nil, cobra.ShellCompDirectiveNoFileComp - } - nodeDB, _ := node.New() - return nodeDB.ListAllProfiles(), cobra.ShellCompDirectiveNoFileComp - }, + 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") diff --git a/internal/app/wwctl/profile/set/root.go b/internal/app/wwctl/profile/set/root.go index 483b8901..36ed8f1c 100644 --- a/internal/app/wwctl/profile/set/root.go +++ b/internal/app/wwctl/profile/set/root.go @@ -36,18 +36,10 @@ func GetCommand() *cobra.Command { Short: "Configure node profile properties", Long: "This command sets configuration properties for the node PROFILE(s).\n\n" + "Note: use the string 'UNSET' to remove a configuration", - Aliases: []string{"modify"}, - Args: cobra.MinimumNArgs(0), - RunE: CobraRunE(&vars), - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - if len(args) != 0 { - return nil, cobra.ShellCompDirectiveNoFileComp - } - - nodeDB, _ := node.New() - profiles := nodeDB.ListAllProfiles() - return profiles, cobra.ShellCompDirectiveNoFileComp - }, + Aliases: []string{"modify"}, + Args: cobra.MinimumNArgs(0), + RunE: CobraRunE(&vars), + ValidArgsFunction: completions.Profiles, } vars.profileConf.CreateFlags(baseCmd) vars.profileDel.CreateDelFlags(baseCmd)