Refactor gRPC-based profile functions

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2026-03-21 21:19:45 -06:00
parent ec6884a878
commit 4cc6d03f2f
7 changed files with 157 additions and 315 deletions

View File

@@ -1,13 +1,15 @@
package list
import (
"encoding/json"
"sort"
"strings"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/warewulf/warewulf/internal/app/wwctl/table"
apiprofile "github.com/warewulf/warewulf/internal/pkg/api/profile"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
@@ -16,27 +18,49 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
if len(args) > 0 && strings.Contains(args[0], ",") {
args = strings.FieldsFunc(args[0], func(r rune) bool { return r == ',' })
}
req := wwapiv1.GetProfileList{
ShowAll: vars.showAll,
ShowYaml: vars.showYaml,
ShowJson: vars.showJson,
Profiles: args,
}
profileInfo, err := apiprofile.ProfileList(&req)
nodeDB, err := node.New()
if err != nil {
return
}
if len(profileInfo.Output) > 0 {
if vars.showYaml || vars.showJson {
wwlog.Info(profileInfo.Output[0])
} else {
t := table.New(cmd.OutOrStdout())
t.AddHeader(table.Prep(strings.Split(profileInfo.Output[0], ":=:"))...)
for _, val := range profileInfo.Output[1:] {
t.AddLine(table.Prep(strings.Split(val, ":=:"))...)
}
t.Print()
profiles, err := nodeDB.FindAllProfiles()
if err != nil {
return
}
profiles = node.FilterProfileListByName(profiles, args)
sort.Slice(profiles, func(i, j int) bool {
return profiles[i].Id() < profiles[j].Id()
})
if vars.showYaml || vars.showJson {
profileMap := make(map[string]node.Profile)
for _, profile := range profiles {
profileMap[profile.Id()] = profile
}
var buf []byte
if vars.showJson {
buf, _ = json.MarshalIndent(profileMap, "", " ")
} else {
buf, _ = yaml.Marshal(profileMap)
}
wwlog.Info(string(buf))
} else if vars.showAll {
t := table.New(cmd.OutOrStdout())
t.AddHeader("PROFILE", "FIELD", "VALUE")
for _, p := range profiles {
fields := node.GetFieldList(p)
for _, f := range fields {
t.AddLine(table.Prep([]string{p.Id(), f.Field, f.Value})...)
}
}
t.Print()
} else {
t := table.New(cmd.OutOrStdout())
t.AddHeader("PROFILE NAME", "COMMENT/DESCRIPTION")
for _, profile := range profiles {
t.AddLine(table.Prep([]string{profile.Id(), profile.Comment})...)
}
t.Print()
}
return
}