Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / formatter / volume.go
1 package formatter
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/docker/docker/api/types"
8         units "github.com/docker/go-units"
9 )
10
11 const (
12         defaultVolumeQuietFormat = "{{.Name}}"
13         defaultVolumeTableFormat = "table {{.Driver}}\t{{.Name}}"
14
15         volumeNameHeader = "VOLUME NAME"
16         mountpointHeader = "MOUNTPOINT"
17         linksHeader      = "LINKS"
18         // Status header ?
19 )
20
21 // NewVolumeFormat returns a format for use with a volume Context
22 func NewVolumeFormat(source string, quiet bool) Format {
23         switch source {
24         case TableFormatKey:
25                 if quiet {
26                         return defaultVolumeQuietFormat
27                 }
28                 return defaultVolumeTableFormat
29         case RawFormatKey:
30                 if quiet {
31                         return `name: {{.Name}}`
32                 }
33                 return `name: {{.Name}}\ndriver: {{.Driver}}\n`
34         }
35         return Format(source)
36 }
37
38 // VolumeWrite writes formatted volumes using the Context
39 func VolumeWrite(ctx Context, volumes []*types.Volume) error {
40         render := func(format func(subContext subContext) error) error {
41                 for _, volume := range volumes {
42                         if err := format(&volumeContext{v: *volume}); err != nil {
43                                 return err
44                         }
45                 }
46                 return nil
47         }
48         return ctx.Write(newVolumeContext(), render)
49 }
50
51 type volumeHeaderContext map[string]string
52
53 func (c volumeHeaderContext) Label(name string) string {
54         n := strings.Split(name, ".")
55         r := strings.NewReplacer("-", " ", "_", " ")
56         h := r.Replace(n[len(n)-1])
57
58         return h
59 }
60
61 type volumeContext struct {
62         HeaderContext
63         v types.Volume
64 }
65
66 func newVolumeContext() *volumeContext {
67         volumeCtx := volumeContext{}
68         volumeCtx.header = volumeHeaderContext{
69                 "Name":       volumeNameHeader,
70                 "Driver":     driverHeader,
71                 "Scope":      scopeHeader,
72                 "Mountpoint": mountpointHeader,
73                 "Labels":     labelsHeader,
74                 "Links":      linksHeader,
75                 "Size":       sizeHeader,
76         }
77         return &volumeCtx
78 }
79
80 func (c *volumeContext) MarshalJSON() ([]byte, error) {
81         return marshalJSON(c)
82 }
83
84 func (c *volumeContext) Name() string {
85         return c.v.Name
86 }
87
88 func (c *volumeContext) Driver() string {
89         return c.v.Driver
90 }
91
92 func (c *volumeContext) Scope() string {
93         return c.v.Scope
94 }
95
96 func (c *volumeContext) Mountpoint() string {
97         return c.v.Mountpoint
98 }
99
100 func (c *volumeContext) Labels() string {
101         if c.v.Labels == nil {
102                 return ""
103         }
104
105         var joinLabels []string
106         for k, v := range c.v.Labels {
107                 joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
108         }
109         return strings.Join(joinLabels, ",")
110 }
111
112 func (c *volumeContext) Label(name string) string {
113         if c.v.Labels == nil {
114                 return ""
115         }
116         return c.v.Labels[name]
117 }
118
119 func (c *volumeContext) Links() string {
120         if c.v.UsageData == nil {
121                 return "N/A"
122         }
123         return fmt.Sprintf("%d", c.v.UsageData.RefCount)
124 }
125
126 func (c *volumeContext) Size() string {
127         if c.v.UsageData == nil {
128                 return "N/A"
129         }
130         return units.HumanSize(float64(c.v.UsageData.Size))
131 }