github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/storage/volumelistformatters.go (about) 1 package storage 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "text/tabwriter" 8 9 "github.com/dustin/go-humanize" 10 "github.com/juju/errors" 11 "github.com/juju/utils/set" 12 ) 13 14 // formatVolumeListTabular returns a tabular summary of volume instances. 15 func formatVolumeListTabular(value interface{}) ([]byte, error) { 16 infos, ok := value.(map[string]map[string]map[string]VolumeInfo) 17 if !ok { 18 return nil, errors.Errorf("expected value of type %T, got %T", infos, value) 19 } 20 return formatVolumeListTabularTyped(infos), nil 21 } 22 23 func formatVolumeListTabularTyped(infos map[string]map[string]map[string]VolumeInfo) []byte { 24 var out bytes.Buffer 25 const ( 26 // To format things into columns. 27 minwidth = 0 28 tabwidth = 1 29 padding = 2 30 padchar = ' ' 31 flags = 0 32 ) 33 tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags) 34 35 print := func(values ...string) { 36 fmt.Fprintln(tw, strings.Join(values, "\t")) 37 } 38 print("MACHINE", "UNIT", "STORAGE", "DEVICE", "VOLUME", "ID", "SIZE", "STATE", "MESSAGE") 39 40 // 1. sort by machines 41 machines := set.NewStrings() 42 for machine := range infos { 43 if !machines.Contains(machine) { 44 machines.Add(machine) 45 } 46 } 47 for _, machine := range machines.SortedValues() { 48 machineUnits := infos[machine] 49 50 // 2. sort by unit 51 units := set.NewStrings() 52 for unit := range machineUnits { 53 if !units.Contains(unit) { 54 units.Add(unit) 55 } 56 } 57 for _, unit := range units.SortedValues() { 58 unitStorages := machineUnits[unit] 59 60 // 3. sort by storage 61 storages := set.NewStrings() 62 for storage := range unitStorages { 63 if !storages.Contains(storage) { 64 storages.Add(storage) 65 } 66 } 67 for _, storage := range storages.SortedValues() { 68 info := unitStorages[storage] 69 var size string 70 if info.Size > 0 { 71 size = humanize.IBytes(info.Size * humanize.MiByte) 72 } 73 print( 74 machine, unit, storage, info.DeviceName, 75 info.Volume, info.VolumeId, size, 76 string(info.Status.Current), info.Status.Message, 77 ) 78 } 79 } 80 } 81 tw.Flush() 82 return out.Bytes() 83 }