wwctl overlay args and completions

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-09 08:39:09 -07:00
parent 08a8eb430c
commit eff5565886
15 changed files with 82 additions and 89 deletions

View File

@@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Set execute permissions for intermediate directories during `wwctl overlay import --parents`. #1655
- Fix log output formatting during overlay build.
- Prevent merging of zero-value net.IP fields. #1710
- Fixed detection of overlay files in `wwctl overlay list --long`
### Removed

View File

@@ -86,3 +86,24 @@ func Overlays(cmd *cobra.Command, args []string, toComplete string) ([]string, c
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
}
func OverlayFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ret, _ := overlay.OverlayGetFiles(args[0])
return ret, cobra.ShellCompDirectiveNoFileComp
}
func OverlayAndFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return Overlays(cmd, args, toComplete)
} else {
return OverlayFiles(cmd, args, toComplete)
}
}
func None(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}
func LocalFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveDefault
}

View File

@@ -1,12 +1,10 @@
package build
import (
"log"
"runtime"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -16,19 +14,7 @@ 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())
}
return node_names, cobra.ShellCompDirectiveNoFileComp
},
ValidArgsFunction: completions.Nodes(0), // no limit
}
OverlayNames []string
OverlayDir string
@@ -38,11 +24,8 @@ var (
func init() {
baseCmd.PersistentFlags().StringSliceVarP(&OverlayNames, "overlay", "O", []string{}, "Build only specific overlay(s)")
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)
if err := baseCmd.RegisterFlagCompletionFunc("overlay", completions.Overlays); err != nil {
panic(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.`)

View File

@@ -2,7 +2,7 @@ package chmod
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -15,16 +15,11 @@ var (
RunE: CobraRunE,
Args: cobra.ExactArgs(3),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
} else if len(args) == 1 {
ret, err := overlay.OverlayGetFiles(args[0])
if err == nil {
return ret, cobra.ShellCompDirectiveNoFileComp
}
if len(args) < 2 {
return completions.OverlayAndFiles(cmd, args, toComplete)
} else {
return completions.None(cmd, args, toComplete)
}
return []string{""}, cobra.ShellCompDirectiveNoFileComp
},
}
)

View File

@@ -2,7 +2,7 @@ package chown
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -14,16 +14,11 @@ var (
RunE: CobraRunE,
Args: cobra.RangeArgs(3, 4),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
} else if len(args) == 1 {
ret, err := overlay.OverlayGetFiles(args[0])
if err == nil {
return ret, cobra.ShellCompDirectiveNoFileComp
}
if len(args) < 2 {
return completions.OverlayAndFiles(cmd, args, toComplete)
} else {
return completions.None(cmd, args, toComplete)
}
return []string{""}, cobra.ShellCompDirectiveNoFileComp
},
}
)

View File

@@ -2,6 +2,7 @@ package create
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -13,6 +14,7 @@ var (
Long: "This command creates a new empty overlay with the given OVERLAY_NAME.",
RunE: CobraRunE,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completions.None,
}
)

View File

@@ -2,7 +2,7 @@ package delete
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -15,16 +15,10 @@ var (
Args: cobra.RangeArgs(1, 2),
Aliases: []string{"rm", "remove", "del"},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
} else if len(args) == 1 {
ret, err := overlay.OverlayGetFiles(args[0])
if err == nil {
return ret, cobra.ShellCompDirectiveNoFileComp
}
if len(args) < 2 {
return completions.OverlayAndFiles(cmd, args, toComplete)
}
return []string{""}, cobra.ShellCompDirectiveNoFileComp
return completions.None(cmd, args, toComplete)
},
}
Force bool

View File

@@ -2,7 +2,7 @@ package edit
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -14,16 +14,10 @@ var (
RunE: CobraRunE,
Args: cobra.ExactArgs(2),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
} else if len(args) == 1 {
ret, err := overlay.OverlayGetFiles(args[0])
if err == nil {
return ret, cobra.ShellCompDirectiveNoFileComp
}
if len(args) < 2 {
return completions.OverlayAndFiles(cmd, args, toComplete)
}
return []string{""}, cobra.ShellCompDirectiveNoFileComp
return completions.None(cmd, args, toComplete)
},
}
CreateDirs bool

View File

@@ -4,7 +4,7 @@ import (
"runtime"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -17,11 +17,13 @@ var (
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
if len(args) == 0 {
return completions.Overlays(cmd, args, toComplete)
} else if len(args) == 1 {
return completions.LocalFiles(cmd, args, toComplete)
} else {
return completions.None(cmd, args, toComplete)
}
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
},
}
NoOverlayUpdate bool

View File

@@ -22,7 +22,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
t := table.New(cmd.OutOrStdout())
if ListLong {
t.AddHeader("PERM MODE", "UID", "GID", "SYSTEM-OVERLAY", "FILE PATH", "SITE")
t.AddHeader("PERM MODE", "UID", "GID", "OVERLAY", "FILE PATH", "SITE")
} else {
t.AddHeader("OVERLAY NAME", "FILES/DIRS", "SITE")
}
@@ -40,8 +40,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Debug("Iterating overlay rootfs: %s", overlay_.Rootfs())
if ListLong {
for file := range files {
s, err := os.Stat(files[file])
s, err := os.Stat(overlay_.File(files[file]))
if err != nil {
wwlog.Warn("%s: %s: %s", name, files[file], err)
continue
}

View File

@@ -39,4 +39,15 @@ func Test_Overlay_List(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, buf.String(), "email.ww")
})
t.Run("overlay list long", func(t *testing.T) {
baseCmd.SetArgs([]string{"--long"})
baseCmd := GetCommand()
buf := new(bytes.Buffer)
baseCmd.SetOut(buf)
baseCmd.SetErr(buf)
wwlog.SetLogWriter(buf)
err := baseCmd.Execute()
assert.NoError(t, err)
assert.Contains(t, buf.String(), "email.ww")
})
}

View File

@@ -2,7 +2,7 @@ package list
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/pkg/overlay"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -12,15 +12,8 @@ var (
Short: "List Warewulf Overlays and files",
Long: "This command displays information about all Warewulf overlays or the specified\nOVERLAY_NAME. It also supports listing overlay content information.",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(0),
Aliases: []string{"ls"},
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
},
ValidArgsFunction: completions.Overlays,
}
ListContents bool
ListLong bool

View File

@@ -2,6 +2,7 @@ package mkdir
import (
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
@@ -11,7 +12,14 @@ var (
Short: "Create a new directory within an Overlay",
Long: "This command creates a new directory within the Warewulf OVERLAY_NAME.",
RunE: CobraRunE,
Args: cobra.MinimumNArgs(2),
Args: cobra.ExactArgs(2),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completions.Overlays(cmd, args, toComplete)
} else {
return completions.None(cmd, args, toComplete)
}
},
}
PermMode int32
)

View File

@@ -34,7 +34,6 @@ func init() {
baseCmd.AddCommand(imprt.GetCommand())
baseCmd.AddCommand(chmod.GetCommand())
baseCmd.AddCommand(chown.GetCommand())
}
// GetRootCommand returns the root cobra.Command for the application.

View File

@@ -4,8 +4,8 @@ import (
"log"
"github.com/spf13/cobra"
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/overlay"
)
var (
@@ -18,16 +18,10 @@ var (
Aliases: []string{"cat"},
Args: cobra.ExactArgs(2),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
list := overlay.FindOverlays()
return list, cobra.ShellCompDirectiveNoFileComp
} else if len(args) == 1 {
ret, err := overlay.OverlayGetFiles(args[0])
if err == nil {
return ret, cobra.ShellCompDirectiveNoFileComp
}
if len(args) < 2 {
return completions.OverlayAndFiles(cmd, args, toComplete)
}
return []string{""}, cobra.ShellCompDirectiveNoFileComp
return completions.None(cmd, args, toComplete)
},
}
NodeName string