github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/checkpoint/formatter.go (about) 1 package checkpoint 2 3 import ( 4 "github.com/docker/cli/cli/command/formatter" 5 "github.com/docker/docker/api/types/checkpoint" 6 ) 7 8 const ( 9 defaultCheckpointFormat = "table {{.Name}}" 10 checkpointNameHeader = "CHECKPOINT NAME" 11 ) 12 13 // NewFormat returns a format for use with a checkpoint Context 14 func NewFormat(source string) formatter.Format { 15 if source == formatter.TableFormatKey { 16 return defaultCheckpointFormat 17 } 18 return formatter.Format(source) 19 } 20 21 // FormatWrite writes formatted checkpoints using the Context 22 func FormatWrite(ctx formatter.Context, checkpoints []checkpoint.Summary) error { 23 render := func(format func(subContext formatter.SubContext) error) error { 24 for _, cp := range checkpoints { 25 if err := format(&checkpointContext{c: cp}); err != nil { 26 return err 27 } 28 } 29 return nil 30 } 31 return ctx.Write(newCheckpointContext(), render) 32 } 33 34 type checkpointContext struct { 35 formatter.HeaderContext 36 c checkpoint.Summary 37 } 38 39 func newCheckpointContext() *checkpointContext { 40 cpCtx := checkpointContext{} 41 cpCtx.Header = formatter.SubHeaderContext{ 42 "Name": checkpointNameHeader, 43 } 44 return &cpCtx 45 } 46 47 func (c *checkpointContext) MarshalJSON() ([]byte, error) { 48 return formatter.MarshalJSON(c) 49 } 50 51 func (c *checkpointContext) Name() string { 52 return c.c.Name 53 }