Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[27.x backport] cli/command: PromptUserForCredentials: assorted minor improvements and (linting) fixes #5611

Merged
merged 8 commits into from
Nov 14, 2024

Conversation

thaJeztah
Copy link
Member


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.

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.

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.

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.

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.

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.

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.

cli/command: PromptUserForCredentials: suppress unhandled errors

Keep the linters (and my IDE) happy; these errors should be safe to ignore.

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

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 <[email protected]>
(cherry picked from commit 3a84850)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
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 <[email protected]>
(cherry picked from commit a55cfe5)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
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 <[email protected]>
(cherry picked from commit 581cf36)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
- 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 <[email protected]>
(cherry picked from commit eda78e9)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
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 <[email protected]>
(cherry picked from commit a21a5f4)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
…e 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 <[email protected]>
(cherry picked from commit 3d8b495)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
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 <[email protected]>
(cherry picked from commit 378a3d7)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Keep the linters (and my IDE) happy; these errors should be safe to ignore.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
(cherry picked from commit 4b7a1e4)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah added this to the 27.4.0 milestone Nov 13, 2024
@thaJeztah thaJeztah self-assigned this Nov 13, 2024
@codecov-commenter
Copy link

codecov-commenter commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 0% with 36 lines in your changes missing coverage. Please review.

Project coverage is 58.36%. Comparing base (e15a979) to head (168a4bd).
Report is 23 commits behind head on 27.x.

Additional details and impacted files
@@            Coverage Diff             @@
##             27.x    #5611      +/-   ##
==========================================
- Coverage   58.65%   58.36%   -0.29%     
==========================================
  Files         345      336       -9     
  Lines       29053    28791     -262     
==========================================
- Hits        17040    16805     -235     
+ Misses      11042    11019      -23     
+ Partials      971      967       -4     

@thaJeztah thaJeztah merged commit 8be6bec into docker:27.x Nov 14, 2024
105 checks passed
@thaJeztah thaJeztah deleted the 27.x_backport_login_minor_refactor branch November 14, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants