Skip to content

Assorted improvements, and add "--version, -v", and "--help, -h" flags#284

Merged
thaJeztah merged 9 commits intodocker:masterfrom
thaJeztah:various_cleanups
May 28, 2023
Merged

Assorted improvements, and add "--version, -v", and "--help, -h" flags#284
thaJeztah merged 9 commits intodocker:masterfrom
thaJeztah:various_cleanups

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

See individual commits for details. Some outlined below:

client: use os/exec/Cmd.Environ() instead of os.Environ()

Don't set Env if not set; the default is already handled if it's nil; from
the documentation: https://pkg.go.dev/os/[email protected]#Cmd.Env

// If Env is nil, the new process uses the current process's
// environment.

Use os/exec/Cmd.Environ() instead of os.Environ(), which was added in
go1.19, and handles additional environment variables, such as PWD on POSIX
systems, and SYSTEMROOT on Windows. https://pkg.go.dev/os/[email protected]#Cmd.Environ

Also remove a redundant fmt.Sprintf(), as we're only concatenating strings.

credentials: Serve(): use "Name instead of "os.Args[0]" for usage output

GNU guidelines describes; https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion

The program’s name should be a constant string; don’t compute it from argv[0].
The idea is to state the standard or canonical name for the program, not its
file name.

Although the above recommendation is for --version output, it probably makes
sense to do the same for the "usage" output.

Before this change:

/usr/local/bin/docker-credential-osxkeychain invalid command
Usage: /usr/local/bin/docker-credential-osxkeychain <store|get|erase|list|version>

/Applications/Docker.app/Contents/Resources/bin/docker-credential-osxkeychain invalid command
Usage: /Applications/Docker.app/Contents/Resources/bin/docker-credential-osxkeychain <store|get|erase|list|version>

With this patch:

/usr/local/bin/docker-credential-osxkeychain invalid command
Usage: docker-credential-osxkeychain <store|get|erase|list|version>

/Applications/Docker.app/Contents/Resources/bin/docker-credential-osxkeychain invalid command
Usage: docker-credential-osxkeychain <store|get|erase|list|version>

credentials: Serve(): simplify error-handling logic

Don't use an err if we can print the error immediately :)

credentials: HandleCommand(): improve error for unknown command/action

  • renamed the "key" variable, which was slightly confusing
  • include the name of the binary in the error

Before this change:

docker-credential-osxkeychain nosuchaction
Unknown credential action `nosuchaction`

After this change:

docker-credential-osxkeychain nosuchaction
docker-credential-osxkeychain: unknown action: nosuchaction

credentials: Serve(): implement "--version, -v", and "--help, -h" flags

As recommended in the GNU documentation;

With this patch:

$ docker-credential-osxkeychain --version
docker-credential-osxkeychain (github.com/docker/docker-credential-helpers) v0.7.0-51-g26c426e.m

$ docker-credential-osxkeychain -v
docker-credential-osxkeychain (github.com/docker/docker-credential-helpers) v0.7.0-51-g26c426e.m

$ docker-credential-osxkeychain --help
Usage: docker-credential-osxkeychain <store|get|erase|list|version>

$ docker-credential-osxkeychain -h
Usage: docker-credential-osxkeychain <store|get|erase|list|version>

credentials: define consts for supported actions (sub-commands)

thaJeztah added 9 commits May 28, 2023 12:11
Don't set Env if not set; the default is already handled if it's nil; from
the documentation: https://pkg.go.dev/os/[email protected]#Cmd.Env

    // If Env is nil, the new process uses the current process's
    // environment.

Use `os/exec/Cmd.Environ()` instead of `os.Environ()`, which was added in
go1.19, and handles additional environment variables, such as `PWD` on POSIX
systems, and `SYSTEMROOT` on Windows. https://pkg.go.dev/os/[email protected]#Cmd.Environ

Also remove a redundant `fmt.Sprintf()`, as we're only concatenating strings.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
- Explicitly suppress some unhandled errors
- Use "pass" credentials helper in examples, which is available
  on more platforms than "secretservice" (only supporte on Linux)
- Update domain and username in examples.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
GNU guidelines describes; https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion

    The program’s name should be a constant string; don’t compute it from argv[0].
    The idea is to state the standard or canonical name for the program, not its
    file name.

Although the above recommendation is for `--version` output, it probably makes
sense to do the same for the "usage" output.

Before this change:

    /usr/local/bin/docker-credential-osxkeychain invalid command
    Usage: /usr/local/bin/docker-credential-osxkeychain <store|get|erase|list|version>

    /Applications/Docker.app/Contents/Resources/bin/docker-credential-osxkeychain invalid command
    Usage: /Applications/Docker.app/Contents/Resources/bin/docker-credential-osxkeychain <store|get|erase|list|version>

With this patch:

    /usr/local/bin/docker-credential-osxkeychain invalid command
    Usage: docker-credential-osxkeychain <store|get|erase|list|version>

    /Applications/Docker.app/Contents/Resources/bin/docker-credential-osxkeychain invalid command
    Usage: docker-credential-osxkeychain <store|get|erase|list|version>

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Don't use an err if we can print the error immediately :)

Signed-off-by: Sebastiaan van Stijn <[email protected]>
- renamed the "key" variable, which was slightly confusing
- include the name of the binary in the error

Before this change:

    docker-credential-osxkeychain nosuchaction
    Unknown credential action `nosuchaction`

After this change:

    docker-credential-osxkeychain nosuchaction
    docker-credential-osxkeychain: unknown action: nosuchaction

Signed-off-by: Sebastiaan van Stijn <[email protected]>
As recommended in the GNU documentation;

- https://www.gnu.org/prep/standards/standards.html#g_t_002d_002dversion
- https://www.gnu.org/prep/standards/standards.html#g_t_002d_002dhelp

With this patch:

    $ docker-credential-osxkeychain --version
    docker-credential-osxkeychain (github.com/docker/docker-credential-helpers) v0.7.0-51-g26c426e.m

    $ docker-credential-osxkeychain -v
    docker-credential-osxkeychain (github.com/docker/docker-credential-helpers) v0.7.0-51-g26c426e.m

    $ docker-credential-osxkeychain --help
    Usage: docker-credential-osxkeychain <store|get|erase|list|version>

    $ docker-credential-osxkeychain -h
    Usage: docker-credential-osxkeychain <store|get|erase|list|version>

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 28, 2023

Codecov Report

❌ Patch coverage is 24.24242% with 25 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
credentials/credentials.go 14.28% 23 Missing and 1 partial ⚠️
client/command.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment on lines +72 to +74
case "--version", "-v":
_ = PrintVersion(os.Stdout)
os.Exit(0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an extra version flag as we already have the version action?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly making them a bit more standard; GNU (and POSIX) define --help and --version as the minimum; https://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfaces

All programs should support two standard options: ‘--version’ and ‘--help’. CGI programs should accept these as command-line options, and also if given as the PATH_INFO; for instance, visiting ‘http://example.org/p.cgi/--help’ in a browser should output the same information as invoking ‘p.cgi --help’ from the command line.

I could omit the shorthand ones though

@thaJeztah thaJeztah merged commit bdd92dd into docker:master May 28, 2023
@thaJeztah thaJeztah deleted the various_cleanups branch May 28, 2023 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants