Files
warewulf/internal/app/wwctl/container/rename/main.go
Jonathon Anderson acfd70a645 Return an error if rename arguments != 2
The original implementation presented a warning if more than two
arguments were presented, and didn't handle the case where fewer than
two arguments were provided. This change requires precisely two
arguments and returns an error otherwise.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
2024-01-24 15:11:21 -07:00

46 lines
1.2 KiB
Go

package rename
import (
"fmt"
api "github.com/hpcng/warewulf/internal/pkg/api/container"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/container"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/spf13/cobra"
)
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
if len(args) != 2 {
return fmt.Errorf("rename requires 2 arguments: %d provided", len(args))
}
crp := &wwapiv1.ContainerRenameParameter{
ContainerName: args[0],
TargetName: args[1],
Build: SetBuild,
}
if !container.DoesSourceExist(crp.ContainerName) {
return fmt.Errorf("%s source dir does not exist", crp.ContainerName)
}
if container.DoesSourceExist(crp.TargetName) {
return fmt.Errorf("an other container with the name %s already exists", crp.TargetName)
}
if !container.ValidName(crp.TargetName) {
wwlog.Error("Container name contains illegal characters : %s", crp.TargetName)
return
}
err = api.ContainerRename(crp)
if err != nil {
err = fmt.Errorf("could not rename image: %s", err.Error())
return
}
wwlog.Info("Container %s successfully renamed as %s", crp.ContainerName, crp.TargetName)
return
}