~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/plugins/juju-metadata/listformatter.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package main
 
5
 
 
6
import (
 
7
        "bytes"
 
8
        "fmt"
 
9
        "strings"
 
10
        "text/tabwriter"
 
11
 
 
12
        "github.com/juju/errors"
 
13
)
 
14
 
 
15
func formatMetadataListTabular(value interface{}) ([]byte, error) {
 
16
        metadata, ok := value.([]MetadataInfo)
 
17
        if !ok {
 
18
                return nil, errors.Errorf("expected value of type %T, got %T", metadata, value)
 
19
        }
 
20
        return formatMetadataTabular(metadata)
 
21
}
 
22
 
 
23
// formatMetadataTabular returns a tabular summary of cloud image metadata.
 
24
func formatMetadataTabular(metadata []MetadataInfo) ([]byte, error) {
 
25
        var out bytes.Buffer
 
26
 
 
27
        const (
 
28
                // To format things into columns.
 
29
                minwidth = 0
 
30
                tabwidth = 1
 
31
                padding  = 2
 
32
                padchar  = ' '
 
33
                flags    = 0
 
34
        )
 
35
        tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags)
 
36
        print := func(values ...string) {
 
37
                fmt.Fprintln(tw, strings.Join(values, "\t"))
 
38
        }
 
39
        print("SOURCE", "SERIES", "ARCH", "REGION", "IMAGE-ID", "STREAM", "VIRT-TYPE", "STORAGE-TYPE")
 
40
 
 
41
        for _, m := range metadata {
 
42
                print(m.Source, m.Series, m.Arch, m.Region, m.ImageId, m.Stream, m.VirtType, m.RootStorageType)
 
43
        }
 
44
        tw.Flush()
 
45
 
 
46
        return out.Bytes(), nil
 
47
}