github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/payload/status/output_tabular.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package status 5 6 import ( 7 "fmt" 8 "io" 9 "strings" 10 11 "github.com/juju/errors" 12 13 "github.com/juju/juju/cmd/output" 14 ) 15 16 const tabularSection = "[Unit Payloads]" 17 18 var ( 19 tabularColumns = []string{ 20 "Unit", 21 "Machine", 22 "Payload class", 23 "Status", 24 "Type", 25 "Id", 26 "Tags", // TODO(ericsnow) Chane this to "LABELS"? 27 } 28 29 tabularHeader = strings.Join(tabularColumns, "\t") + "\t" 30 tabularRow = strings.Repeat("%s\t", len(tabularColumns)) 31 ) 32 33 // FormatTabular writes a tabular summary of payloads. 34 func FormatTabular(writer io.Writer, value interface{}) error { 35 payloads, valueConverted := value.([]FormattedPayload) 36 if !valueConverted { 37 return errors.Errorf("expected value of type %T, got %T", payloads, value) 38 } 39 40 // TODO(ericsnow) sort the rows first? 41 42 tw := output.TabWriter(writer) 43 44 // Write the header. 45 fmt.Fprintln(tw, tabularSection) 46 fmt.Fprintln(tw, tabularHeader) 47 48 // Print each payload to its own row. 49 for _, payload := range payloads { 50 // tabularColumns must be kept in sync with these. 51 fmt.Fprintf(tw, tabularRow+"\n", 52 payload.Unit, 53 payload.Machine, 54 payload.Class, 55 payload.Status, 56 payload.Type, 57 payload.ID, 58 strings.Join(payload.Labels, " "), 59 ) 60 } 61 tw.Flush() 62 63 return nil 64 }