mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00

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>
129 lines
2.7 KiB
Go
129 lines
2.7 KiB
Go
package formatter
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/pkg/stringid"
|
|
)
|
|
|
|
const (
|
|
defaultNetworkTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.Scope}}"
|
|
|
|
networkIDHeader = "NETWORK ID"
|
|
ipv6Header = "IPV6"
|
|
internalHeader = "INTERNAL"
|
|
)
|
|
|
|
// NewNetworkFormat returns a Format for rendering using a network Context
|
|
func NewNetworkFormat(source string, quiet bool) Format {
|
|
switch source {
|
|
case TableFormatKey:
|
|
if quiet {
|
|
return defaultQuietFormat
|
|
}
|
|
return defaultNetworkTableFormat
|
|
case RawFormatKey:
|
|
if quiet {
|
|
return `network_id: {{.ID}}`
|
|
}
|
|
return `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n`
|
|
}
|
|
return Format(source)
|
|
}
|
|
|
|
// NetworkWrite writes the context
|
|
func NetworkWrite(ctx Context, networks []types.NetworkResource) error {
|
|
render := func(format func(subContext subContext) error) error {
|
|
for _, network := range networks {
|
|
networkCtx := &networkContext{trunc: ctx.Trunc, n: network}
|
|
if err := format(networkCtx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
networkCtx := networkContext{}
|
|
networkCtx.header = networkHeaderContext{
|
|
"ID": networkIDHeader,
|
|
"Name": nameHeader,
|
|
"Driver": driverHeader,
|
|
"Scope": scopeHeader,
|
|
"IPv6": ipv6Header,
|
|
"Internal": internalHeader,
|
|
"Labels": labelsHeader,
|
|
"CreatedAt": createdAtHeader,
|
|
}
|
|
return ctx.Write(&networkCtx, render)
|
|
}
|
|
|
|
type networkHeaderContext map[string]string
|
|
|
|
func (c networkHeaderContext) Label(name string) string {
|
|
n := strings.Split(name, ".")
|
|
r := strings.NewReplacer("-", " ", "_", " ")
|
|
h := r.Replace(n[len(n)-1])
|
|
|
|
return h
|
|
}
|
|
|
|
type networkContext struct {
|
|
HeaderContext
|
|
trunc bool
|
|
n types.NetworkResource
|
|
}
|
|
|
|
func (c *networkContext) MarshalJSON() ([]byte, error) {
|
|
return marshalJSON(c)
|
|
}
|
|
|
|
func (c *networkContext) ID() string {
|
|
if c.trunc {
|
|
return stringid.TruncateID(c.n.ID)
|
|
}
|
|
return c.n.ID
|
|
}
|
|
|
|
func (c *networkContext) Name() string {
|
|
return c.n.Name
|
|
}
|
|
|
|
func (c *networkContext) Driver() string {
|
|
return c.n.Driver
|
|
}
|
|
|
|
func (c *networkContext) Scope() string {
|
|
return c.n.Scope
|
|
}
|
|
|
|
func (c *networkContext) IPv6() string {
|
|
return fmt.Sprintf("%v", c.n.EnableIPv6)
|
|
}
|
|
|
|
func (c *networkContext) Internal() string {
|
|
return fmt.Sprintf("%v", c.n.Internal)
|
|
}
|
|
|
|
func (c *networkContext) Labels() string {
|
|
if c.n.Labels == nil {
|
|
return ""
|
|
}
|
|
|
|
var joinLabels []string
|
|
for k, v := range c.n.Labels {
|
|
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
|
|
}
|
|
return strings.Join(joinLabels, ",")
|
|
}
|
|
|
|
func (c *networkContext) Label(name string) string {
|
|
if c.n.Labels == nil {
|
|
return ""
|
|
}
|
|
return c.n.Labels[name]
|
|
}
|
|
|
|
func (c *networkContext) CreatedAt() string {
|
|
return c.n.Created.String()
|
|
}
|