github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/storage/poollistformatters.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"sort"
    10  	"strings"
    11  
    12  	"github.com/juju/errors"
    13  	"github.com/juju/juju/cmd/output"
    14  )
    15  
    16  // formatPoolListTabular returns a tabular summary of pool instances or
    17  // errors out if parameter is not a map of PoolInfo.
    18  func formatPoolListTabular(writer io.Writer, value interface{}) error {
    19  	pools, ok := value.(map[string]PoolInfo)
    20  	if !ok {
    21  		return errors.Errorf("expected value of type %T, got %T", pools, value)
    22  	}
    23  	formatPoolsTabular(writer, pools)
    24  	return nil
    25  }
    26  
    27  // formatPoolsTabular returns a tabular summary of pool instances.
    28  func formatPoolsTabular(writer io.Writer, pools map[string]PoolInfo) {
    29  	tw := output.TabWriter(writer)
    30  	print := func(values ...string) {
    31  		fmt.Fprintln(tw, strings.Join(values, "\t"))
    32  	}
    33  
    34  	print("Name", "Provider", "Attrs")
    35  
    36  	poolNames := make([]string, 0, len(pools))
    37  	for name := range pools {
    38  		poolNames = append(poolNames, name)
    39  	}
    40  	sort.Strings(poolNames)
    41  	for _, name := range poolNames {
    42  		pool := pools[name]
    43  		// order by key for deterministic return
    44  		keys := make([]string, 0, len(pool.Attrs))
    45  		for key := range pool.Attrs {
    46  			keys = append(keys, key)
    47  		}
    48  		sort.Strings(keys)
    49  		attrs := make([]string, len(pool.Attrs))
    50  		for i, key := range keys {
    51  			attrs[i] = fmt.Sprintf("%v=%v", key, pool.Attrs[key])
    52  		}
    53  		print(name, pool.Provider, strings.Join(attrs, " "))
    54  	}
    55  	tw.Flush()
    56  }