refactored overlay class

overlay.GetOverlay(name) returns now an error if the overlay doesn't
exist. This is the most canonical way to act if there is no overlay.
This commit is contained in:
Christian Goll
2025-08-04 15:18:51 +02:00
committed by Jonathon Anderson
parent c17fe8d512
commit 6f4fd60d8f
32 changed files with 1105 additions and 437 deletions

View File

@@ -2,6 +2,7 @@ package list
import (
"os"
"strconv"
"syscall"
"github.com/spf13/cobra"
@@ -11,62 +12,80 @@ import (
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
var overlays []string
/*
RunE needs a function of type func(*cobraCommand,[]string) err, but
in order to avoid global variables which mess up testing a function of
the required type is returned
*/
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var overlays []string
if len(args) > 0 {
overlays = args
} else {
overlays = overlay.FindOverlays()
}
t := table.New(cmd.OutOrStdout())
if ListLong {
t.AddHeader("PERM MODE", "UID", "GID", "OVERLAY", "FILE PATH", "SITE")
} else {
t.AddHeader("OVERLAY NAME", "FILES/DIRS", "SITE")
}
for _, name := range overlays {
overlay_ := overlay.GetOverlay(name)
if !overlay_.Exists() {
wwlog.Error("system/%s (path not found:%s)", name, overlay_.Rootfs())
continue
}
files := util.FindFiles(overlay_.Rootfs())
wwlog.Debug("Iterating overlay rootfs: %s", overlay_.Rootfs())
if ListLong {
for file := range files {
s, err := os.Stat(overlay_.File(files[file]))
if err != nil {
wwlog.Warn("%s: %s: %s", name, files[file], err)
continue
}
fileMode := s.Mode()
perms := fileMode & os.ModePerm
sys := s.Sys()
t.AddLine(perms, sys.(*syscall.Stat_t).Uid, sys.(*syscall.Stat_t).Gid, name, files[file], overlay_.IsSiteOverlay())
}
} else if ListContents {
var fileCount int
for file := range files {
t.AddLine(name, files[file], overlay_.IsSiteOverlay())
fileCount++
}
if fileCount == 0 {
t.AddLine(name, 0, overlay_.IsSiteOverlay())
}
if len(args) > 0 {
overlays = args
} else {
t.AddLine(name, len(files), overlay_.IsSiteOverlay())
overlays = overlay.FindOverlays()
}
}
t.Print()
return nil
t := table.New(cmd.OutOrStdout())
locationStr := "SITE"
if vars.ShowPath {
locationStr = "PATH"
}
if vars.ListLong {
t.AddHeader("PERM MODE", "UID", "GID", "OVERLAY", "FILE PATH", locationStr)
} else {
t.AddHeader("OVERLAY NAME", "FILES/DIRS", locationStr)
}
for _, name := range overlays {
overlay_, err := overlay.GetOverlay(name)
if err != nil {
wwlog.Error("%s:%s", name, err)
continue
}
files := util.FindFiles(overlay_.Rootfs())
wwlog.Debug("Iterating overlay rootfs: %s", overlay_.Rootfs())
if vars.ListLong {
for file := range files {
s, err := os.Stat(overlay_.File(files[file]))
if err != nil {
wwlog.Warn("%s: %s: %s", name, files[file], err)
continue
}
fileMode := s.Mode()
perms := fileMode & os.ModePerm
sys := s.Sys()
locLine := strconv.FormatBool(overlay_.IsSiteOverlay())
if vars.ShowPath {
locLine = overlay_.Path()
}
t.AddLine(perms, sys.(*syscall.Stat_t).Uid, sys.(*syscall.Stat_t).Gid, name, files[file], locLine)
}
} else {
locLine := strconv.FormatBool(overlay_.IsSiteOverlay())
if vars.ShowPath {
locLine = overlay_.Path()
}
if vars.ListContents {
var fileCount int
for file := range files {
t.AddLine(name, files[file], locLine)
fileCount++
}
if fileCount == 0 {
t.AddLine(name, 0, locLine)
}
} else {
t.AddLine(name, len(files), locLine)
}
}
}
t.Print()
return nil
}
}

View File

@@ -29,8 +29,8 @@ func Test_Overlay_List(t *testing.T) {
assert.Contains(t, buf.String(), "testoverlay")
})
t.Run("overlay list all", func(t *testing.T) {
baseCmd.SetArgs([]string{"-a"})
baseCmd := GetCommand()
baseCmd.SetArgs([]string{"-a"})
buf := new(bytes.Buffer)
baseCmd.SetOut(buf)
baseCmd.SetErr(buf)
@@ -40,8 +40,8 @@ func Test_Overlay_List(t *testing.T) {
assert.Contains(t, buf.String(), "email.ww")
})
t.Run("overlay list long", func(t *testing.T) {
baseCmd.SetArgs([]string{"--long"})
baseCmd := GetCommand()
baseCmd.SetArgs([]string{"--long"})
buf := new(bytes.Buffer)
baseCmd.SetOut(buf)
baseCmd.SetErr(buf)
@@ -50,4 +50,16 @@ func Test_Overlay_List(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, buf.String(), "email.ww")
})
t.Run("overlay list path", func(t *testing.T) {
baseCmd := GetCommand()
baseCmd.SetArgs([]string{"--path"})
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(), env.BaseDir)
})
}

View File

@@ -5,28 +5,29 @@ import (
"github.com/warewulf/warewulf/internal/app/wwctl/completions"
)
var (
baseCmd = &cobra.Command{
// Holds the variables which are needed in CobraRunE
type variables struct {
ListContents bool
ListLong bool
ShowPath bool
}
// GetCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
vars := variables{}
baseCmd := &cobra.Command{
DisableFlagsInUseLine: true,
Use: "list [OPTIONS] OVERLAY_NAME",
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,
RunE: CobraRunE(&vars),
Aliases: []string{"ls"},
ValidArgsFunction: completions.Overlays,
Args: cobra.ArbitraryArgs,
}
ListContents bool
ListLong bool
)
baseCmd.PersistentFlags().BoolVarP(&vars.ListContents, "all", "a", false, "List the contents of overlays")
baseCmd.PersistentFlags().BoolVarP(&vars.ListLong, "long", "l", false, "List 'long' of all overlay contents")
baseCmd.PersistentFlags().BoolVarP(&vars.ShowPath, "path", "p", false, "Show the absolute path to the overlay")
func init() {
baseCmd.PersistentFlags().BoolVarP(&ListContents, "all", "a", false, "List the contents of overlays")
baseCmd.PersistentFlags().BoolVarP(&ListLong, "long", "l", false, "List 'long' of all overlay contents")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}