From 69c058f1060d042e6c420c41865e1cb002216e48 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 8 Jun 2022 15:49:55 +0200 Subject: [PATCH] added bash completion for overlay commands --- internal/app/wwctl/overlay/chmod/root.go | 8 ++++++++ internal/app/wwctl/overlay/chown/root.go | 12 +++++++++++- internal/app/wwctl/overlay/delete/root.go | 8 ++++++++ internal/app/wwctl/overlay/edit/root.go | 8 ++++++++ internal/app/wwctl/overlay/imprt/root.go | 12 +++++++++++- internal/app/wwctl/overlay/list/root.go | 8 ++++++++ internal/app/wwctl/overlay/show/root.go | 12 ++++++++++-- 7 files changed, 64 insertions(+), 4 deletions(-) diff --git a/internal/app/wwctl/overlay/chmod/root.go b/internal/app/wwctl/overlay/chmod/root.go index 44fabc95..b6f5f016 100644 --- a/internal/app/wwctl/overlay/chmod/root.go +++ b/internal/app/wwctl/overlay/chmod/root.go @@ -1,6 +1,7 @@ package chmod import ( + "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/spf13/cobra" ) @@ -13,6 +14,13 @@ var ( Example: "wwctl overlay chmod default /etc/hostname.ww 0660", RunE: CobraRunE, Args: cobra.ExactArgs(3), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } ) diff --git a/internal/app/wwctl/overlay/chown/root.go b/internal/app/wwctl/overlay/chown/root.go index 31b2e40d..fc8fe5c3 100644 --- a/internal/app/wwctl/overlay/chown/root.go +++ b/internal/app/wwctl/overlay/chown/root.go @@ -1,6 +1,9 @@ package chown -import "github.com/spf13/cobra" +import ( + "github.com/hpcng/warewulf/internal/pkg/overlay" + "github.com/spf13/cobra" +) var ( baseCmd = &cobra.Command{ @@ -10,6 +13,13 @@ var ( Long: "This command changes the ownership of a FILE within the system or runtime OVERLAY_NAME\nto the user specified by UID. Optionally, it will also change group ownership to GID.", RunE: CobraRunE, Args: cobra.RangeArgs(3, 4), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } ) diff --git a/internal/app/wwctl/overlay/delete/root.go b/internal/app/wwctl/overlay/delete/root.go index 90ec39fe..ba2f75f5 100644 --- a/internal/app/wwctl/overlay/delete/root.go +++ b/internal/app/wwctl/overlay/delete/root.go @@ -1,6 +1,7 @@ package delete import ( + "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/spf13/cobra" ) @@ -13,6 +14,13 @@ var ( RunE: CobraRunE, Args: cobra.RangeArgs(1, 2), Aliases: []string{"rm", "del"}, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } Force bool Parents bool diff --git a/internal/app/wwctl/overlay/edit/root.go b/internal/app/wwctl/overlay/edit/root.go index b3d7c9f0..2e265a60 100644 --- a/internal/app/wwctl/overlay/edit/root.go +++ b/internal/app/wwctl/overlay/edit/root.go @@ -1,6 +1,7 @@ package edit import ( + "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/spf13/cobra" ) @@ -12,6 +13,13 @@ var ( Long: "This command will open the FILE for editing or create a new file within the\nOVERLAY_NAME. Note: files created with a '.ww' suffix will always be\nparsed as Warewulf template files, and the suffix will be removed automatically.", RunE: CobraRunE, Args: cobra.ExactArgs(2), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } ListFiles bool CreateDirs bool diff --git a/internal/app/wwctl/overlay/imprt/root.go b/internal/app/wwctl/overlay/imprt/root.go index 2d41343c..4dc89acb 100644 --- a/internal/app/wwctl/overlay/imprt/root.go +++ b/internal/app/wwctl/overlay/imprt/root.go @@ -1,6 +1,9 @@ package imprt -import "github.com/spf13/cobra" +import ( + "github.com/hpcng/warewulf/internal/pkg/overlay" + "github.com/spf13/cobra" +) var ( baseCmd = &cobra.Command{ @@ -11,6 +14,13 @@ var ( RunE: CobraRunE, Args: cobra.RangeArgs(2, 3), Aliases: []string{"cp"}, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } PermMode int32 NoOverlayUpdate bool diff --git a/internal/app/wwctl/overlay/list/root.go b/internal/app/wwctl/overlay/list/root.go index 0048d7f2..dbb9b46e 100644 --- a/internal/app/wwctl/overlay/list/root.go +++ b/internal/app/wwctl/overlay/list/root.go @@ -1,6 +1,7 @@ package list import ( + "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/spf13/cobra" ) @@ -14,6 +15,13 @@ var ( Args: cobra.MinimumNArgs(0), Aliases: []string{"ls"}, ValidArgs: []string{"system", "runtime"}, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } ListContents bool ListLong bool diff --git a/internal/app/wwctl/overlay/show/root.go b/internal/app/wwctl/overlay/show/root.go index aaa3961b..2dae0e77 100644 --- a/internal/app/wwctl/overlay/show/root.go +++ b/internal/app/wwctl/overlay/show/root.go @@ -4,6 +4,7 @@ import ( "log" "github.com/hpcng/warewulf/internal/pkg/node" + "github.com/hpcng/warewulf/internal/pkg/overlay" "github.com/spf13/cobra" ) @@ -16,14 +17,21 @@ var ( RunE: CobraRunE, Aliases: []string{"cat"}, Args: cobra.ExactArgs(2), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) != 0 { + return nil, cobra.ShellCompDirectiveNoFileComp + } + list, _ := overlay.FindOverlays() + return list, cobra.ShellCompDirectiveNoFileComp + }, } NodeName string Quiet bool ) func init() { - baseCmd.PersistentFlags().StringVarP(&NodeName, "name", "n", "", "node used for the variables in the template") - if err := baseCmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + baseCmd.PersistentFlags().StringVarP(&NodeName, "render", "r", "", "node used for the variables in the template") + if err := baseCmd.RegisterFlagCompletionFunc("render", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { nodeDB, _ := node.New() nodes, _ := nodeDB.FindAllNodes() var node_names []string