2016-08-04 08:59:55 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-08-23 19:37:37 -04:00
|
|
|
units "github.com/docker/go-units"
|
2016-08-04 08:59:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultVolumeQuietFormat = "{{.Name}}"
|
|
|
|
defaultVolumeTableFormat = "table {{.Driver}}\t{{.Name}}"
|
|
|
|
|
2016-10-17 14:03:31 -04:00
|
|
|
volumeNameHeader = "VOLUME NAME"
|
2016-08-04 08:59:55 -04:00
|
|
|
mountpointHeader = "MOUNTPOINT"
|
2016-08-23 19:37:37 -04:00
|
|
|
linksHeader = "LINKS"
|
2016-08-04 08:59:55 -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-08-04 08:59:55 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return defaultVolumeTableFormat
|
|
|
|
case RawFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return `name: {{.Name}}`
|
2016-08-04 08:59:55 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return `name: {{.Name}}\ndriver: {{.Driver}}\n`
|
2016-08-04 08:59:55 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(source)
|
|
|
|
}
|
2016-08-04 08:59:55 -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-08-04 08:59:55 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return nil
|
2016-08-04 08:59:55 -04:00
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
return ctx.Write(newVolumeContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type volumeHeaderContext map[string]string
|
|
|
|
|
|
|
|
func (c volumeHeaderContext) Label(name string) string {
|
|
|
|
n := strings.Split(name, ".")
|
|
|
|
r := strings.NewReplacer("-", " ", "_", " ")
|
|
|
|
h := r.Replace(n[len(n)-1])
|
|
|
|
|
|
|
|
return h
|
2016-08-04 08:59:55 -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-08-04 08:59:55 -04:00
|
|
|
}
|
|
|
|
|
2017-02-03 19:48:46 -05:00
|
|
|
func newVolumeContext() *volumeContext {
|
|
|
|
volumeCtx := volumeContext{}
|
|
|
|
volumeCtx.header = volumeHeaderContext{
|
|
|
|
"Name": volumeNameHeader,
|
|
|
|
"Driver": driverHeader,
|
|
|
|
"Scope": scopeHeader,
|
|
|
|
"Mountpoint": mountpointHeader,
|
|
|
|
"Labels": labelsHeader,
|
|
|
|
"Links": linksHeader,
|
|
|
|
"Size": sizeHeader,
|
|
|
|
}
|
|
|
|
return &volumeCtx
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (c *volumeContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return marshalJSON(c)
|
|
|
|
}
|
|
|
|
|
2016-08-04 08:59:55 -04:00
|
|
|
func (c *volumeContext) Name() string {
|
|
|
|
return c.v.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Driver() string {
|
|
|
|
return c.v.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Scope() string {
|
|
|
|
return c.v.Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Mountpoint() string {
|
|
|
|
return c.v.Mountpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Labels() string {
|
|
|
|
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 {
|
|
|
|
if c.v.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.v.Labels[name]
|
|
|
|
}
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
func (c *volumeContext) Links() string {
|
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 {
|
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
|
|
|
}
|