Skip to content

Commit ccb3b44

Browse files
authored
Merge pull request #5480 from thaJeztah/moby_sys_capability
cli/container: use github.com/moby/sys/capability for completions
2 parents f4fab2c + 462e082 commit ccb3b44

16 files changed

Lines changed: 1529 additions & 56 deletions

File tree

cli/command/container/completion.go

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,48 @@
11
package container
22

33
import (
4+
"strings"
5+
"sync"
6+
47
"github.com/docker/cli/cli/command/completion"
58
"github.com/docker/docker/api/types/container"
9+
"github.com/moby/sys/capability"
610
"github.com/moby/sys/signal"
711
"github.com/spf13/cobra"
812
)
913

14+
// allCaps is the magic value for "all capabilities".
15+
const allCaps = "ALL"
16+
1017
// allLinuxCapabilities is a list of all known Linux capabilities.
1118
//
12-
// This list was based on the containerd pkg/cap package;
13-
// https://github.com/containerd/containerd/blob/v1.7.19/pkg/cap/cap_linux.go#L133-L181
14-
//
1519
// TODO(thaJeztah): add descriptions, and enable descriptions for our completion scripts (cobra.CompletionOptions.DisableDescriptions is currently set to "true")
16-
var allLinuxCapabilities = []string{
17-
"ALL", // magic value for "all capabilities"
18-
19-
// caps35 is the caps of kernel 3.5 (37 entries)
20-
"CAP_CHOWN", // 2.2
21-
"CAP_DAC_OVERRIDE", // 2.2
22-
"CAP_DAC_READ_SEARCH", // 2.2
23-
"CAP_FOWNER", // 2.2
24-
"CAP_FSETID", // 2.2
25-
"CAP_KILL", // 2.2
26-
"CAP_SETGID", // 2.2
27-
"CAP_SETUID", // 2.2
28-
"CAP_SETPCAP", // 2.2
29-
"CAP_LINUX_IMMUTABLE", // 2.2
30-
"CAP_NET_BIND_SERVICE", // 2.2
31-
"CAP_NET_BROADCAST", // 2.2
32-
"CAP_NET_ADMIN", // 2.2
33-
"CAP_NET_RAW", // 2.2
34-
"CAP_IPC_LOCK", // 2.2
35-
"CAP_IPC_OWNER", // 2.2
36-
"CAP_SYS_MODULE", // 2.2
37-
"CAP_SYS_RAWIO", // 2.2
38-
"CAP_SYS_CHROOT", // 2.2
39-
"CAP_SYS_PTRACE", // 2.2
40-
"CAP_SYS_PACCT", // 2.2
41-
"CAP_SYS_ADMIN", // 2.2
42-
"CAP_SYS_BOOT", // 2.2
43-
"CAP_SYS_NICE", // 2.2
44-
"CAP_SYS_RESOURCE", // 2.2
45-
"CAP_SYS_TIME", // 2.2
46-
"CAP_SYS_TTY_CONFIG", // 2.2
47-
"CAP_MKNOD", // 2.4
48-
"CAP_LEASE", // 2.4
49-
"CAP_AUDIT_WRITE", // 2.6.11
50-
"CAP_AUDIT_CONTROL", // 2.6.11
51-
"CAP_SETFCAP", // 2.6.24
52-
"CAP_MAC_OVERRIDE", // 2.6.25
53-
"CAP_MAC_ADMIN", // 2.6.25
54-
"CAP_SYSLOG", // 2.6.37
55-
"CAP_WAKE_ALARM", // 3.0
56-
"CAP_BLOCK_SUSPEND", // 3.5
57-
58-
// caps316 is the caps of kernel 3.16 (38 entries)
59-
"CAP_AUDIT_READ",
60-
61-
// caps58 is the caps of kernel 5.8 (40 entries)
62-
"CAP_PERFMON",
63-
"CAP_BPF",
64-
65-
// caps59 is the caps of kernel 5.9 (41 entries)
66-
"CAP_CHECKPOINT_RESTORE",
67-
}
20+
// TODO(thaJeztah): consider what casing we want to use for completion (see below);
21+
//
22+
// We need to consider what format is most convenient; currently we use the
23+
// canonical name (uppercase and "CAP_" prefix), however, tab-completion is
24+
// case-sensitive by default, so requires the user to type uppercase letters
25+
// to filter the list of options.
26+
//
27+
// Bash completion provides a `completion-ignore-case on` option to make completion
28+
// case-insensitive (https://askubuntu.com/a/87066), but it looks to be a global
29+
// option; the current cobra.CompletionOptions also don't provide this as an option
30+
// to be used in the generated completion-script.
31+
//
32+
// Fish completion has `smartcase` (by default?) which matches any case if
33+
// all of the input is lowercase.
34+
//
35+
// Zsh does not appear have a dedicated option, but allows setting matching-rules
36+
// (see https://superuser.com/a/1092328).
37+
var allLinuxCapabilities = sync.OnceValue(func() []string {
38+
caps := capability.ListKnown()
39+
out := make([]string, 0, len(caps)+1)
40+
out = append(out, allCaps)
41+
for _, c := range caps {
42+
out = append(out, "CAP_"+strings.ToUpper(c.String()))
43+
}
44+
return out
45+
})
6846

6947
// restartPolicies is a list of all valid restart-policies..
7048
//
@@ -77,7 +55,7 @@ var restartPolicies = []string{
7755
}
7856

7957
func completeLinuxCapabilityNames(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) {
80-
return completion.FromList(allLinuxCapabilities...)(cmd, args, toComplete)
58+
return completion.FromList(allLinuxCapabilities()...)(cmd, args, toComplete)
8159
}
8260

8361
func completeRestartPolicies(cmd *cobra.Command, args []string, toComplete string) (names []string, _ cobra.ShellCompDirective) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package container
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
"gotest.tools/v3/assert"
9+
is "gotest.tools/v3/assert/cmp"
10+
)
11+
12+
func TestCompleteLinuxCapabilityNames(t *testing.T) {
13+
names, directives := completeLinuxCapabilityNames(nil, nil, "")
14+
assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion")
15+
assert.Assert(t, len(names) > 1)
16+
assert.Check(t, names[0] == allCaps)
17+
for _, name := range names[1:] {
18+
assert.Check(t, strings.HasPrefix(name, "CAP_"))
19+
assert.Check(t, is.Equal(name, strings.ToUpper(name)), "Should be formatted uppercase")
20+
}
21+
}

vendor.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/mattn/go-runewidth v0.0.15
2727
github.com/moby/patternmatcher v0.6.0
2828
github.com/moby/swarmkit/v2 v2.0.0-20240611172349-ea1a7cec35cb
29+
github.com/moby/sys/capability v0.3.0
2930
github.com/moby/sys/sequential v0.6.0
3031
github.com/moby/sys/signal v0.7.1
3132
github.com/moby/term v0.5.0

vendor.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkV
182182
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
183183
github.com/moby/swarmkit/v2 v2.0.0-20240611172349-ea1a7cec35cb h1:1UTTg2EgO3nuyV03wREDzldqqePzQ4+0a5G1C1y1bIo=
184184
github.com/moby/swarmkit/v2 v2.0.0-20240611172349-ea1a7cec35cb/go.mod h1:kNy225f/gWAnF8wPftteMc5nbAHhrH+HUfvyjmhFjeQ=
185+
github.com/moby/sys/capability v0.3.0 h1:kEP+y6te0gEXIaeQhIi0s7vKs/w0RPoH1qPa6jROcVg=
186+
github.com/moby/sys/capability v0.3.0/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
185187
github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU=
186188
github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko=
187189
github.com/moby/sys/signal v0.7.1 h1:PrQxdvxcGijdo6UXXo/lU/TvHUWyPhj7UOpSo8tuvk0=

vendor/github.com/moby/sys/capability/.codespellrc

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/sys/capability/.golangci.yml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/sys/capability/CHANGELOG.md

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/sys/capability/LICENSE

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/sys/capability/README.md

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)