2018-10-23 11:05:44 -04:00
|
|
|
package network
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
import (
|
golangci-lint: enable perfsprint linter
cli/compose/types/types.go:568:17: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return []byte(fmt.Sprintf("%v", e.External)), nil
^
cli/command/formatter/buildcache.go:174:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
return fmt.Sprintf("%d", c.v.UsageCount)
^
cli/command/formatter/buildcache.go:178:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.InUse)
^
cli/command/formatter/buildcache.go:182:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.Shared)
^
cli/command/formatter/image.go:259:9: fmt.Sprintf can be replaced with faster strconv.FormatInt (perfsprint)
return fmt.Sprintf("%d", c.i.Containers)
^
cli/command/formatter/tabwriter/tabwriter_test.go:698:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", x), func(b *testing.B) {
^
cli/command/formatter/tabwriter/tabwriter_test.go:720:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", h), func(b *testing.B) {
^
cli/command/image/prune.go:62:31: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all))
^
cli/command/network/formatter.go:92:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.EnableIPv6)
^
cli/command/network/formatter.go:96:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.Internal)
^
cli/command/service/formatter.go:745:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
pub = fmt.Sprintf("%d", pr.pStart)
^
cli/command/service/formatter.go:750:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
tgt = fmt.Sprintf("%d", pr.tStart)
^
cli/command/service/opts.go:49:10: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
return fmt.Sprintf("%v", *i.value)
^
cli/compose/loader/loader.go:720:36: fmt.Sprint can be replaced with faster strconv.Itoa (perfsprint)
v, err := toServicePortConfigs(fmt.Sprint(value))
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 10:18:19 -05:00
|
|
|
"strconv"
|
2016-09-08 13:11:39 -04:00
|
|
|
"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 {
|
golangci-lint: enable perfsprint linter
cli/compose/types/types.go:568:17: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return []byte(fmt.Sprintf("%v", e.External)), nil
^
cli/command/formatter/buildcache.go:174:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
return fmt.Sprintf("%d", c.v.UsageCount)
^
cli/command/formatter/buildcache.go:178:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.InUse)
^
cli/command/formatter/buildcache.go:182:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.Shared)
^
cli/command/formatter/image.go:259:9: fmt.Sprintf can be replaced with faster strconv.FormatInt (perfsprint)
return fmt.Sprintf("%d", c.i.Containers)
^
cli/command/formatter/tabwriter/tabwriter_test.go:698:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", x), func(b *testing.B) {
^
cli/command/formatter/tabwriter/tabwriter_test.go:720:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", h), func(b *testing.B) {
^
cli/command/image/prune.go:62:31: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all))
^
cli/command/network/formatter.go:92:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.EnableIPv6)
^
cli/command/network/formatter.go:96:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.Internal)
^
cli/command/service/formatter.go:745:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
pub = fmt.Sprintf("%d", pr.pStart)
^
cli/command/service/formatter.go:750:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
tgt = fmt.Sprintf("%d", pr.tStart)
^
cli/command/service/opts.go:49:10: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
return fmt.Sprintf("%v", *i.value)
^
cli/compose/loader/loader.go:720:36: fmt.Sprint can be replaced with faster strconv.Itoa (perfsprint)
v, err := toServicePortConfigs(fmt.Sprint(value))
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 10:18:19 -05:00
|
|
|
return strconv.FormatBool(c.n.EnableIPv6)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Internal() string {
|
golangci-lint: enable perfsprint linter
cli/compose/types/types.go:568:17: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return []byte(fmt.Sprintf("%v", e.External)), nil
^
cli/command/formatter/buildcache.go:174:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
return fmt.Sprintf("%d", c.v.UsageCount)
^
cli/command/formatter/buildcache.go:178:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.InUse)
^
cli/command/formatter/buildcache.go:182:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%t", c.v.Shared)
^
cli/command/formatter/image.go:259:9: fmt.Sprintf can be replaced with faster strconv.FormatInt (perfsprint)
return fmt.Sprintf("%d", c.i.Containers)
^
cli/command/formatter/tabwriter/tabwriter_test.go:698:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", x), func(b *testing.B) {
^
cli/command/formatter/tabwriter/tabwriter_test.go:720:9: fmt.Sprintf can be replaced with faster strconv.Itoa (perfsprint)
b.Run(fmt.Sprintf("%d", h), func(b *testing.B) {
^
cli/command/image/prune.go:62:31: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
pruneFilters.Add("dangling", fmt.Sprintf("%v", !options.all))
^
cli/command/network/formatter.go:92:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.EnableIPv6)
^
cli/command/network/formatter.go:96:9: fmt.Sprintf can be replaced with faster strconv.FormatBool (perfsprint)
return fmt.Sprintf("%v", c.n.Internal)
^
cli/command/service/formatter.go:745:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
pub = fmt.Sprintf("%d", pr.pStart)
^
cli/command/service/formatter.go:750:9: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
tgt = fmt.Sprintf("%d", pr.tStart)
^
cli/command/service/opts.go:49:10: fmt.Sprintf can be replaced with faster strconv.FormatUint (perfsprint)
return fmt.Sprintf("%v", *i.value)
^
cli/compose/loader/loader.go:720:36: fmt.Sprint can be replaced with faster strconv.Itoa (perfsprint)
v, err := toServicePortConfigs(fmt.Sprint(value))
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 10:18:19 -05:00
|
|
|
return strconv.FormatBool(c.n.Internal)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *networkContext) Labels() string {
|
|
|
|
if c.n.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:07:29 -04:00
|
|
|
joinLabels := make([]string, 0, len(c.n.Labels))
|
2016-09-08 13:11:39 -04:00
|
|
|
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()
|
|
|
|
}
|