Files
warewulf/internal/app/wwctl/node/delete/main.go
2020-11-21 21:51:14 -08:00

56 lines
1.1 KiB
Go

package delete
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"os"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
var count int
nodeDB, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Failed to open node database: %s\n", err)
os.Exit(1)
}
nodeList, err := nodeDB.SearchByNameList(args)
for _, n := range nodeList {
if SetGroup != "" && SetGroup != n.GroupName {
wwlog.Printf(wwlog.DEBUG, "skipping node of different group: %s/%s\n", n.GroupName, n.Id)
continue
}
err := nodeDB.DelNode(n.GroupName, n.Id)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
} else {
count++
}
}
if count > 0 {
q := fmt.Sprintf("Are you sure you want to delete %d nodes(s)", count)
prompt := promptui.Prompt{
Label: q,
IsConfirm: true,
}
result, _ := prompt.Run()
if result == "y" || result == "yes" {
nodeDB.Persist()
}
} else {
wwlog.Printf(wwlog.INFO, "No nodes found\n")
}
return nil
}