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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 func (ft *FormatType) Set(v string) error { | 302 func (ft *FormatType) Set(v string) error { |
303 return FormatTypeEnum.FlagSet(ft, v) | 303 return FormatTypeEnum.FlagSet(ft, v) |
304 } | 304 } |
305 | 305 |
306 // FormatIPRange formats a slice of IP ranges for pretty-printing. | 306 // FormatIPRange formats a slice of IP ranges for pretty-printing. |
307 func FormatIPRange(ipRanges []*crimson.IPRange, format FormatType) ([]string, er
ror) { | 307 func FormatIPRange(ipRanges []*crimson.IPRange, format FormatType) ([]string, er
ror) { |
308 var formatter Formatter | 308 var formatter Formatter |
309 | 309 |
310 switch format { | 310 switch format { |
311 case jsonFormat: | 311 case jsonFormat: |
312 » » jsonBytes, err := json.Marshal(ipRanges) | 312 » » jsonBytes, err := json.MarshalIndent(ipRanges, "", " ") |
313 if err != nil { | 313 if err != nil { |
314 return []string{}, err | 314 return []string{}, err |
315 } | 315 } |
316 return []string{string(jsonBytes)}, nil | 316 return []string{string(jsonBytes)}, nil |
317 case textFormat: | 317 case textFormat: |
318 formatter = &TextFormatter{} | 318 formatter = &TextFormatter{} |
319 case csvFormat: | 319 case csvFormat: |
320 formatter = &CSVFormatter{} | 320 formatter = &CSVFormatter{} |
321 } | 321 } |
322 rows := [][]string{{"site", "vlan", "Start IP", "End IP"}} | 322 rows := [][]string{{"site", "vlan", "Start IP", "End IP"}} |
(...skipping 12 matching lines...) Expand all 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.MarshalIndent(hostList.Hosts, "", " ") |
| 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 |