1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
13
"github.com/dustin/go-humanize"
14
"github.com/juju/errors"
17
// formatFilesystemListTabular returns a tabular summary of filesystem instances.
18
func formatFilesystemListTabular(value interface{}) ([]byte, error) {
19
infos, ok := value.(map[string]FilesystemInfo)
21
return nil, errors.Errorf("expected value of type %T, got %T", infos, value)
23
return formatFilesystemListTabularTyped(infos), nil
26
func formatFilesystemListTabularTyped(infos map[string]FilesystemInfo) []byte {
29
// To format things into columns.
36
tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags)
38
print := func(values ...string) {
39
fmt.Fprintln(tw, strings.Join(values, "\t"))
41
print("MACHINE", "UNIT", "STORAGE", "ID", "VOLUME", "PROVIDER-ID", "MOUNTPOINT", "SIZE", "STATE", "MESSAGE")
43
filesystemAttachmentInfos := make(filesystemAttachmentInfos, 0, len(infos))
44
for filesystemId, info := range infos {
45
filesystemAttachmentInfo := filesystemAttachmentInfo{
46
FilesystemId: filesystemId,
49
if info.Attachments == nil {
50
filesystemAttachmentInfos = append(filesystemAttachmentInfos, filesystemAttachmentInfo)
53
// Each unit attachment must have a corresponding filesystem
54
// attachment. Enumerate each of the filesystem attachments,
55
// and locate the corresponding unit attachment if any.
56
// Each filesystem attachment has at most one corresponding
58
for machineId, machineInfo := range info.Attachments.Machines {
59
filesystemAttachmentInfo := filesystemAttachmentInfo
60
filesystemAttachmentInfo.MachineId = machineId
61
filesystemAttachmentInfo.MachineFilesystemAttachment = machineInfo
62
for unitId, unitInfo := range info.Attachments.Units {
63
if unitInfo.MachineId == machineId {
64
filesystemAttachmentInfo.UnitId = unitId
65
filesystemAttachmentInfo.UnitStorageAttachment = unitInfo
69
filesystemAttachmentInfos = append(filesystemAttachmentInfos, filesystemAttachmentInfo)
72
sort.Sort(filesystemAttachmentInfos)
74
for _, info := range filesystemAttachmentInfos {
77
size = humanize.IBytes(info.Size * humanize.MiByte)
80
info.MachineId, info.UnitId, info.Storage,
81
info.FilesystemId, info.Volume, info.ProviderFilesystemId,
82
info.MountPoint, size,
83
string(info.Status.Current), info.Status.Message,
91
type filesystemAttachmentInfo struct {
96
MachineFilesystemAttachment
102
type filesystemAttachmentInfos []filesystemAttachmentInfo
104
func (v filesystemAttachmentInfos) Len() int {
108
func (v filesystemAttachmentInfos) Swap(i, j int) {
109
v[i], v[j] = v[j], v[i]
112
func (v filesystemAttachmentInfos) Less(i, j int) bool {
113
switch compareStrings(v[i].MachineId, v[j].MachineId) {
120
switch compareSlashSeparated(v[i].UnitId, v[j].UnitId) {
127
switch compareSlashSeparated(v[i].Storage, v[j].Storage) {
134
return v[i].FilesystemId < v[j].FilesystemId