2018-10-23 11:05:44 -04:00
|
|
|
package network
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2016-09-08 13:11:39 -04:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// NewFormat returns a Format for rendering using a network Context
|
|
|
|
func NewFormat(source string, quiet bool) formatter.Format {
|
2016-09-12 16:59:18 -04:00
|
|
|
switch source {
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.TableFormatKey:
|
2016-09-12 16:59:18 -04:00
|
|
|
if quiet {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.DefaultQuietFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return defaultNetworkTableFormat
|
2018-10-23 11:05:44 -04:00
|
|
|
case formatter.RawFormatKey:
|
2016-09-12 16:59:18 -04:00
|
|
|
if quiet {
|
|
|
|
return `network_id: {{.ID}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.Format(source)
|
2016-09-12 16:59:18 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// FormatWrite writes the context
|
|
|
|
func FormatWrite(ctx formatter.Context, networks []types.NetworkResource) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
for _, network := range networks {
|
|
|
|
networkCtx := &networkContext{trunc: ctx.Trunc, n: network}
|
|
|
|
if err := format(networkCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
networkCtx := networkContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
networkCtx.Header = formatter.SubHeaderContext{
|
2017-02-03 19:48:46 -05:00
|
|
|
"ID": networkIDHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Name": formatter.NameHeader,
|
|
|
|
"Driver": formatter.DriverHeader,
|
|
|
|
"Scope": formatter.ScopeHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"IPv6": ipv6Header,
|
|
|
|
"Internal": internalHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Labels": formatter.LabelsHeader,
|
|
|
|
"CreatedAt": formatter.CreatedAtHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
}
|
|
|
|
return ctx.Write(&networkCtx, render)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
type networkContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc bool
|
|
|
|
n types.NetworkResource
|
|
|
|
}
|
|
|
|
|
2016-09-13 03:01:31 -04:00
|
|
|
func (c *networkContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return formatter.MarshalJSON(c)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
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 {
|
2023-06-12 12:19:50 -04:00
|
|
|
joinLabels = append(joinLabels, k+"="+v)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return strings.Join(joinLabels, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Label(name string) string {
|
|
|
|
if c.n.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.n.Labels[name]
|
|
|
|
}
|
2017-01-04 18:17:54 -05:00
|
|
|
|
|
|
|
func (c *networkContext) CreatedAt() string {
|
|
|
|
return c.n.Created.String()
|
|
|
|
}
|