Remove Chdir from util.FindFiles
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
@@ -2,8 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"path"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
)
|
||||
|
||||
var ConfigFile = "@SYSCONFDIR@/warewulf/warewulf.conf"
|
||||
@@ -36,7 +34,7 @@ type TFTPConf struct {
|
||||
}
|
||||
|
||||
func (this TFTPConf) Enabled() bool {
|
||||
return util.BoolP(this.EnabledP)
|
||||
return BoolP(this.EnabledP)
|
||||
}
|
||||
|
||||
// WarewulfConf adds additional Warewulf-specific configuration to
|
||||
@@ -52,23 +50,23 @@ type WarewulfConf struct {
|
||||
}
|
||||
|
||||
func (this WarewulfConf) Secure() bool {
|
||||
return util.BoolP(this.SecureP)
|
||||
return BoolP(this.SecureP)
|
||||
}
|
||||
|
||||
func (this WarewulfConf) AutobuildOverlays() bool {
|
||||
return util.BoolP(this.AutobuildOverlaysP)
|
||||
return BoolP(this.AutobuildOverlaysP)
|
||||
}
|
||||
|
||||
func (this WarewulfConf) EnableHostOverlay() bool {
|
||||
return util.BoolP(this.EnableHostOverlayP)
|
||||
return BoolP(this.EnableHostOverlayP)
|
||||
}
|
||||
|
||||
func (this WarewulfConf) Syslog() bool {
|
||||
return util.BoolP(this.SyslogP)
|
||||
return BoolP(this.SyslogP)
|
||||
}
|
||||
|
||||
func (this WarewulfConf) GrubBoot() bool {
|
||||
return util.BoolP(this.GrubBootP)
|
||||
return BoolP(this.GrubBootP)
|
||||
}
|
||||
|
||||
func (paths BuildConfig) NodesConf() string {
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
)
|
||||
|
||||
// DHCPConf represents the configuration for the DHCP service that
|
||||
// Warewulf will configure.
|
||||
type DHCPConf struct {
|
||||
@@ -15,5 +11,5 @@ type DHCPConf struct {
|
||||
}
|
||||
|
||||
func (this DHCPConf) Enabled() bool {
|
||||
return util.BoolP(this.EnabledP)
|
||||
return BoolP(this.EnabledP)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
)
|
||||
|
||||
// A MountEntry represents a bind mount that is applied to a container
|
||||
// during exec and shell.
|
||||
type MountEntry struct {
|
||||
@@ -15,9 +11,9 @@ type MountEntry struct {
|
||||
}
|
||||
|
||||
func (this MountEntry) ReadOnly() bool {
|
||||
return util.BoolP(this.ReadOnlyP)
|
||||
return BoolP(this.ReadOnlyP)
|
||||
}
|
||||
|
||||
func (this MountEntry) Copy() bool {
|
||||
return util.BoolP(this.CopyP)
|
||||
return BoolP(this.CopyP)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"github.com/creasty/defaults"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
)
|
||||
|
||||
// NFSConf represents the NFS configuration that will be used by
|
||||
@@ -16,7 +14,7 @@ type NFSConf struct {
|
||||
}
|
||||
|
||||
func (this NFSConf) Enabled() bool {
|
||||
return util.BoolP(this.EnabledP)
|
||||
return BoolP(this.EnabledP)
|
||||
}
|
||||
|
||||
// An NFSExportConf reprents a single NFS export / mount.
|
||||
@@ -28,7 +26,7 @@ type NFSExportConf struct {
|
||||
}
|
||||
|
||||
func (this NFSExportConf) Mount() bool {
|
||||
return util.BoolP(this.MountP)
|
||||
return BoolP(this.MountP)
|
||||
}
|
||||
|
||||
// Implements the Unmarshal interface for NFSConf to set default
|
||||
|
||||
5
internal/pkg/config/util.go
Normal file
5
internal/pkg/config/util.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
func BoolP(p *bool) bool {
|
||||
return p != nil && *p
|
||||
}
|
||||
@@ -18,10 +18,6 @@ import (
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func BoolP(p *bool) bool {
|
||||
return p != nil && *p
|
||||
}
|
||||
|
||||
func FirstError(errs ...error) (err error) {
|
||||
for _, e := range errs {
|
||||
if err == nil {
|
||||
@@ -136,37 +132,40 @@ func ValidString(pattern string, expr string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ******************************************************************************
|
||||
func FindFiles(path string) []string {
|
||||
var ret []string
|
||||
|
||||
wwlog.Debug("Changing directory to FindFiles path: %s", path)
|
||||
err := os.Chdir(path)
|
||||
if err != nil {
|
||||
wwlog.Warn("Could not chdir() to: %s", path)
|
||||
return ret
|
||||
}
|
||||
wwlog.Debug("Finding files in path: %s", path)
|
||||
|
||||
err = filepath.Walk(".", func(location string, info os.FileInfo, err error) error {
|
||||
err := filepath.Walk(path, func(location string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
wwlog.Warn("Error walking path %s: %v", location, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if location == "." {
|
||||
// Get the relative path from the base directory
|
||||
relPath, relErr := filepath.Rel(path, location)
|
||||
if relErr != nil {
|
||||
wwlog.Warn("Error computing relative path for %s: %v", location, relErr)
|
||||
return relErr
|
||||
}
|
||||
|
||||
if relPath == "." {
|
||||
return nil
|
||||
}
|
||||
|
||||
if IsDir(location) {
|
||||
wwlog.Debug("FindFiles() found directory: %s", location)
|
||||
ret = append(ret, location+"/")
|
||||
if info.IsDir() {
|
||||
wwlog.Debug("FindFiles() found directory: %s", relPath)
|
||||
ret = append(ret, relPath+"/")
|
||||
} else {
|
||||
wwlog.Debug("FindFiles() found file: %s", location)
|
||||
ret = append(ret, location)
|
||||
wwlog.Debug("FindFiles() found file: %s", relPath)
|
||||
ret = append(ret, relPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
wwlog.Warn("Error during file walk: %v", err)
|
||||
return ret
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/warewulf/warewulf/internal/pkg/testenv"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
@@ -19,6 +20,40 @@ func TryCreatePath(t *testing.T, elem ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_FindFiles(t *testing.T) {
|
||||
var tests = map[string]struct {
|
||||
createFiles []string
|
||||
findFiles []string
|
||||
}{
|
||||
"no files": {
|
||||
createFiles: []string{},
|
||||
findFiles: nil,
|
||||
},
|
||||
"single file": {
|
||||
createFiles: []string{"testfile"},
|
||||
findFiles: []string{"testfile"},
|
||||
},
|
||||
"nested file": {
|
||||
createFiles: []string{"testdir/testfile"},
|
||||
findFiles: []string{"testdir/", "testdir/testfile"},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
env := testenv.New(t)
|
||||
defer env.RemoveAll(t)
|
||||
env.MkdirAll(t, "/test")
|
||||
for _, file_ := range tt.createFiles {
|
||||
env.CreateFile(t, filepath.Join("/test", file_))
|
||||
}
|
||||
|
||||
files := FindFiles(env.GetPath("/test"))
|
||||
assert.Equal(t, tt.findFiles, files)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_FindFilterFiles(t *testing.T) {
|
||||
wwlog.SetLogLevel(wwlog.DEBUG)
|
||||
dir, err := os.MkdirTemp(os.TempDir(), "warewulf-test")
|
||||
|
||||
Reference in New Issue
Block a user