Commit Graph

2193 Commits

Author SHA1 Message Date
Harald Albers 955e003345
Handle null completions with a default callback
Credits to thaJeztah

Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 06260e68f3)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:55 +01:00
Harald Albers ce77f4a545
Add completion for `--volume-driver`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 4525fe37b4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:54 +01:00
Harald Albers e20bcf3565
Add completion for `--cgroupns`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit db0ed1e216)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:54 +01:00
Harald Albers 78544cd587
Add completion for `--uts`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 2915749279)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:54 +01:00
Harald Albers d11291966d
Add completion for --log-driver` and --log-opt`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 3a2503fa43)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:54 +01:00
Harald Albers 25ff89519f
Add completion for `--security-opt`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 9a9ae231a9)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:54 +01:00
Harald Albers 83303411ee
Add completion for `--detach-keys`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 5f7c43e5e6)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:53 +01:00
Harald Albers e25548d1c6
Add completion for `--userns`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 3292afe6e6)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:53 +01:00
Harald Albers 99e60c4e39
Add completion for `--ulimit`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 5d709a8d9f)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:53 +01:00
Harald Albers 6d087eb91b
Add completion for `--storage-opt`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 2d89339b34)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:53 +01:00
Harald Albers 83f79c3e86
Add completion for `--pid`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit ac7bde6f64)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:04:48 +01:00
Harald Albers 6f439ea4a4
Add completion for `--link`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit e513454244)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:00:47 +01:00
Harald Albers 7e35be13ea
Add completion for `--ipc`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit c555327f0b)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:00:47 +01:00
Harald Albers f9ccc50304
Add completion for `--attach`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit b598ec8cdb)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:00:47 +01:00
Harald Albers 38e37778b0
Share the container completions
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 761d76750c)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 17:00:47 +01:00
Sebastiaan van Stijn fd53fe7f47
Merge pull request #5613 from thaJeztah/27.x_backport_bump_golangci_lint
[27.x backport] bump golangci-lint to v1.61.0 and cleanup config
2024-11-14 16:48:41 +01:00
Harald Albers 8caf347188
Add tests for completions that call the API
Some small adjustments for this branch due to some times being renamed
in master;

    63.76 cli/command/system/cmd.go:1: : # github.com/docker/cli/cli/command/system [github.com/docker/cli/cli/command/system.test]
    63.76 cli/command/system/client_test.go:22:79: undefined: container.Summary
    63.76 cli/command/system/client_test.go:38:103: undefined: container.Summary
    63.76 cli/command/system/client_test.go:42:21: undefined: container.Summary
    63.76 cli/command/system/completion_test.go:30:86: undefined: container.Summary
    63.76 cli/command/system/completion_test.go:31:25: undefined: container.Summary
    63.76 cli/command/system/completion_test.go:42:86: undefined: container.Summary (typecheck)

Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit e1c5180dba)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 14:55:02 +01:00
Harald Albers 9e7d01bfb3
Add completion for `events --filter`
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit d4f4cf1418)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 14:53:54 +01:00
Sebastiaan van Stijn df7113d7eb
[27.x] cli: fix non-constant format string in call to errors.Errorf (govet)
cli/required.go:17:24: printf: non-constant format string in call to github.com/docker/cli/vendor/github.com/pkg/errors.Errorf (govet)
     		return errors.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
     		                     ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-14 09:42:49 +01:00
Sebastiaan van Stijn 168a4bdc05
cli/command: PromptUserForCredentials: suppress unhandled errors
Keep the linters (and my IDE) happy; these errors should be safe to ignore.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 4b7a1e4613)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:41 +01:00
Sebastiaan van Stijn ddeb7eb4ed
cli/command: PromptUserForCredentials: use consts for all hints
This message resulted in code-lines that were too long; move it to a
const together with the other hint. While at it, also suppress unhandled
error, and touch-up the code-comment.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 378a3d7d36)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:41 +01:00
Sebastiaan van Stijn da3a1c3027
cli/command: PromptUserForCredentials: print error on terminal restore fail
If restoring the terminal state fails, "echo" no longer works, which means
that anything the user types is no longer shown. The login itself may already
have succeeded, so we should not fail the command, but it's good to inform
the user that this happened, which may give them a clue why things no longer
work as they expect them to work.

With this patch:

    docker login -u yourname
    Password:
    Error: failed to restore terminal state to echo input: something bad happened

    Login Succeeded

We should consider printing instructions how  to restore this manually (other
than restarting the shell). e.g., 'run stty echo' when in a Linux or macOS shell,
but PowerShell and CMD.exe may need different instructions.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3d8b49523d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:41 +01:00
Sebastiaan van Stijn 1377310110
cli/command: PromptUserForCredentials: always trim password
we don't support empty passwords; when prompting the user for a password,
we already trim the result, but we didn't do the same for a password that's
passed through stdin or through the `-p` / `--password` flag.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit a21a5f4243)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:41 +01:00
Sebastiaan van Stijn 78dbcca264
cli/command: PromptUserForCredentials: move trimming where it's used
- move trimming defaultUsername inside the if-branch, as it's the only
  location where the result of the trimmed username is use.
- do the reverse for trimming argUser, because the result of trimming
  argUser is used outside of the if-branch (not just for the condition).
  putting it inside the condition makes it easy to assume the result is
  only used locally.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit eda78e9cdc)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:41 +01:00
Sebastiaan van Stijn 97c25b7574
cli/command: PromptUserForCredentials: move "post" check for empty name
move the "post" check for username being empty inside the branch
that's handling the username, as it's the only branch where username
is mutated after checking if it's empty.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 581cf36bd4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:41 +01:00
Sebastiaan van Stijn 05455f8505
cli/command: PromptUserForCredentials: inline isDefaultRegistry
remove isDefaultRegistry and inline it where it's used; the code-comment
already outlines what we're looking for, so the intermediate var didn't
add much currently.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit a55cfe5f82)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:40 +01:00
Sebastiaan van Stijn d23ab524c3
cli/command: PromptUserForCredentials: remove named output variables
This function has multiple conditional branches, which makes it harder
to see at a glance whether authConfig may be partially populated. This
patch instead returns a fresh instance for error returns to prevent any
confusion.

It also removes the named output variables, as they're now no longer used,
and the returned types should already be descriptive enough to understand
what's returned.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3a8485085d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-13 23:32:40 +01:00
Giedrius Jonikas 470ab05503
Buffer 'docker stats' text to avoid terminal flickering
This change reduces the flickering of the terminal when
running `docker stats` by buffering the formatted stats
text and printing it in one write.

Should also consume less CPU as we now only have to issue
a single syscall to write the stats text to the terminal.

Signed-off-by: Giedrius Jonikas <giedriusj1@gmail.com>
(cherry picked from commit 0b16070ae6)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2024-10-31 14:29:38 +01:00
Laura Brehm 968506fd0f
Merge pull request #5569 from thaJeztah/27.x_backport_login_idempotent 2024-10-22 16:18:18 +01:00
Sebastiaan van Stijn 6736be779a
cli/config/credentials: add test for save being idempotent
Test case for d3f6867e4d

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3c78069240)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-22 12:23:27 +02:00
Sebastiaan van Stijn 720da3c65a
cil/config/credentials: remove newStore() test-utility
This function was names slightly confusing, as it returns a fakeStore,
and it didn't do any constructing, so didn't provide value above just
constructing the type.

I'm planning to add more functionality to the fakeStore, but don't want
to maintain a full-fledged constructor for all of that, so let's remove
this utility.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 0dd6f7f1b3)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-22 12:15:34 +02:00
Sebastiaan van Stijn 05c860e866
Merge pull request #5548 from thaJeztah/27.x_backport_plugin_better_error
[27.x backport] cli/command/plugins: use errors.Join instead of custom cli.Errors, and deprecate cli.Errors
2024-10-22 10:42:38 +02:00
Sebastiaan van Stijn 365e7a5a4e
cli/config/credentials: skip saving config-file if credentials didn't change
Before this change, the config-file was always updated, even if there
were no changes to save. This could cause issues when the config-file
already had credentials set and was read-only for the current user.

For example, on NixOS, this poses a problem because `config.json` is a
symlink to a write-protected file;

    $ readlink ~/.docker/config.json
    /home/username/.config/sops-nix/secrets/ghcr_auth

    $ readlink -f ~/.docker/config.json
    /run/user/1000/secrets.d/28/ghcr_auth

Which causes `docker login` to fail, even if no changes were to be made;

    Error saving credentials: rename /home/derek/.docker/config.json2180380217 /home/username/.config/sops-nix/secrets/ghcr_auth: invalid cross-device link

This patch updates the code to only update the config file if changes
were detected. It there's nothing to save, it skips updating the file,
as well as skips printing the warning about credentials being stored
insecurely.

With this patch applied:

    $ docker login -u yourname
    Password:

    WARNING! Your credentials are stored unencrypted in '/root/.docker/config.json'.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/go/credential-store/

    Login Succeeded

    $ docker login -u yourname
    Password:
    Login Succeeded

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit d3f6867e4d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-22 00:21:18 +02:00
Sebastiaan van Stijn df7e6e5c5f
cli/command/container: TestWaitExitOrRemoved use subtests
=== RUN   TestWaitExitOrRemoved
    === RUN   TestWaitExitOrRemoved/normal-container
    === RUN   TestWaitExitOrRemoved/give-me-exit-code-42
    === RUN   TestWaitExitOrRemoved/i-want-a-wait-error
    time="2024-10-13T18:48:14+02:00" level=error msg="Error waiting for container: removal failed"
    === RUN   TestWaitExitOrRemoved/non-existent-container-id
    time="2024-10-13T18:48:14+02:00" level=error msg="error waiting for container: no such container: non-existent-container-id"
    --- PASS: TestWaitExitOrRemoved (0.00s)
        --- PASS: TestWaitExitOrRemoved/normal-container (0.00s)
        --- PASS: TestWaitExitOrRemoved/give-me-exit-code-42 (0.00s)
        --- PASS: TestWaitExitOrRemoved/i-want-a-wait-error (0.00s)
        --- PASS: TestWaitExitOrRemoved/non-existent-container-id (0.00s)
    PASS

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 35d7b1a7a6)
Signed-off-by: Austin Vazquez <macedonv@amazon.com>
2024-10-21 15:53:20 +00:00
Sebastiaan van Stijn 254b966b0d
cli/command/container: set empty args in tests and discard output
Prevent some tests from failing when running from a pre-compiled
testbinary, and discard output to make the output less noisy.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3b38dc67be)
Signed-off-by: Austin Vazquez <macedonv@amazon.com>
2024-10-21 15:53:09 +00:00
Sebastiaan van Stijn a8a8f68268
cli/command: ConfigureAuth: fix deprecation comment
Deprecation comments must have an empty line before them, otherwise tools
and linters may not recognise them. While fixing this, also updated the
reference to PromptUserForCredentials to be a docs-link to make it clickable.

Updates 6e4818e7d6.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 54e3685bcd)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-19 13:10:31 +02:00
Sebastiaan van Stijn b3a55ecbba
cli: deprecate Errors type
The Errors type is no longer used by the CLI itself, and this custom
"multi-error" implementation had both limitations (empty list not being
`nil`), as well as formatting not being great. All of this making it not
something to recommend, and better handled with Go's stdlib.

As far as I could find, there's no external consumers of this, but let's
deprecate first, and remove in the next release.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit d3bafa5f3e)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-19 10:36:33 +02:00
Sebastiaan van Stijn d3bf82db86
cli/command/plugins: use errors.Join instead of custom cli.Errors
This command was using a custom "multi-error" implementation, but it
had some limitations, and the formatting wasn't great.

This patch replaces it with Go's errors.Join.

Before:

    docker plugin remove one two three
    Error response from daemon: plugin "one" not found, Error response from daemon: plugin "two" not found, Error response from daemon: plugin "three" not found

After:

    docker plugin remove one two three
    Error response from daemon: plugin "one" not found
    Error response from daemon: plugin "two" not found
    Error response from daemon: plugin "three" not found

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 71ebbb81ae)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-19 10:36:33 +02:00
Harald Albers b018e55ca4
Only complete removable containers if --force is not given
Signed-off-by: Harald Albers <github@albersweb.de>
(cherry picked from commit 147630a309)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:43:03 +02:00
Sebastiaan van Stijn e2831282ee
completion: add test for VolumeNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit e1c472a436)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:43:03 +02:00
Sebastiaan van Stijn b68bf3afe4
completion: add test for NetworkNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 302d73f990)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:43:03 +02:00
Sebastiaan van Stijn 77d002ae25
completion: add test for ImageNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit ab418a38d8)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:43:01 +02:00
Sebastiaan van Stijn 95e329d3e3
completion: add test for ContainerNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit f3b4094eb0)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:42:10 +02:00
Sebastiaan van Stijn 02da13fb18
completion: add test for NoComplete
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit be197da6b8)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:11:05 +02:00
Sebastiaan van Stijn 62230c7ec2
completion: add test for FromList
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 51713196c9)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:11:05 +02:00
Sebastiaan van Stijn 954dba4482
completion: add test for FileNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit a5ca5b33f1)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:11:05 +02:00
Sebastiaan van Stijn 7d1fa132fb
completion: add test for EnvVarNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 8f2e5662e7)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:11:05 +02:00
Sebastiaan van Stijn b6e7eba447
completion: ContainerNames: don't panic on nil filter
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit b8cddc63ad)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:11:04 +02:00
Sebastiaan van Stijn 099d4baeed
cli/command/image: add shell completion for --platform flags
With this patch, completion is provided for `--platform` flags:

    docker pull --platform<TAB>
    linux           linux/amd64     linux/arm/v5    linux/arm/v7    linux/arm64/v8  linux/riscv64   wasip1          windows
    linux/386       linux/arm       linux/arm/v6    linux/arm64     linux/ppc64le   linux/s390x     wasip1/wasm     windows/amd64

Note that `docker buildx build` (with BuildKit) does not yet provide completion;
it's provided through buildx, and uses a different format (accepting multiple
comma-separated platforms). Interestingly, tab-completion for `docker build`
currently uses completion for non-buildkit, and has some other issues that may
have to be looked into.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit d42cf96e15)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:10:59 +02:00
Sebastiaan van Stijn dd4b370d3c
cli/command/container: add shell completion for --platform flags
With this patch, completion is provided for `--platform` flags:

    docker run --platform<TAB>
    linux           linux/amd64     linux/arm/v5    linux/arm/v7    linux/arm64/v8  linux/riscv64   wasip1          windows
    linux/386       linux/arm       linux/arm/v6    linux/arm64     linux/ppc64le   linux/s390x     wasip1/wasm     windows/amd64

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 8c7f713db6)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-17 23:10:59 +02:00