2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2016-08-23 19:37:37 -04:00
|
|
|
units "github.com/docker/go-units"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultVolumeQuietFormat = "{{.Name}}"
|
|
|
|
defaultVolumeTableFormat = "table {{.Driver}}\t{{.Name}}"
|
|
|
|
|
2016-10-17 14:03:31 -04:00
|
|
|
volumeNameHeader = "VOLUME NAME"
|
2016-09-08 13:11:39 -04:00
|
|
|
mountpointHeader = "MOUNTPOINT"
|
2016-08-23 19:37:37 -04:00
|
|
|
linksHeader = "LINKS"
|
2016-09-08 13:11:39 -04:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (c *volumeContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return marshalJSON(c)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func (c *volumeContext) Name() string {
|
2016-10-17 14:03:31 -04:00
|
|
|
c.AddHeader(volumeNameHeader)
|
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]
|
|
|
|
}
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
func (c *volumeContext) Links() string {
|
|
|
|
c.AddHeader(linksHeader)
|
2016-10-11 14:49:26 -04:00
|
|
|
if c.v.UsageData == nil {
|
2016-08-23 19:37:37 -04:00
|
|
|
return "N/A"
|
|
|
|
}
|
2016-10-11 14:49:26 -04:00
|
|
|
return fmt.Sprintf("%d", c.v.UsageData.RefCount)
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Size() string {
|
|
|
|
c.AddHeader(sizeHeader)
|
2016-10-11 14:49:26 -04:00
|
|
|
if c.v.UsageData == nil {
|
2016-08-23 19:37:37 -04:00
|
|
|
return "N/A"
|
|
|
|
}
|
2016-10-11 14:49:26 -04:00
|
|
|
return units.HumanSize(float64(c.v.UsageData.Size))
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|