github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  
    14  	"github.com/juju/juju/cmd/output"
    15  )
    16  
    17  // formatPoolListTabular returns a tabular summary of pool instances or
    18  // errors out if parameter is not a map of PoolInfo.
    19  func formatPoolListTabular(writer io.Writer, value interface{}) error {
    20  	pools, ok := value.(map[string]PoolInfo)
    21  	if !ok {
    22  		return errors.Errorf("expected value of type %T, got %T", pools, value)
    23  	}
    24  	formatPoolsTabular(writer, pools)
    25  	return nil
    26  }
    27  
    28  // formatPoolsTabular returns a tabular summary of pool instances.
    29  func formatPoolsTabular(writer io.Writer, pools map[string]PoolInfo) {
    30  	tw := output.TabWriter(writer)
    31  	print := func(values ...string) {
    32  		fmt.Fprintln(tw, strings.Join(values, "\t"))
    33  	}
    34  
    35  	print("Name", "Provider", "Attrs")
    36  
    37  	poolNames := make([]string, 0, len(pools))
    38  	for name := range pools {
    39  		poolNames = append(poolNames, name)
    40  	}
    41  	sort.Strings(poolNames)
    42  	for _, name := range poolNames {
    43  		pool := pools[name]
    44  		// order by key for deterministic return
    45  		keys := make([]string, 0, len(pool.Attrs))
    46  		for key := range pool.Attrs {
    47  			keys = append(keys, key)
    48  		}
    49  		sort.Strings(keys)
    50  		attrs := make([]string, len(pool.Attrs))
    51  		for i, key := range keys {
    52  			attrs[i] = fmt.Sprintf("%v=%v", key, pool.Attrs[key])
    53  		}
    54  		print(name, pool.Provider, strings.Join(attrs, " "))
    55  	}
    56  	tw.Flush()
    57  }