2016-09-08 13:11:39 -04:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// 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
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return defaultNetworkTableFormat
|
|
|
|
case RawFormatKey:
|
|
|
|
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
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(source)
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// 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
|
|
|
|
}
|
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
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return ctx.Write(&networkContext{}, render)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type networkContext struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
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) {
|
|
|
|
return marshalJSON(c)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func (c *networkContext) ID() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(networkIDHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.n.ID)
|
|
|
|
}
|
|
|
|
return c.n.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Name() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(nameHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.n.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Driver() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(driverHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.n.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Scope() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(scopeHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.n.Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) IPv6() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(ipv6Header)
|
2016-09-08 13:11:39 -04:00
|
|
|
return fmt.Sprintf("%v", c.n.EnableIPv6)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Internal() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(internalHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return fmt.Sprintf("%v", c.n.Internal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Labels() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(labelsHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
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 {
|
|
|
|
n := strings.Split(name, ".")
|
|
|
|
r := strings.NewReplacer("-", " ", "_", " ")
|
|
|
|
h := r.Replace(n[len(n)-1])
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(h)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if c.n.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.n.Labels[name]
|
|
|
|
}
|