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

usage, completion: assume executable is always lowercase #2951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thaJeztah
Copy link
Member

usage, completion: assume executable is always lowercase

When running on a case-insensitive platform / filesystem, the user
can invoke the executable using any case (e.g., "bUiLdX"). In situations
where buildx is running standalone, we use os.Args to determine the
name of the executable, and this information is used by Cobra to
print "Usage" output as well as generating the shell-completion
scripts.

Unfortunately, neither os.Executable nor os.Stat provide information
about the actual name of the executable, and the "correct" solution would
be to use os.File.Readdirnames, which is a rather heavy hammer to use
just for this.

This patch assumes the executable to be named lowercase, regardless of
how it's invoked, on macOS and Windows (usually case-insensitive platforms).

It's worth noting that there's a corner-case to this corner-case; both
Windows and macOS can be configured to use a case-sensitive filesystem
(on Windows, this can be configured per-Directory). If that is the case,
and the executable is not lowercase, the generated shell-completion script
will be invalid.

Let's assume that's not the case, and that the user did not rename the
executable to anything uppercase.

Before this patch:

./bin/build/bUiLdX --help | head -n 5
Extended build capabilities with BuildKit

Usage:
  bUiLdX
  bUiLdX [command]

./bin/build/bUiLdX completion bash | head -n 3
# bash completion V2 for bUiLdX                               -*- shell-script -*-

__bUiLdX_debug()

With this patch applied:

./bin/build/bUiLdX --help | head -n 5
Extended build capabilities with BuildKit

Usage:
  buildx
  buildx [command]

./bin/build/bUiLdX completion bash | head -n 3
# bash completion V2 for buildx                               -*- shell-script -*-

__buildx_debug()

@thaJeztah thaJeztah added kind/enhancement New feature or request area/cli labels Jan 25, 2025
@thaJeztah thaJeztah self-assigned this Jan 25, 2025
When running on a case-insensitive platform / filesystem, the user
can invoke the executable using any case (e.g., "bUiLdX"). In situations
where buildx is running standalone, we use os.Args to determine the
name of the executable, and this information is used by Cobra to
print "Usage" output as well as generating the shell-completion
scripts.

Unfortunately, neither [os.Executable] nor [os.Stat] provide information
about the actual name of the executable, and the "correct" solution would
be to use [os.File.Readdirnames], which is a rather heavy hammer to use
just for this.

This patch assumes the executable to be named lowercase, regardless of
how it's invoked, on macOS and Windows (usually case-insensitive platforms).

It's worth noting that there's a corner-case to this corner-case; both
Windows and macOS can be configured to use a case-sensitive filesystem
(on Windows, this can be configured per-Directory). If that is the case,
and the executable is not lowercase, the generated shell-completion script
will be invalid.

Let's assume that's not the case, and that the user did not rename the
executable to anything uppercase.

Before this patch:

    ./bin/build/bUiLdX --help | head -n 5
    Extended build capabilities with BuildKit

    Usage:
      bUiLdX
      bUiLdX [command]

    ./bin/build/bUiLdX completion bash | head -n 3
    # bash completion V2 for bUiLdX                               -*- shell-script -*-

    __bUiLdX_debug()

With this patch applied:

    ./bin/build/bUiLdX --help | head -n 5
    Extended build capabilities with BuildKit

    Usage:
      buildx
      buildx [command]

    ./bin/build/bUiLdX completion bash | head -n 3
    # bash completion V2 for buildx                               -*- shell-script -*-

    __buildx_debug()

[os.Executable]: https://pkg.go.dev/os#Executable
[os.Stat]: https://pkg.go.dev/os#Stat
[os.File.Readdirnames]: https://pkg.go.dev/os#File.Readdirnames

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the fix_usage_and_completion_case_insensitive branch from d636c73 to 67687bd Compare February 2, 2025 13:31
@thaJeztah thaJeztah marked this pull request as ready for review February 2, 2025 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cli kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant