add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
2024-06-18 06:07:25 -04:00
|
|
|
//go:build go1.21
|
add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-23 10:24:43 -04:00
|
|
|
"sort"
|
2016-09-08 13:11:39 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-30 18:36:58 -04:00
|
|
|
"github.com/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-06-23 10:24:43 -04:00
|
|
|
"github.com/docker/go-units"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-03 19:48:46 -05:00
|
|
|
defaultContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
namesHeader = "NAMES"
|
|
|
|
commandHeader = "COMMAND"
|
|
|
|
runningForHeader = "CREATED"
|
|
|
|
mountsHeader = "MOUNTS"
|
|
|
|
localVolumes = "LOCAL VOLUMES"
|
|
|
|
networksHeader = "NETWORKS"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// NewContainerFormat returns a Format for rendering using a Context
|
|
|
|
func NewContainerFormat(source string, quiet bool, size bool) Format {
|
|
|
|
switch source {
|
2022-05-30 21:33:19 -04:00
|
|
|
case TableFormatKey, "": // table formatting is the default if none is set.
|
2016-09-12 16:59:18 -04:00
|
|
|
if quiet {
|
2018-10-23 11:05:44 -04:00
|
|
|
return DefaultQuietFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
format := defaultContainerTableFormat
|
|
|
|
if size {
|
|
|
|
format += `\t{{.Size}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(format)
|
|
|
|
case RawFormatKey:
|
|
|
|
if quiet {
|
|
|
|
return `container_id: {{.ID}}`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-13 14:21:07 -04:00
|
|
|
format := `container_id: {{.ID}}
|
|
|
|
image: {{.Image}}
|
|
|
|
command: {{.Command}}
|
|
|
|
created_at: {{.CreatedAt}}
|
2019-07-12 07:10:08 -04:00
|
|
|
state: {{- pad .State 1 0}}
|
2016-09-13 14:21:07 -04:00
|
|
|
status: {{- pad .Status 1 0}}
|
|
|
|
names: {{.Names}}
|
|
|
|
labels: {{- pad .Labels 1 0}}
|
|
|
|
ports: {{- pad .Ports 1 0}}
|
|
|
|
`
|
2016-09-12 16:59:18 -04:00
|
|
|
if size {
|
|
|
|
format += `size: {{.Size}}\n`
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return Format(format)
|
2022-05-30 21:33:19 -04:00
|
|
|
default: // custom format
|
2022-06-07 04:14:31 -04:00
|
|
|
if quiet {
|
|
|
|
return DefaultQuietFormat
|
|
|
|
}
|
2022-05-30 21:33:19 -04:00
|
|
|
return Format(source)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// ContainerWrite renders the context for a list of containers
|
|
|
|
func ContainerWrite(ctx Context, containers []types.Container) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext SubContext) error) error {
|
2024-07-03 10:14:40 -04:00
|
|
|
for _, ctr := range containers {
|
|
|
|
err := format(&ContainerContext{trunc: ctx.Trunc, c: ctr})
|
2016-09-12 16:59:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-24 10:30:23 -05:00
|
|
|
return ctx.Write(NewContainerContext(), render)
|
2017-02-03 19:48:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// ContainerContext is a struct used for rendering a list of containers in a Go template.
|
|
|
|
type ContainerContext struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
HeaderContext
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc bool
|
|
|
|
c types.Container
|
2019-12-24 10:30:23 -05:00
|
|
|
|
|
|
|
// FieldsUsed is used in the pre-processing step to detect which fields are
|
|
|
|
// used in the template. It's currently only used to detect use of the .Size
|
|
|
|
// field which (if used) automatically sets the '--size' option when making
|
|
|
|
// the API call.
|
2023-11-20 12:04:36 -05:00
|
|
|
FieldsUsed map[string]any
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// NewContainerContext creates a new context for rendering containers
|
|
|
|
func NewContainerContext() *ContainerContext {
|
|
|
|
containerCtx := ContainerContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
containerCtx.Header = SubHeaderContext{
|
|
|
|
"ID": ContainerIDHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Names": namesHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Image": ImageHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Command": commandHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"CreatedAt": CreatedAtHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"RunningFor": runningForHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Ports": PortsHeader,
|
2019-07-12 07:10:08 -04:00
|
|
|
"State": StateHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"Status": StatusHeader,
|
|
|
|
"Size": SizeHeader,
|
|
|
|
"Labels": LabelsHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Mounts": mountsHeader,
|
|
|
|
"LocalVolumes": localVolumes,
|
|
|
|
"Networks": networksHeader,
|
|
|
|
}
|
|
|
|
return &containerCtx
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// MarshalJSON makes ContainerContext implement json.Marshaler
|
|
|
|
func (c *ContainerContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return MarshalJSON(c)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// ID returns the container's ID as a string. Depending on the `--no-trunc`
|
|
|
|
// option being set, the full or truncated ID is returned.
|
|
|
|
func (c *ContainerContext) ID() string {
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.c.ID)
|
|
|
|
}
|
|
|
|
return c.c.ID
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Names returns a comma-separated string of the container's names, with their
|
|
|
|
// slash (/) prefix stripped. Additional names for the container (related to the
|
|
|
|
// legacy `--link` feature) are omitted.
|
|
|
|
func (c *ContainerContext) Names() string {
|
2022-03-30 09:27:25 -04:00
|
|
|
names := StripNamePrefix(c.c.Names)
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.trunc {
|
|
|
|
for _, name := range names {
|
|
|
|
if len(strings.Split(name, "/")) == 1 {
|
|
|
|
names = []string{name}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(names, ",")
|
|
|
|
}
|
|
|
|
|
2022-03-30 09:27:25 -04:00
|
|
|
// StripNamePrefix removes prefix from string, typically container names as returned by `ContainersList` API
|
|
|
|
func StripNamePrefix(ss []string) []string {
|
|
|
|
sss := make([]string, len(ss))
|
|
|
|
for i, s := range ss {
|
|
|
|
sss[i] = s[1:]
|
|
|
|
}
|
|
|
|
return sss
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Image returns the container's image reference. If the trunc option is set,
|
|
|
|
// the image's registry digest can be included.
|
|
|
|
func (c *ContainerContext) Image() string {
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.c.Image == "" {
|
|
|
|
return "<no image>"
|
|
|
|
}
|
|
|
|
if c.trunc {
|
|
|
|
if trunc := stringid.TruncateID(c.c.ImageID); trunc == stringid.TruncateID(c.c.Image) {
|
|
|
|
return trunc
|
|
|
|
}
|
2017-02-08 17:15:32 -05:00
|
|
|
// truncate digest if no-trunc option was not selected
|
|
|
|
ref, err := reference.ParseNormalizedNamed(c.c.Image)
|
|
|
|
if err == nil {
|
|
|
|
if nt, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
// case for when a tag is provided
|
|
|
|
if namedTagged, err := reference.WithTag(reference.TrimNamed(nt), nt.Tag()); err == nil {
|
|
|
|
return reference.FamiliarString(namedTagged)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// case for when a tag is not provided
|
|
|
|
named := reference.TrimNamed(ref)
|
|
|
|
return reference.FamiliarString(named)
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-02-08 17:15:32 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.c.Image
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Command returns's the container's command. If the trunc option is set, the
|
|
|
|
// returned command is truncated (ellipsized).
|
|
|
|
func (c *ContainerContext) Command() string {
|
2016-09-08 13:11:39 -04:00
|
|
|
command := c.c.Command
|
|
|
|
if c.trunc {
|
2017-10-13 05:01:32 -04:00
|
|
|
command = Ellipsis(command, 20)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return strconv.Quote(command)
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// CreatedAt returns the "Created" date/time of the container as a unix timestamp.
|
|
|
|
func (c *ContainerContext) CreatedAt() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
return time.Unix(c.c.Created, 0).String()
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// RunningFor returns a human-readable representation of the duration for which
|
|
|
|
// the container has been running.
|
|
|
|
//
|
|
|
|
// Note that this duration is calculated on the client, and as such is influenced
|
|
|
|
// by clock skew between the client and the daemon.
|
|
|
|
func (c *ContainerContext) RunningFor() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
createdAt := time.Unix(c.c.Created, 0)
|
2017-02-03 19:48:46 -05:00
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Ports returns a comma-separated string representing open ports of the container
|
|
|
|
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
|
|
|
|
// it's used by command 'docker ps'
|
|
|
|
// Both published and exposed ports are included.
|
|
|
|
func (c *ContainerContext) Ports() string {
|
2017-06-23 10:24:43 -04:00
|
|
|
return DisplayablePorts(c.c.Ports)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// State returns the container's current state (e.g. "running" or "paused")
|
|
|
|
func (c *ContainerContext) State() string {
|
2019-07-12 07:10:08 -04:00
|
|
|
return c.c.State
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Status returns the container's status in a human readable form (for example,
|
|
|
|
// "Up 24 hours" or "Exited (0) 8 days ago")
|
|
|
|
func (c *ContainerContext) Status() string {
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.c.Status
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Size returns the container's size and virtual size (e.g. "2B (virtual 21.5MB)")
|
|
|
|
func (c *ContainerContext) Size() string {
|
|
|
|
if c.FieldsUsed == nil {
|
2023-11-20 12:04:36 -05:00
|
|
|
c.FieldsUsed = map[string]any{}
|
2019-12-24 10:30:23 -05:00
|
|
|
}
|
|
|
|
c.FieldsUsed["Size"] = struct{}{}
|
2016-09-04 17:44:34 -04:00
|
|
|
srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3)
|
|
|
|
sv := units.HumanSizeWithPrecision(float64(c.c.SizeRootFs), 3)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
sf := srw
|
|
|
|
if c.c.SizeRootFs > 0 {
|
|
|
|
sf = fmt.Sprintf("%s (virtual %s)", srw, sv)
|
|
|
|
}
|
|
|
|
return sf
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Labels returns a comma-separated string of labels present on the container.
|
|
|
|
func (c *ContainerContext) Labels() string {
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.c.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:07:29 -04:00
|
|
|
joinLabels := make([]string, 0, len(c.c.Labels))
|
2016-09-08 13:11:39 -04:00
|
|
|
for k, v := range c.c.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, ",")
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Label returns the value of the label with the given name or an empty string
|
|
|
|
// if the given label does not exist.
|
|
|
|
func (c *ContainerContext) Label(name string) string {
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.c.Labels == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.c.Labels[name]
|
|
|
|
}
|
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Mounts returns a comma-separated string of mount names present on the container.
|
|
|
|
// If the trunc option is set, names can be truncated (ellipsized).
|
|
|
|
func (c *ContainerContext) Mounts() string {
|
2016-09-08 13:11:39 -04:00
|
|
|
var name string
|
2022-09-03 14:07:29 -04:00
|
|
|
mounts := make([]string, 0, len(c.c.Mounts))
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, m := range c.c.Mounts {
|
|
|
|
if m.Name == "" {
|
|
|
|
name = m.Source
|
|
|
|
} else {
|
|
|
|
name = m.Name
|
|
|
|
}
|
|
|
|
if c.trunc {
|
2017-10-13 05:01:32 -04:00
|
|
|
name = Ellipsis(name, 15)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
mounts = append(mounts, name)
|
|
|
|
}
|
|
|
|
return strings.Join(mounts, ",")
|
|
|
|
}
|
2016-08-23 19:37:37 -04:00
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// LocalVolumes returns the number of volumes using the "local" volume driver.
|
|
|
|
func (c *ContainerContext) LocalVolumes() string {
|
2016-08-23 19:37:37 -04:00
|
|
|
count := 0
|
|
|
|
for _, m := range c.c.Mounts {
|
|
|
|
if m.Driver == "local" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:07:29 -04:00
|
|
|
return strconv.Itoa(count)
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|
2016-10-19 18:09:42 -04:00
|
|
|
|
2019-12-24 10:30:23 -05:00
|
|
|
// Networks returns a comma-separated string of networks that the container is
|
|
|
|
// attached to.
|
|
|
|
func (c *ContainerContext) Networks() string {
|
2016-10-19 18:09:42 -04:00
|
|
|
if c.c.NetworkSettings == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-03 14:07:29 -04:00
|
|
|
networks := make([]string, 0, len(c.c.NetworkSettings.Networks))
|
2016-10-19 18:09:42 -04:00
|
|
|
for k := range c.c.NetworkSettings.Networks {
|
|
|
|
networks = append(networks, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(networks, ",")
|
|
|
|
}
|
2017-06-23 10:24:43 -04:00
|
|
|
|
|
|
|
// DisplayablePorts returns formatted string representing open ports of container
|
|
|
|
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
|
|
|
|
// it's used by command 'docker ps'
|
|
|
|
func DisplayablePorts(ports []types.Port) string {
|
|
|
|
type portGroup struct {
|
|
|
|
first uint16
|
|
|
|
last uint16
|
|
|
|
}
|
|
|
|
groupMap := make(map[string]*portGroup)
|
2022-09-03 14:07:29 -04:00
|
|
|
var result []string //nolint:prealloc
|
2017-06-23 10:24:43 -04:00
|
|
|
var hostMappings []string
|
|
|
|
var groupMapKeys []string
|
2018-07-06 15:49:10 -04:00
|
|
|
sort.Slice(ports, func(i, j int) bool {
|
|
|
|
return comparePorts(ports[i], ports[j])
|
|
|
|
})
|
|
|
|
|
2017-06-23 10:24:43 -04:00
|
|
|
for _, port := range ports {
|
|
|
|
current := port.PrivatePort
|
|
|
|
portKey := port.Type
|
|
|
|
if port.IP != "" {
|
|
|
|
if port.PublicPort != current {
|
|
|
|
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
|
|
|
|
continue
|
|
|
|
}
|
2022-09-03 14:07:29 -04:00
|
|
|
portKey = port.IP + "/" + port.Type
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
group := groupMap[portKey]
|
|
|
|
|
|
|
|
if group == nil {
|
|
|
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
|
|
|
// record order that groupMap keys are created
|
|
|
|
groupMapKeys = append(groupMapKeys, portKey)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if current == (group.last + 1) {
|
|
|
|
group.last = current
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, formGroup(portKey, group.first, group.last))
|
|
|
|
groupMap[portKey] = &portGroup{first: current, last: current}
|
|
|
|
}
|
|
|
|
for _, portKey := range groupMapKeys {
|
|
|
|
g := groupMap[portKey]
|
|
|
|
result = append(result, formGroup(portKey, g.first, g.last))
|
|
|
|
}
|
|
|
|
result = append(result, hostMappings...)
|
|
|
|
return strings.Join(result, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func formGroup(key string, start, last uint16) string {
|
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
groupType := parts[0]
|
|
|
|
var ip string
|
|
|
|
if len(parts) > 1 {
|
|
|
|
ip = parts[0]
|
|
|
|
groupType = parts[1]
|
|
|
|
}
|
|
|
|
group := strconv.Itoa(int(start))
|
|
|
|
if start != last {
|
|
|
|
group = fmt.Sprintf("%s-%d", group, last)
|
|
|
|
}
|
|
|
|
if ip != "" {
|
|
|
|
group = fmt.Sprintf("%s:%s->%s", ip, group, group)
|
|
|
|
}
|
2022-09-03 14:07:29 -04:00
|
|
|
return group + "/" + groupType
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
func comparePorts(i, j types.Port) bool {
|
|
|
|
if i.PrivatePort != j.PrivatePort {
|
|
|
|
return i.PrivatePort < j.PrivatePort
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
if i.IP != j.IP {
|
|
|
|
return i.IP < j.IP
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
if i.PublicPort != j.PublicPort {
|
|
|
|
return i.PublicPort < j.PublicPort
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
return i.Type < j.Type
|
2017-06-23 10:24:43 -04:00
|
|
|
}
|