This fix is an attempt to address
https://github.com/docker/docker/pull/28213#issuecomment-273840405
Currently when specify table format with table `--format "table {{.ID}}..."`,
the delimiter in the header section of the table is always `"\t"`.
That is actually different from the content of the table as the delimiter
could be anything (or even contatenated with `.`, for example):
```
$ docker service ps web --format 'table {{.Name}}.{{.ID}}' --no-trunc
NAME ID
web.1.inyhxhvjcijl0hdbu8lgrwwh7
\_ web.1.p9m4kx2srjqmfms4igam0uqlb
```
This fix is an attampt to address the skewness of the table when delimiter
is not `"\t"`.
The basic idea is that, when header consists of `table` key, the header section
will be redendered the same way as content section. A map mapping each
placeholder name to the HEADER entry name is used for the context of the header.
Unit tests have been updated and added to cover the changes.
This fix is related to #28313.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Upstream-commit: 9dda1155f3
Component: cli
96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package formatter
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/pkg/stringid"
|
|
"github.com/docker/docker/pkg/stringutils"
|
|
)
|
|
|
|
const (
|
|
defaultPluginTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Description}}\t{{.Enabled}}"
|
|
|
|
pluginIDHeader = "ID"
|
|
descriptionHeader = "DESCRIPTION"
|
|
enabledHeader = "ENABLED"
|
|
)
|
|
|
|
// NewPluginFormat returns a Format for rendering using a plugin Context
|
|
func NewPluginFormat(source string, quiet bool) Format {
|
|
switch source {
|
|
case TableFormatKey:
|
|
if quiet {
|
|
return defaultQuietFormat
|
|
}
|
|
return defaultPluginTableFormat
|
|
case RawFormatKey:
|
|
if quiet {
|
|
return `plugin_id: {{.ID}}`
|
|
}
|
|
return `plugin_id: {{.ID}}\nname: {{.Name}}\ndescription: {{.Description}}\nenabled: {{.Enabled}}\n`
|
|
}
|
|
return Format(source)
|
|
}
|
|
|
|
// PluginWrite writes the context
|
|
func PluginWrite(ctx Context, plugins []*types.Plugin) error {
|
|
render := func(format func(subContext subContext) error) error {
|
|
for _, plugin := range plugins {
|
|
pluginCtx := &pluginContext{trunc: ctx.Trunc, p: *plugin}
|
|
if err := format(pluginCtx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
pluginCtx := pluginContext{}
|
|
pluginCtx.header = map[string]string{
|
|
"ID": pluginIDHeader,
|
|
"Name": nameHeader,
|
|
"Description": descriptionHeader,
|
|
"Enabled": enabledHeader,
|
|
"PluginReference": imageHeader,
|
|
}
|
|
return ctx.Write(&pluginCtx, render)
|
|
}
|
|
|
|
type pluginContext struct {
|
|
HeaderContext
|
|
trunc bool
|
|
p types.Plugin
|
|
}
|
|
|
|
func (c *pluginContext) MarshalJSON() ([]byte, error) {
|
|
return marshalJSON(c)
|
|
}
|
|
|
|
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 {
|
|
desc = stringutils.Ellipsis(desc, 45)
|
|
}
|
|
|
|
return desc
|
|
}
|
|
|
|
func (c *pluginContext) Enabled() bool {
|
|
return c.p.Enabled
|
|
}
|
|
|
|
func (c *pluginContext) PluginReference() string {
|
|
return c.p.PluginReference
|
|
}
|