2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
2021-02-23 09:23:53 -05:00
|
|
|
"fmt"
|
2022-04-29 13:26:50 -04:00
|
|
|
"strconv"
|
2016-09-08 13:11:39 -04:00
|
|
|
"strings"
|
|
|
|
|
2022-04-29 13:26:50 -04:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
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}}"
|
|
|
|
|
2021-02-23 09:23:53 -05:00
|
|
|
idHeader = "ID"
|
|
|
|
volumeNameHeader = "VOLUME NAME"
|
|
|
|
mountpointHeader = "MOUNTPOINT"
|
|
|
|
linksHeader = "LINKS"
|
|
|
|
groupHeader = "GROUP"
|
|
|
|
availabilityHeader = "AVAILABILITY"
|
|
|
|
statusHeader = "STATUS"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
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
|
2022-04-29 13:26:50 -04:00
|
|
|
func VolumeWrite(ctx Context, volumes []*volume.Volume) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext SubContext) error) error {
|
2022-04-29 13:26:50 -04:00
|
|
|
for _, vol := range volumes {
|
|
|
|
if err := format(&volumeContext{v: *vol}); 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
|
2022-04-29 13:26:50 -04:00
|
|
|
v volume.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{
|
2021-02-23 09:23:53 -05:00
|
|
|
"ID": idHeader,
|
|
|
|
"Name": volumeNameHeader,
|
|
|
|
"Group": groupHeader,
|
|
|
|
"Driver": DriverHeader,
|
|
|
|
"Scope": ScopeHeader,
|
|
|
|
"Availability": availabilityHeader,
|
|
|
|
"Mountpoint": mountpointHeader,
|
|
|
|
"Labels": LabelsHeader,
|
|
|
|
"Links": linksHeader,
|
|
|
|
"Size": SizeHeader,
|
|
|
|
"Status": statusHeader,
|
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 {
|
2022-04-29 13:26:50 -04:00
|
|
|
joinLabels = append(joinLabels, k+"="+v)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
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"
|
|
|
|
}
|
2022-04-29 13:26:50 -04:00
|
|
|
return strconv.FormatInt(c.v.UsageData.RefCount, 10)
|
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
|
|
|
}
|
2021-02-23 09:23:53 -05:00
|
|
|
|
|
|
|
func (c *volumeContext) Group() string {
|
|
|
|
if c.v.ClusterVolume == nil {
|
|
|
|
return "N/A"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.v.ClusterVolume.Spec.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Availability() string {
|
|
|
|
if c.v.ClusterVolume == nil {
|
|
|
|
return "N/A"
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(c.v.ClusterVolume.Spec.Availability)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *volumeContext) Status() string {
|
|
|
|
if c.v.ClusterVolume == nil {
|
|
|
|
return "N/A"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.v.ClusterVolume.Info == nil || c.v.ClusterVolume.Info.VolumeID == "" {
|
|
|
|
return "pending creation"
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(c.v.ClusterVolume.PublishStatus)
|
|
|
|
switch l {
|
|
|
|
case 0:
|
|
|
|
return "created"
|
|
|
|
case 1:
|
|
|
|
return "in use (1 node)"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("in use (%d nodes)", l)
|
|
|
|
}
|
|
|
|
}
|