2018-10-23 11:05:44 -04:00
|
|
|
package engine
|
2018-03-19 18:56:51 -04:00
|
|
|
|
|
|
|
import (
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2018-09-11 08:46:30 -04:00
|
|
|
clitypes "github.com/docker/cli/types"
|
2018-03-19 18:56:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultUpdatesTableFormat = "table {{.Type}}\t{{.Version}}\t{{.Notes}}"
|
|
|
|
defaultUpdatesQuietFormat = "{{.Version}}"
|
|
|
|
|
|
|
|
updatesTypeHeader = "TYPE"
|
|
|
|
versionHeader = "VERSION"
|
|
|
|
notesHeader = "NOTES"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewUpdatesFormat returns a Format for rendering using a updates context
|
2018-10-23 11:05:44 -04:00
|
|
|
func NewUpdatesFormat(source string, quiet bool) formatter.Format {
|
2018-03-19 18:56:51 -04:00
|
|
|
switch source {
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2018-03-19 18:56:51 -04:00
|
|
|
if quiet {
|
|
|
|
return defaultUpdatesQuietFormat
|
|
|
|
}
|
|
|
|
return defaultUpdatesTableFormat
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.RawFormatKey:
|
2018-03-19 18:56:51 -04:00
|
|
|
if quiet {
|
|
|
|
return `update_version: {{.Version}}`
|
|
|
|
}
|
|
|
|
return `update_version: {{.Version}}\ntype: {{.Type}}\nnotes: {{.Notes}}\n`
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatesWrite writes the context
|
2018-10-23 11:05:44 -04:00
|
|
|
func UpdatesWrite(ctx formatter.Context, availableUpdates []clitypes.Update) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2018-03-19 18:56:51 -04:00
|
|
|
for _, update := range availableUpdates {
|
|
|
|
updatesCtx := &updateContext{trunc: ctx.Trunc, u: update}
|
|
|
|
if err := format(updatesCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
updatesCtx := updateContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
updatesCtx.Header = map[string]string{
|
2018-03-19 18:56:51 -04:00
|
|
|
"Type": updatesTypeHeader,
|
|
|
|
"Version": versionHeader,
|
|
|
|
"Notes": notesHeader,
|
|
|
|
}
|
|
|
|
return ctx.Write(&updatesCtx, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2018-03-19 18:56:51 -04:00
|
|
|
trunc bool
|
2018-09-11 08:46:30 -04:00
|
|
|
u clitypes.Update
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *updateContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2018-03-19 18:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *updateContext) Type() string {
|
|
|
|
return c.u.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *updateContext) Version() string {
|
|
|
|
return c.u.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *updateContext) Notes() string {
|
|
|
|
return c.u.Notes
|
|
|
|
}
|