56 lines
1.1 KiB
Go
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.String() {
|
|
wwlog.Printf(wwlog.DEBUG, "skipping node of different group: %s/%s\n", n.GroupName, n.Id)
|
|
continue
|
|
}
|
|
err := nodeDB.DelNode(n.GroupName.String(), n.Id.String())
|
|
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 {
|
|
fmt.Printf("No nodes found\n")
|
|
}
|
|
|
|
return nil
|
|
}
|