Optimized sorting methods across nodes and profiles

This commit is contained in:
Gregory Kurtzer
2021-04-25 15:43:35 -07:00
parent a321709901
commit 4bc523a052
15 changed files with 266 additions and 234 deletions

View File

@@ -13,8 +13,6 @@ import (
func CobraRunE(cmd *cobra.Command, args []string) error {
var count int
var numNodes int
var numGroups int
nodeDB, err := node.New()
if err != nil {
@@ -22,50 +20,71 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
os.Exit(1)
}
nodes, err := nodeDB.FindAllNodes()
profiles, err := nodeDB.FindAllProfiles()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not load all nodes: %s\n", err)
wwlog.Printf(wwlog.ERROR, "Could not load all profiles: %s\n", err)
os.Exit(1)
}
for _, p := range args {
err := nodeDB.DelProfile(p)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
continue
}
for _, n := range nodes {
for _, np := range n.Profiles {
if np == p {
numNodes++
wwlog.Printf(wwlog.VERBOSE, "Removing profile from node %s: %s\n", n.Id.Get(), p)
n.Profiles = util.SliceRemoveElement(n.Profiles, p)
nodeDB.NodeUpdate(n)
for _, r := range args {
for _, p := range profiles {
if p.Id.Get() == r {
nodes, err := nodeDB.FindAllNodes()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not load all nodes: %s\n", err)
os.Exit(1)
}
for _, n := range nodes {
for _, np := range n.Profiles {
if np == r {
wwlog.Printf(wwlog.VERBOSE, "Removing profile from node %s: %s\n", n.Id.Get(), r)
n.Profiles = util.SliceRemoveElement(n.Profiles, r)
nodeDB.NodeUpdate(n)
}
}
}
}
}
count++
}
if count > 0 {
if SetYes == true {
nodeDB.Persist()
} else {
q := fmt.Sprintf("Are you sure you want to delete %d profile(s) (%d groups, %d nodes)", count, numGroups, numNodes)
prompt := promptui.Prompt{
Label: q,
IsConfirm: true,
}
result, _ := prompt.Run()
if result == "y" || result == "yes" {
nodeDB.Persist()
for _, r := range args {
var found bool
for _, p := range profiles {
if p.Id.Get() == r {
count++
found = true
err := nodeDB.DelProfile(r)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
}
}
}
if !found {
fmt.Fprintf(os.Stderr, "Profile not found: %s\n", r)
}
}
if count == 0 {
fmt.Fprintf(os.Stderr, "No profiles found\n")
os.Exit(1)
}
if SetYes {
nodeDB.Persist()
} else {
wwlog.Printf(wwlog.INFO, "No groups found\n")
q := fmt.Sprintf("Are you sure you want to delete %d profile(s)", count)
prompt := promptui.Prompt{
Label: q,
IsConfirm: true,
}
result, _ := prompt.Run()
if result == "y" || result == "yes" {
nodeDB.Persist()
}
}
return nil