1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/cli/command/formatter/network.go
Daniel Nephin 0640a14b4f Move api/client -> cli/command
Using
  gomvpkg
     -from github.com/docker/docker/api/client
     -to github.com/docker/docker/cli/command
     -vcs_mv_cmd 'git mv {{.Src}} {{.Dst}}'

Signed-off-by: Daniel Nephin <dnephin@docker.com>
2016-09-08 15:46:29 -04:00

129 lines
2.4 KiB
Go

package formatter
import (
"bytes"
"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"
)
// NetworkContext contains network specific information required by the formatter,
// encapsulate a Context struct.
type NetworkContext struct {
Context
// Networks
Networks []types.NetworkResource
}
func (ctx NetworkContext) Write() {
switch ctx.Format {
case tableFormatKey:
if ctx.Quiet {
ctx.Format = defaultQuietFormat
} else {
ctx.Format = defaultNetworkTableFormat
}
case rawFormatKey:
if ctx.Quiet {
ctx.Format = `network_id: {{.ID}}`
} else {
ctx.Format = `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n`
}
}
ctx.buffer = bytes.NewBufferString("")
ctx.preformat()
tmpl, err := ctx.parseFormat()
if err != nil {
return
}
for _, network := range ctx.Networks {
networkCtx := &networkContext{
trunc: ctx.Trunc,
n: network,
}
err = ctx.contextFormat(tmpl, networkCtx)
if err != nil {
return
}
}
ctx.postformat(tmpl, &networkContext{})
}
type networkContext struct {
baseSubContext
trunc bool
n types.NetworkResource
}
func (c *networkContext) ID() string {
c.addHeader(networkIDHeader)
if c.trunc {
return stringid.TruncateID(c.n.ID)
}
return c.n.ID
}
func (c *networkContext) Name() string {
c.addHeader(nameHeader)
return c.n.Name
}
func (c *networkContext) Driver() string {
c.addHeader(driverHeader)
return c.n.Driver
}
func (c *networkContext) Scope() string {
c.addHeader(scopeHeader)
return c.n.Scope
}
func (c *networkContext) IPv6() string {
c.addHeader(ipv6Header)
return fmt.Sprintf("%v", c.n.EnableIPv6)
}
func (c *networkContext) Internal() string {
c.addHeader(internalHeader)
return fmt.Sprintf("%v", c.n.Internal)
}
func (c *networkContext) Labels() string {
c.addHeader(labelsHeader)
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])
c.addHeader(h)
if c.n.Labels == nil {
return ""
}
return c.n.Labels[name]
}