wwctl image completions and arguments

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-08 14:52:48 -07:00
parent 1d34477425
commit 5494424c09
14 changed files with 59 additions and 55 deletions

View File

@@ -11,36 +11,39 @@ import (
"github.com/spf13/cobra"
cntexec "github.com/warewulf/warewulf/internal/app/wwctl/image/exec"
"github.com/warewulf/warewulf/internal/pkg/image"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
imageName := args[0]
var allargs []string
if !image.ValidSource(imageName) {
return fmt.Errorf("unknown Warewulf image: %s", imageName)
}
shellName := os.Getenv("SHELL")
if !image.ValidSource(imageName) {
return fmt.Errorf("unknown Warewulf image: %s", imageName)
}
var shells []string
if shellName == "" {
shells = append(shells, "/bin/bash")
} else {
shells = append(shells, shellName, "/bin/bash")
if os.Getenv("SHELL") != "" {
shells = append(shells, os.Getenv("SHELL"))
}
shells = append(shells, "/bin/bash", "/bin/sh")
shellName := ""
for _, s := range shells {
if _, err := os.Stat(path.Join(image.RootFsDir(imageName), s)); err == nil {
if util.IsFile(path.Join(image.RootFsDir(imageName), s)) {
shellName = s
break
}
}
if shellName == "" {
return fmt.Errorf("no shell found in image: %s", imageName)
}
args = append(args, shellName)
allargs = append(allargs, args...)
wwlog.Debug("Calling exec with args: %s", allargs)
wwlog.Debug("%s: exec with args: %v", imageName, args)
cntexec.SetBinds(binds)
cntexec.SetNode(nodeName)
cntexec.SyncUser = syncUser
@@ -48,5 +51,5 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
if cntexec.Build {
wwlog.Info("Image build will be skipped if the shell ends with a non-zero exit code.")
}
return cntexec.CobraRunE(cmd, allargs)
return cntexec.CobraRunE(cmd, args)
}