use tablewriter to format the output

Signed-off-by: jason yang <jasonyangshadow@gmail.com>
This commit is contained in:
jason yang
2023-03-23 02:44:59 +00:00
parent ceb7859a85
commit 522c3478fb
14 changed files with 144 additions and 51 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
@@ -18,7 +19,6 @@ import (
)
func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) {
if cbp == nil {
return fmt.Errorf("ContainerBuildParameter is nil")
}
@@ -60,7 +60,7 @@ func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) {
return
}
//TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map
// TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map
profiles, _ := nodeDB.FindAllProfiles()
for _, profile := range profiles {
wwlog.Debug("Looking for profile default: %s", profile.Id.Get())
@@ -85,7 +85,6 @@ func ContainerBuild(cbp *wwapiv1.ContainerBuildParameter) (err error) {
}
func ContainerDelete(cdp *wwapiv1.ContainerDeleteParameter) (err error) {
if cdp == nil {
return fmt.Errorf("ContainerDeleteParameter is nil")
}
@@ -132,7 +131,6 @@ ARG_LOOP:
}
func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName string, err error) {
if cip == nil {
err = fmt.Errorf("NodeAddParameter is nil")
return
@@ -176,6 +174,14 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
// TODO: mhink - return was missing here. Was that deliberate?
}
if util.IsFile(cip.Source) && !filepath.IsAbs(cip.Source) {
cip.Source, err = filepath.Abs(cip.Source)
if err != nil {
err = fmt.Errorf("when resolving absolute path of %s, err: %v", cip.Source, err)
wwlog.Error(err.Error())
return
}
}
err = container.ImportDocker(cip.Source, cip.Name, sCtx)
if err != nil {
err = fmt.Errorf("could not import image: %s", err.Error())
@@ -221,7 +227,7 @@ func ContainerImport(cip *wwapiv1.ContainerImportParameter) (containerName strin
return
}
//TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map
// TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map
profiles, _ := nodeDB.FindAllProfiles()
for _, profile := range profiles {
wwlog.Debug("Looking for profile default: %s", profile.Id.Get())
@@ -332,7 +338,6 @@ func ContainerList() (containerInfo []*wwapiv1.ContainerInfo, err error) {
}
func ContainerShow(csp *wwapiv1.ContainerShowParameter) (response *wwapiv1.ContainerShowResponse, err error) {
containerName := csp.ContainerName
if !container.ValidName(containerName) {

View File

@@ -29,8 +29,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
sort.Strings(nodeGet.Nodes)
if nodeGet.Type == wwapiv1.GetNodeList_Simple {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-26s %s", "NODE NAME", "PROFILES", "NETWORK"))
nodeList.Output = append(nodeList.Output, (strings.Repeat("=", 80)))
fmt.Sprintf("%s=%s=%s", "NODE NAME", "PROFILES", "NETWORK"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
var netNames []string
for k := range n.NetDevs {
@@ -38,33 +37,31 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
}
sort.Strings(netNames)
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-26s %s", n.Id.Print(), n.Profiles.Print(), strings.Join(netNames, ", ")))
fmt.Sprintf("%s=%s=%s", n.Id.Print(), n.Profiles.Print(), strings.Join(netNames, ", ")))
}
} else if nodeGet.Type == wwapiv1.GetNodeList_Network {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-8s %-18s %-15s %-15s %-15s", "NODE NAME", "NAME", "HWADDR", "IPADDR", "GATEWAY", "DEVICE"),
strings.Repeat("=", 90))
fmt.Sprintf("%s=%s=%s=%s=%s=%s", "NODE NAME", "NAME", "HWADDR", "IPADDR", "GATEWAY", "DEVICE"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
if len(n.NetDevs) > 0 {
for name := range n.NetDevs {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-8s %-18s %-15s %-15s %-15s", n.Id.Print(), name,
fmt.Sprintf("%s=%s=%s=%s=%s=%s", n.Id.Print(), name,
n.NetDevs[name].Hwaddr.Print(),
n.NetDevs[name].Ipaddr.Print(),
n.NetDevs[name].Gateway.Print(),
n.NetDevs[name].Device.Print()))
}
} else {
fmt.Printf("%-22s %-6s %-18s %-15s %-15s", n.Id.Print(), "--", "--", "--", "--")
fmt.Printf("%s=%s=%s=%s=%s=%s", n.Id.Print(), "--", "--", "--", "--", "--")
}
}
} else if nodeGet.Type == wwapiv1.GetNodeList_Ipmi {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-10s %-20s %-14s", "NODE NAME", "IPMI IPADDR", "IPMI PORT", "IPMI USERNAME", "IPMI INTERFACE"),
strings.Repeat("=", 98))
fmt.Sprintf("%s=%s=%s=%s=%s", "NODE NAME", "IPMI IPADDR", "IPMI PORT", "IPMI USERNAME", "IPMI INTERFACE"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-10s %-20s %-14s", n.Id.Print(),
fmt.Sprintf("%s=%s=%s=%s=%s", n.Id.Print(),
n.Ipmi.Ipaddr.Print(),
n.Ipmi.Port.Print(),
n.Ipmi.UserName.Print(),
@@ -72,19 +69,18 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
}
} else if nodeGet.Type == wwapiv1.GetNodeList_Long {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-16s %s", "NODE NAME", "KERNEL OVERRIDE", "CONTAINER", "OVERLAYS (S/R)"),
strings.Repeat("=", 85))
fmt.Sprintf("%s=%s=%s=%s", "NODE NAME", "KERNEL OVERRIDE", "CONTAINER", "OVERLAYS (S/R)"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-22s %-16s %-16s %s", n.Id.Print(),
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(),
n.Kernel.Override.Print(),
n.ContainerName.Print(),
n.SystemOverlay.Print()+"/"+n.RuntimeOverlay.Print()))
}
} else if nodeGet.Type == wwapiv1.GetNodeList_All {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s=%s=%s=%s", "NODE", "FIELD", "PROFILE", "VALUE"))
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", "NODE", "FIELD", "PROFILE", "VALUE"), strings.Repeat("=", 85))
nType := reflect.TypeOf(n)
nVal := reflect.ValueOf(n)
nConfType := reflect.TypeOf(node.NodeConf{})
@@ -101,12 +97,12 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
fieldSource = entr.Source()
fieldVal = entr.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.Entry{}) {
entrMap := nVal.Field(i).Interface().(map[string]*node.Entry)
for key, val := range entrMap {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), key, val.Source(), val.Print()))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), key, val.Source(), val.Print()))
}
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.NetDevEntry{}) {
netDevs := nVal.Field(i).Interface().(map[string]*node.NetDevEntry)
@@ -132,7 +128,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
// only print fields with lopt
if netConfField.Tag.Get("lopt") != "" {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
}
} else if netInfoType.Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range netInfoVal.Field(j).Interface().(map[string]*node.Entry) {
@@ -140,7 +136,7 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
fieldSource = val.Source()
fieldVal = val.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), keyfieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), keyfieldName, fieldSource, fieldVal))
}
}
@@ -162,14 +158,14 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
fieldSource = entr.Source()
fieldVal = entr.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nestInfoType.Elem().Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range nestInfoVal.Elem().Field(j).Interface().(map[string]*node.Entry) {
fieldName = fieldName + ":" + key
fieldSource = val.Source()
fieldVal = val.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", n.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
}
}
}
@@ -177,7 +173,6 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
}
}
}
return
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/node"
@@ -34,7 +33,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
if ShowOpt.ShowAll {
for _, p := range profiles {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", "PROFILE", "FIELD", "PROFILE", "VALUE"), strings.Repeat("=", 85))
fmt.Sprintf("%s=%s=%s=%s", "PROFILE", "FIELD", "PROFILE", "VALUE"))
nType := reflect.TypeOf(p)
nVal := reflect.ValueOf(p)
nConfType := reflect.TypeOf(node.NodeConf{})
@@ -51,12 +50,12 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
fieldSource = entr.Source()
fieldVal = entr.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.Entry{}) {
entrMap := nVal.Field(i).Interface().(map[string]*node.Entry)
for key, val := range entrMap {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), key, val.Source(), val.Print()))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), key, val.Source(), val.Print()))
}
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.NetDevEntry{}) {
netDevs := nVal.Field(i).Interface().(map[string]*node.NetDevEntry)
@@ -82,7 +81,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
// only print fields with lopt
if netConfField.Tag.Get("lopt") != "" {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
}
} else if netInfoType.Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range netInfoVal.Field(j).Interface().(map[string]*node.Entry) {
@@ -90,7 +89,7 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
fieldSource = val.Source()
fieldVal = val.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), keyfieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), keyfieldName, fieldSource, fieldVal))
}
}
@@ -112,14 +111,14 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
fieldSource = entr.Source()
fieldVal = entr.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nestInfoType.Elem().Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range nestInfoVal.Elem().Field(j).Interface().(map[string]*node.Entry) {
fieldName = fieldName + ":" + key
fieldSource = val.Source()
fieldVal = val.Print()
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %-18s %-12s %s", p.Id.Print(), fieldName, fieldSource, fieldVal))
fmt.Sprintf("%s=%s=%s=%s", p.Id.Print(), fieldName, fieldSource, fieldVal))
}
}
}
@@ -128,12 +127,11 @@ func ProfileList(ShowOpt *wwapiv1.GetProfileList) (profileList wwapiv1.ProfileLi
}
} else {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %s", "PROFILE NAME", "COMMENT/DESCRIPTION"))
profileList.Output = append(profileList.Output, strings.Repeat("=", 80))
fmt.Sprintf("%s=%s", "PROFILE NAME", "COMMENT/DESCRIPTION"))
for _, profile := range profiles {
profileList.Output = append(profileList.Output,
fmt.Sprintf("%-20s %s", profile.Id.Print(), profile.Comment.Print()))
fmt.Sprintf("%s=%s", profile.Id.Print(), profile.Comment.Print()))
}
}
return