2018-10-23 11:05:44 -04:00
|
|
|
package plugin
|
2016-11-22 19:23:21 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2016-11-22 19:23:21 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPluginTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Description}}\t{{.Enabled}}"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
enabledHeader = "ENABLED"
|
|
|
|
pluginIDHeader = "ID"
|
2016-11-22 19:23:21 -05:00
|
|
|
)
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// NewFormat returns a Format for rendering using a plugin Context
|
|
|
|
func NewFormat(source string, quiet bool) formatter.Format {
|
2016-11-22 19:23:21 -05:00
|
|
|
switch source {
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2016-11-22 19:23:21 -05:00
|
|
|
if quiet {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.DefaultQuietFormat
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
return defaultPluginTableFormat
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.RawFormatKey:
|
2016-11-22 19:23:21 -05:00
|
|
|
if quiet {
|
|
|
|
return `plugin_id: {{.ID}}`
|
|
|
|
}
|
|
|
|
return `plugin_id: {{.ID}}\nname: {{.Name}}\ndescription: {{.Description}}\nenabled: {{.Enabled}}\n`
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// FormatWrite writes the context
|
|
|
|
func FormatWrite(ctx formatter.Context, plugins []*types.Plugin) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2016-11-22 19:23:21 -05:00
|
|
|
for _, plugin := range plugins {
|
|
|
|
pluginCtx := &pluginContext{trunc: ctx.Trunc, p: *plugin}
|
|
|
|
if err := format(pluginCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
pluginCtx := pluginContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
pluginCtx.Header = formatter.SubHeaderContext{
|
2017-02-03 19:48:46 -05:00
|
|
|
"ID": pluginIDHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Name": formatter.NameHeader,
|
|
|
|
"Description": formatter.DescriptionHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Enabled": enabledHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"PluginReference": formatter.ImageHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
}
|
|
|
|
return ctx.Write(&pluginCtx, render)
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type pluginContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2016-11-22 19:23:21 -05:00
|
|
|
trunc bool
|
|
|
|
p types.Plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *pluginContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *pluginContext) ID() string {
|
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.p.ID)
|
|
|
|
}
|
|
|
|
return c.p.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *pluginContext) Name() string {
|
|
|
|
return c.p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *pluginContext) Description() string {
|
|
|
|
desc := strings.Replace(c.p.Config.Description, "\n", "", -1)
|
|
|
|
desc = strings.Replace(desc, "\r", "", -1)
|
|
|
|
if c.trunc {
|
2018-10-23 11:05:44 -04:00
|
|
|
desc = formatter.Ellipsis(desc, 45)
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *pluginContext) Enabled() bool {
|
|
|
|
return c.p.Enabled
|
|
|
|
}
|
2017-01-28 19:54:32 -05:00
|
|
|
|
|
|
|
func (c *pluginContext) PluginReference() string {
|
|
|
|
return c.p.PluginReference
|
|
|
|
}
|