7 "github.com/docker/docker/api/types"
8 units "github.com/docker/go-units"
12 defaultVolumeQuietFormat = "{{.Name}}"
13 defaultVolumeTableFormat = "table {{.Driver}}\t{{.Name}}"
15 volumeNameHeader = "VOLUME NAME"
16 mountpointHeader = "MOUNTPOINT"
21 // NewVolumeFormat returns a format for use with a volume Context
22 func NewVolumeFormat(source string, quiet bool) Format {
26 return defaultVolumeQuietFormat
28 return defaultVolumeTableFormat
31 return `name: {{.Name}}`
33 return `name: {{.Name}}\ndriver: {{.Driver}}\n`
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 {
48 return ctx.Write(newVolumeContext(), render)
51 type volumeHeaderContext map[string]string
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])
61 type volumeContext struct {
66 func newVolumeContext() *volumeContext {
67 volumeCtx := volumeContext{}
68 volumeCtx.header = volumeHeaderContext{
69 "Name": volumeNameHeader,
70 "Driver": driverHeader,
72 "Mountpoint": mountpointHeader,
73 "Labels": labelsHeader,
80 func (c *volumeContext) MarshalJSON() ([]byte, error) {
84 func (c *volumeContext) Name() string {
88 func (c *volumeContext) Driver() string {
92 func (c *volumeContext) Scope() string {
96 func (c *volumeContext) Mountpoint() string {
100 func (c *volumeContext) Labels() string {
101 if c.v.Labels == nil {
105 var joinLabels []string
106 for k, v := range c.v.Labels {
107 joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
109 return strings.Join(joinLabels, ",")
112 func (c *volumeContext) Label(name string) string {
113 if c.v.Labels == nil {
116 return c.v.Labels[name]
119 func (c *volumeContext) Links() string {
120 if c.v.UsageData == nil {
123 return fmt.Sprintf("%d", c.v.UsageData.RefCount)
126 func (c *volumeContext) Size() string {
127 if c.v.UsageData == nil {
130 return units.HumanSize(float64(c.v.UsageData.Size))