Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package cmdhelper | 5 package cmdhelper |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bufio" | 8 "bufio" |
| 9 "bytes" | 9 "bytes" |
| 10 "encoding/json" | 10 "encoding/json" |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 func PrintIPRange(ipRanges []*crimson.IPRange, format FormatType) { | 335 func PrintIPRange(ipRanges []*crimson.IPRange, format FormatType) { |
| 336 lines, err := FormatIPRange(ipRanges, format) | 336 lines, err := FormatIPRange(ipRanges, format) |
| 337 if err != nil { | 337 if err != nil { |
| 338 fmt.Fprintf(os.Stderr, "ERROR: %s", err) | 338 fmt.Fprintf(os.Stderr, "ERROR: %s", err) |
| 339 return | 339 return |
| 340 } | 340 } |
| 341 for _, s := range lines { | 341 for _, s := range lines { |
| 342 fmt.Println(s) | 342 fmt.Println(s) |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | |
| 346 // FormatHostList formats a list of hosts for pretty-printing. | |
| 347 func FormatHostList(hostList *crimson.HostList, format FormatType) ([]string, er ror) { | |
| 348 var formatter Formatter | |
| 349 | |
| 350 switch format { | |
| 351 case jsonFormat: | |
| 352 jsonBytes, err := json.Marshal(hostList.Hosts) | |
|
Vadim Sh.
2016/06/30 23:58:12
nit: consider using https://golang.org/pkg/encodin
pgervais
2016/07/01 01:01:00
Done.
| |
| 353 if err != nil { | |
| 354 return []string{}, err | |
| 355 } | |
| 356 return []string{string(jsonBytes)}, nil | |
| 357 case textFormat: | |
| 358 formatter = &TextFormatter{} | |
| 359 case csvFormat: | |
| 360 formatter = &CSVFormatter{} | |
| 361 } | |
| 362 rows := [][]string{{"mac", "ip", "site", "hostname", "class"}} | |
| 363 for _, host := range hostList.Hosts { | |
| 364 rows = append(rows, []string{ | |
| 365 fmt.Sprintf("%s", host.MacAddr), | |
| 366 fmt.Sprintf("%s", host.Ip), | |
| 367 fmt.Sprintf("%s", host.Site), | |
| 368 fmt.Sprintf("%s", host.Hostname), | |
| 369 fmt.Sprintf("%s", host.BootClass), | |
| 370 }) | |
| 371 } | |
| 372 return formatter.FormatRows(rows), nil | |
| 373 } | |
| 374 | |
| 375 // PrintHostList pretty-prints a list of hosts. | |
| 376 func PrintHostList(hostList *crimson.HostList, format FormatType) { | |
| 377 lines, err := FormatHostList(hostList, format) | |
| 378 if err != nil { | |
| 379 fmt.Fprintf(os.Stderr, "ERROR: %s", err) | |
| 380 return | |
| 381 } | |
| 382 for _, s := range lines { | |
| 383 fmt.Println(s) | |
| 384 } | |
| 385 } | |
| OLD | NEW |