2018-10-23 11:05:44 -04:00
|
|
|
package checkpoint
|
2017-03-13 17:19:46 -04:00
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
import (
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
)
|
2017-03-13 17:19:46 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
defaultCheckpointFormat = "table {{.Name}}"
|
|
|
|
|
|
|
|
checkpointNameHeader = "CHECKPOINT NAME"
|
|
|
|
)
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// NewFormat returns a format for use with a checkpoint Context
|
|
|
|
func NewFormat(source string) formatter.Format {
|
2017-03-13 17:19:46 -04:00
|
|
|
switch source {
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2017-03-13 17:19:46 -04:00
|
|
|
return defaultCheckpointFormat
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2017-03-13 17:19:46 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// FormatWrite writes formatted checkpoints using the Context
|
|
|
|
func FormatWrite(ctx formatter.Context, checkpoints []types.Checkpoint) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-03-13 17:19:46 -04:00
|
|
|
for _, checkpoint := range checkpoints {
|
|
|
|
if err := format(&checkpointContext{c: checkpoint}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newCheckpointContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type checkpointContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-03-13 17:19:46 -04:00
|
|
|
c types.Checkpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCheckpointContext() *checkpointContext {
|
|
|
|
cpCtx := checkpointContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
cpCtx.Header = formatter.SubHeaderContext{
|
2017-03-13 17:19:46 -04:00
|
|
|
"Name": checkpointNameHeader,
|
|
|
|
}
|
|
|
|
return &cpCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checkpointContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2017-03-13 17:19:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checkpointContext) Name() string {
|
|
|
|
return c.c.Name
|
|
|
|
}
|