Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(748)

Unified Diff: go/src/infra/crimson/cmd/cmdhelper/cmdhelper.go

Issue 2110963002: Crimson client: created add-host and query-host (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@crimson-add-host
Patch Set: JSON is printed indented Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | go/src/infra/crimson/cmd/crimson/main.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/crimson/cmd/cmdhelper/cmdhelper.go
diff --git a/go/src/infra/crimson/cmd/cmdhelper/cmdhelper.go b/go/src/infra/crimson/cmd/cmdhelper/cmdhelper.go
index ddb50ad060b6cce6157583f07f44ae3e1d10e7b4..700b0c407258bbe2c514c1fa4dfb241b4328ab70 100644
--- a/go/src/infra/crimson/cmd/cmdhelper/cmdhelper.go
+++ b/go/src/infra/crimson/cmd/cmdhelper/cmdhelper.go
@@ -309,7 +309,7 @@ func FormatIPRange(ipRanges []*crimson.IPRange, format FormatType) ([]string, er
switch format {
case jsonFormat:
- jsonBytes, err := json.Marshal(ipRanges)
+ jsonBytes, err := json.MarshalIndent(ipRanges, "", " ")
if err != nil {
return []string{}, err
}
@@ -342,3 +342,44 @@ func PrintIPRange(ipRanges []*crimson.IPRange, format FormatType) {
fmt.Println(s)
}
}
+
+// FormatHostList formats a list of hosts for pretty-printing.
+func FormatHostList(hostList *crimson.HostList, format FormatType) ([]string, error) {
+ var formatter Formatter
+
+ switch format {
+ case jsonFormat:
+ jsonBytes, err := json.MarshalIndent(hostList.Hosts, "", " ")
+ if err != nil {
+ return []string{}, err
+ }
+ return []string{string(jsonBytes)}, nil
+ case textFormat:
+ formatter = &TextFormatter{}
+ case csvFormat:
+ formatter = &CSVFormatter{}
+ }
+ rows := [][]string{{"mac", "ip", "site", "hostname", "class"}}
+ for _, host := range hostList.Hosts {
+ rows = append(rows, []string{
+ fmt.Sprintf("%s", host.MacAddr),
+ fmt.Sprintf("%s", host.Ip),
+ fmt.Sprintf("%s", host.Site),
+ fmt.Sprintf("%s", host.Hostname),
+ fmt.Sprintf("%s", host.BootClass),
+ })
+ }
+ return formatter.FormatRows(rows), nil
+}
+
+// PrintHostList pretty-prints a list of hosts.
+func PrintHostList(hostList *crimson.HostList, format FormatType) {
+ lines, err := FormatHostList(hostList, format)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "ERROR: %s", err)
+ return
+ }
+ for _, s := range lines {
+ fmt.Println(s)
+ }
+}
« no previous file with comments | « no previous file | go/src/infra/crimson/cmd/crimson/main.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698