github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/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" 6 ) 7 8 const ( 9 defaultCheckpointFormat = "table {{.Name}}" 10 11 checkpointNameHeader = "CHECKPOINT NAME" 12 ) 13 14 // NewFormat returns a format for use with a checkpoint Context 15 func NewFormat(source string) formatter.Format { 16 switch source { 17 case formatter.TableFormatKey: 18 return defaultCheckpointFormat 19 } 20 return formatter.Format(source) 21 } 22 23 // FormatWrite writes formatted checkpoints using the Context 24 func FormatWrite(ctx formatter.Context, checkpoints []types.Checkpoint) error { 25 render := func(format func(subContext formatter.SubContext) error) error { 26 for _, checkpoint := range checkpoints { 27 if err := format(&checkpointContext{c: checkpoint}); err != nil { 28 return err 29 } 30 } 31 return nil 32 } 33 return ctx.Write(newCheckpointContext(), render) 34 } 35 36 type checkpointContext struct { 37 formatter.HeaderContext 38 c types.Checkpoint 39 } 40 41 func newCheckpointContext() *checkpointContext { 42 cpCtx := checkpointContext{} 43 cpCtx.Header = formatter.SubHeaderContext{ 44 "Name": checkpointNameHeader, 45 } 46 return &cpCtx 47 } 48 49 func (c *checkpointContext) MarshalJSON() ([]byte, error) { 50 return formatter.MarshalJSON(c) 51 } 52 53 func (c *checkpointContext) Name() string { 54 return c.c.Name 55 }