2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultVolumeQuietFormat = "{{.Name}}"
|
|
|
|
defaultVolumeTableFormat = "table {{.Driver}}\t{{.Name}}"
|
|
|
|
|
|
|
|
mountpointHeader = "MOUNTPOINT"
|
|
|
|
// Status header ?
|
|
|
|
)
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// NewVolumeFormat returns a format for use with a volume Context
|
|
|
|
func NewVolumeFormat(source string, quiet bool) Format {
|
|
|
|
switch source {
|
|
|
|
case TableFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return defaultVolumeQuietFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return defaultVolumeTableFormat
|
|
|
|
case RawFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return `name: {{.Name}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return `name: {{.Name}}\ndriver: {{.Driver}}\n`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(source)
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// VolumeWrite writes formatted volumes using the Context
|
|
|
|
func VolumeWrite(ctx Context, volumes []*types.Volume) error {
|
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
for _, volume := range volumes {
|
2016-09-13 14:21:07 -04:00
|
|
|
if err := format(&volumeContext{v: *volume}); err != nil {
|
2016-09-12 16:59:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return ctx.Write(&volumeContext{}, render)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type volumeContext struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
HeaderContext
|
2016-09-13 14:21:07 -04:00
|
|
|
v types.Volume
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Name() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(nameHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Driver() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(driverHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.v.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Scope() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(scopeHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.v.Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Mountpoint() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(mountpointHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.v.Mountpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Labels() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(labelsHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.v.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var joinLabels []string
|
|
|
|
for k, v := range c.v.Labels {
|
|
|
|
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
return strings.Join(joinLabels, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Label(name string) string {
|
|
|
|
|
|
|
|
n := strings.Split(name, ".")
|
|
|
|
r := strings.NewReplacer("-", " ", "_", " ")
|
|
|
|
h := r.Replace(n[len(n)-1])
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(h)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if c.v.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.v.Labels[name]
|
|
|
|
}
|