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"
|
2023-08-28 06:06:35 -04:00
|
|
|
"github.com/docker/docker/api/types/checkpoint"
|
2018-10-23 11:05:44 -04:00
|
|
|
)
|
2017-03-13 17:19:46 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
defaultCheckpointFormat = "table {{.Name}}"
|
2023-08-28 06:06:35 -04:00
|
|
|
checkpointNameHeader = "CHECKPOINT NAME"
|
2017-03-13 17:19:46 -04:00
|
|
|
)
|
|
|
|
|
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
|
2023-08-28 06:06:35 -04:00
|
|
|
func FormatWrite(ctx formatter.Context, checkpoints []checkpoint.Summary) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2023-08-28 06:06:35 -04:00
|
|
|
for _, cp := range checkpoints {
|
|
|
|
if err := format(&checkpointContext{c: cp}); err != nil {
|
2017-03-13 17:19:46 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ctx.Write(newCheckpointContext(), render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type checkpointContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2023-08-28 06:06:35 -04:00
|
|
|
c checkpoint.Summary
|
2017-03-13 17:19:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|