github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/controller/listcontrollersformatters.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package controller
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"sort"
    10  	"strings"
    11  	"text/tabwriter"
    12  
    13  	"github.com/juju/errors"
    14  )
    15  
    16  const noValueDisplay = "-"
    17  
    18  func formatControllersListTabular(value interface{}) ([]byte, error) {
    19  	controllers, ok := value.(ControllerSet)
    20  	if !ok {
    21  		return nil, errors.Errorf("expected value of type %T, got %T", controllers, value)
    22  	}
    23  	return formatControllersTabular(controllers)
    24  }
    25  
    26  // formatControllersTabular returns a tabular summary of controller/model items
    27  // sorted by controller name alphabetically.
    28  func formatControllersTabular(set ControllerSet) ([]byte, error) {
    29  	var out bytes.Buffer
    30  
    31  	const (
    32  		// To format things into columns.
    33  		minwidth = 0
    34  		tabwidth = 1
    35  		padding  = 2
    36  		padchar  = ' '
    37  		flags    = 0
    38  	)
    39  	tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags)
    40  	print := func(values ...string) {
    41  		fmt.Fprintln(tw, strings.Join(values, "\t"))
    42  	}
    43  
    44  	print("CONTROLLER", "MODEL", "USER", "SERVER")
    45  
    46  	names := []string{}
    47  	for name, _ := range set.Controllers {
    48  		names = append(names, name)
    49  	}
    50  	sort.Strings(names)
    51  
    52  	for _, name := range names {
    53  		c := set.Controllers[name]
    54  		modelName := noValueDisplay
    55  		if c.ModelName != "" {
    56  			modelName = c.ModelName
    57  		}
    58  		userName := noValueDisplay
    59  		if c.User != "" {
    60  			userName = c.User
    61  		}
    62  		if name == set.CurrentController {
    63  			name += "*"
    64  		}
    65  		print(name, modelName, userName, c.Server)
    66  	}
    67  	tw.Flush()
    68  
    69  	return out.Bytes(), nil
    70  }