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

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

Merged
merged 9 commits into from
May 28, 2023

Conversation

thaJeztah
Copy link
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/exec@go1.20.4#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/exec@go1.20.4#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)

Don't set Env if not set; the default is already handled if it's nil; from
the documentation: https://pkg.go.dev/os/exec@go1.20.4#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/exec@go1.20.4#Cmd.Environ

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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- 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 <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
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 <github@gone.nl>
Don't use an err if we can print the error immediately :)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
- 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 <github@gone.nl>
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 <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@codecov-commenter
Copy link

codecov-commenter commented May 28, 2023

Codecov Report

Patch coverage: 24.24% and project coverage change: -0.56 ⚠️

Comparison is base (f8e94d9) 55.23% compared to head (129017a) 54.68%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #284      +/-   ##
==========================================
- Coverage   55.23%   54.68%   -0.56%     
==========================================
  Files           9        9              
  Lines         668      673       +5     
==========================================
- Hits          369      368       -1     
- Misses        256      262       +6     
  Partials       43       43              
Impacted Files Coverage Δ
client/command.go 0.00% <0.00%> (ø)
credentials/credentials.go 48.67% <14.28%> (-3.67%) ⬇️
client/client.go 67.53% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +72 to +74
case "--version", "-v":
_ = PrintVersion(os.Stdout)
os.Exit(0)
Copy link
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
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
10 checks passed
@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.

None yet

3 participants