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