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 {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext SubContext) error) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
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
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
return ctx.Write(newVolumeContext(), 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
|
|
|
}
|
|
|
|
|
2017-02-03 19:48:46 -05:00
|
|
|
func newVolumeContext() *volumeContext {
|
|
|
|
volumeCtx := volumeContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
volumeCtx.Header = SubHeaderContext{
|
2017-02-03 19:48:46 -05:00
|
|
|
"Name": volumeNameHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Driver": DriverHeader,
|
|
|
|
"Scope": ScopeHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Mountpoint": mountpointHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Labels": LabelsHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Links": linksHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Size": SizeHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
}
|
|
|
|
return &volumeCtx
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (c *volumeContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return MarshalJSON(c)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -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
|
|
|
}
|