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

cmd: Added shell completion command #840

Merged
merged 2 commits into from
Feb 21, 2022

Conversation

olivergs
Copy link
Collaborator

Initial work for fixing #836

I need to dig a bit more in the command completion generation to enhance the completions for args like distro.

@olivergs olivergs mentioned this pull request Jul 16, 2021
@softwarefactory-project-zuul
Copy link

Build succeeded.

@HarryMichal HarryMichal added 2. CLI Issue is related to the command line interface 3. New Feature New feature Project Pickle 🥒 UX refinement initiative labels Jul 16, 2021
@travier
Copy link
Member

travier commented Jul 21, 2021

This looks like a great start and works nicely for general options. Here are some suggestions for next steps:

@olivergs
Copy link
Collaborator Author

olivergs commented Jul 21, 2021

I was working on that today. I'm fixing some bits here and there and will update the branch.

About the generated bash and rpm install, I will work on that after I get the completion feature finished.

@softwarefactory-project-zuul
Copy link

Build failed.

var image_names []string
if images, err := getImages(); err == nil {
for _, image := range images {
image_names = append(image_names, image.Names[0])
Copy link
Member

@HarryMichal HarryMichal Jul 21, 2021

Choose a reason for hiding this comment

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

Be careful here because image.Names may not contain a single string. See #800.

@olivergs olivergs force-pushed the toolbox-cobra-completions branch from e128791 to 2a1666b Compare July 21, 2021 23:47
@softwarefactory-project-zuul
Copy link

Build succeeded.

@HarryMichal HarryMichal added this to the Release 0.1.0 milestone Oct 25, 2021
HarryMichal pushed a commit to olivergs/toolbox that referenced this pull request Nov 21, 2021
Cobra (the CLI library) has an advanced support for generating shell
completion. It support Bash, Zsh, Fish and PowerShell. This offering
covers the majority of use cases with some exceptions, of course.

The generated completion scripts have one behavioral difference when
compared to the existing solution: flags (--xxx) are not shown by
default. User needs to type '-' first to get the completion.

containers#840
HarryMichal added a commit to olivergs/toolbox that referenced this pull request Nov 21, 2021
The previous commit added a means to generating the completion scripts
and this one plugs that into the build system.

A new build option 'install_completions' has been introduced.

Completions for bash and fish use pkg-config for getting the preferred
install locations for the completions. If the packages are not
available, fallbacks are in-place.

The 'completion' subdir has been kept to work around the ideology of
Meson that does not allow creating/outputing files in subdirectories nor
using the output of custom_target() in install_data().

containers#840
@HarryMichal HarryMichal force-pushed the toolbox-cobra-completions branch from 2a1666b to e4e4783 Compare November 21, 2021 14:35
@HarryMichal
Copy link
Member

I rebased this on top of #917 to benefit from the work on updating dependencies (required for the Cobra features this PR makes use of).

@HarryMichal
Copy link
Member

This should be good for testing. @debarshiray @olivergs @travier 🙏

@softwarefactory-project-zuul
Copy link

Build failed.

HarryMichal added a commit to olivergs/toolbox that referenced this pull request Nov 21, 2021
Cobra (the CLI library) has an advanced support for generating shell
completion. It support Bash, Zsh, Fish and PowerShell. This offering
covers the majority of use cases with some exceptions, of course.

The generated completion scripts have one behavioral difference when
compared to the existing solution: flags (--xxx) are not shown by
default. User needs to type '-' first to get the completion.

containers#840

Co-authored-by: Ondřej Míchal <harrymichal@seznam.cz>
HarryMichal added a commit to olivergs/toolbox that referenced this pull request Nov 21, 2021
The previous commit added a means to generating the completion scripts
and this one plugs that into the build system.

A new build option 'install_completions' has been introduced.

Completions for bash and fish use pkg-config for getting the preferred
install locations for the completions. If the packages are not
available, fallbacks are in-place.

The 'completion' subdir has been kept to work around the ideology of
Meson that does not allow creating/outputing files in subdirectories nor
using the output of custom_target() in install_data().

containers#840
@HarryMichal HarryMichal force-pushed the toolbox-cobra-completions branch from e4e4783 to 1b6b9c2 Compare November 21, 2021 23:39
@softwarefactory-project-zuul
Copy link

Merge Failed.

This change or one of its cross-repo dependencies was unable to be automatically merged with the current state of its repository. Please rebase the change and upload a new patchset.

HarryMichal added a commit to olivergs/toolbox that referenced this pull request Nov 21, 2021
Cobra (the CLI library) has an advanced support for generating shell
completion. It support Bash, Zsh, Fish and PowerShell. This offering
covers the majority of use cases with some exceptions, of course.

The generated completion scripts have one behavioral difference when
compared to the existing solution: flags (--xxx) are not shown by
default. User needs to type '-' first to get the completion.

containers#840

Co-authored-by: Ondřej Míchal <harrymichal@seznam.cz>
HarryMichal added a commit to olivergs/toolbox that referenced this pull request Nov 21, 2021
The previous commit added a means to generating the completion scripts
and this one plugs that into the build system.

A new build option 'install_completions' has been introduced.

Completions for bash and fish use pkg-config for getting the preferred
install locations for the completions. If the packages are not
available, fallbacks are in-place.

The 'completion' subdir has been kept to work around the ideology of
Meson that does not allow creating/outputing files in subdirectories nor
using the output of custom_target() in install_data().

containers#840
@HarryMichal HarryMichal force-pushed the toolbox-cobra-completions branch from 1b6b9c2 to 47eb18a Compare November 21, 2021 23:51
@HarryMichal
Copy link
Member

Darn you Zuul! This works locally :/.

@softwarefactory-project-zuul
Copy link

Build failed.

@travier
Copy link
Member

travier commented Nov 22, 2021

I gave it a try today by doing:

$ meson -Dprofile_dir=/etc/profile.d builddir
$ ninja -C buillddir
$ ./builddir/src/toolbox completion zsh > ~/.path/to/zsh/completion/zsh_completions/_toolbox

but as far as I could see, the comments from #840 (comment) still apply. This is however still progress as at least the commands are completed so addressing my comments could be for a later PR.

Thanks!

@HarryMichal
Copy link
Member

but as far as I could see, the comments from #840 (comment) still apply. This is however still progress as at least the commands are completed so addressing my comments could be for a later PR.

Which part of the comment do you mean? If the "extra completion" then could you elaborate a bit more? I don't quite understand the suggestion in its current form. The later part should be taken care of at build (resp. install) time. The completion is generated using the build system.

Thanks!

Also thanks!

HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
While the use of Logrus is convenient, it causes unneeded fluff to be
printed during a panic which distracts from finding the cause of the
panic in the first place.

Fallout from containers#840
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
Requiring having a shell installed at build-time to get the appropriate
location won't cover the cases like when a system has such a location
set to a non-standard path.

The use of fallbacks together with hardcoded paths also increases the
complexity of this particular feature.

All 3 major shells (Bash, Zsh, Fish) have documented default directories
where they look for completion scripts and we can make use of that.
Additionaly, hard-coding of these paths seems to be the standard among
projects.

Fallout from containers#840

containers#1055
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
We don't provide completion for PowerShell, we support only Linux and
instructions for loading completion files are redundant in Toolbx.

Fallout from containers#840

containers#1055
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
While the use of Logrus is convenient, it causes unneeded fluff to be
printed during a panic which distracts from finding the cause of the
panic in the first place.

Fallout from containers#840

containers#1055
@HarryMichal
Copy link
Member

Opened #1055 to address the raised concerns.

HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
Requiring having a shell installed at build-time to get the appropriate
location won't cover the cases like when a system has such a location
set to a non-standard path.

The use of fallbacks together with hardcoded paths also increases the
complexity of this particular feature.

All 3 major shells (Bash, Zsh, Fish) have documented default directories
where they look for completion scripts and we can make use of that.
Additionaly, hard-coding of these paths seems to be the standard among
projects.

Fallout from containers#840

containers#1055
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
We don't provide completion for PowerShell, we support only Linux and
instructions for loading completion files are redundant in Toolbx.

Fallout from containers#840

containers#1055
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
HarryMichal added a commit to HarryMichal/toolbox that referenced this pull request May 20, 2022
While the use of Logrus is convenient, it causes unneeded fluff to be
printed during a panic which distracts from finding the cause of the
panic in the first place.

Fallout from containers#840

containers#1055
completion/meson.build Show resolved Hide resolved
completion/meson.build Show resolved Hide resolved
src/cmd/completion.go Show resolved Hide resolved
src/cmd/create.go Show resolved Hide resolved
src/cmd/completion.go Show resolved Hide resolved
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Sep 12, 2022
This is particularly relevant for Z shell, because, unlike Bash's
bash-completion.pc and fish's fish.pc, it doesn't have an API to detect
the location for the shell completions and Debian and Fedora use
different locations [1, 2].  Namely, /usr/share/zsh/vendor-completions
and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd as a build dependency [3].

Finally, when installing to a non-system-wide prefix while hacking on
Toolbox as a non-root user, the locations for the completions in the
shells' APIs might not be accessible.  Being able to override the
locations prevents the installation from failing.

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Sep 12, 2022
There's no well-defined use-case for having a build option for
generating and installing the shell completions.

The bash-completion and fish dependencies are already optional, and
there's no dependency required for Z shell.  The shell completions for
Bash and fish won't be generated and installed if they are absent.  So
the build option isn't reducing the dependency burden.

The build option is a way to disable the generation and installation of
the shell completions, regardless of whether the necessary dependencies
are present or not.  There's no well-defined use-case for that.

Fallout from bafbbe8

containers#1123
containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Sep 12, 2022
This is particularly relevant for Z shell, because, unlike Bash's
bash-completion.pc and fish's fish.pc, it doesn't have an API to detect
the location for the shell completions and Debian and Fedora use
different locations [1, 2].  Namely, /usr/share/zsh/vendor-completions
and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd as a build dependency [3].

Finally, when installing to a non-system-wide prefix while hacking on
Toolbox as a non-root user, the locations for the completions in the
shells' APIs might not be accessible.  Being able to override the
locations prevents the installation from failing.

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#1123
containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Sep 12, 2022
The bash-completion and fish dependencies are already optional, and
there's no dependency required for Z shell.  The shell completions for
Bash and fish won't be generated and installed if they are absent.  So
the build option isn't reducing the dependency burden.

The build option is a way to disable the generation and installation of
the shell completions, regardless of whether the necessary dependencies
are present or not.  The only use-case for this is when installing to a
non-system-wide prefix while hacking on Toolbox as a non-root user,
because the locations for the completions in the shells' APIs might not
be accessible.  Being able to disable the completions prevents the
installation from failing.

Fallout from bafbbe8

containers#1123
containers#840

build: Add options to override the locations for the shell completions

This is particularly relevant for Z shell, because, unlike Bash's
bash-completion.pc and fish's fish.pc, it doesn't have an API to detect
the location for the shell completions and Debian and Fedora use
different locations [1, 2].  Namely, /usr/share/zsh/vendor-completions
and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd as a build dependency [3].

Finally, when installing to a non-system-wide prefix while hacking on
Toolbox as a non-root user, the locations for the completions in the
shells' APIs might not be accessible.  Being able to override the
locations prevents the installation from failing.

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#1123
containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Oct 21, 2022
The bash-completion and fish dependencies were already optional - the
shell completions for Bash and fish won't be generated and installed if
they are absent; and there's no dependency required for Z shell.  So the
install_completions build option wasn't reducing the dependency burden.

The build option was a way to disable the generation and installation of
the shell completions, regardless of whether the necessary dependencies
are present or not.  The only use-case for this is when installing to a
non-system-wide prefix while hacking on Toolbox as a non-root user,
because the locations for the completions advertised by the shells' APIs
might not be accessible.  Being able to disable the completions prevents
the installation from failing.

A different way of ensuring a smooth developer experience for a Toolbx
hacker is to offer a way to change the locations where the shell
completions are installed, which is necessary and beneficial for other
use-cases.

Z shell, unlike Bash's bash-completion.pc and fish's fish.pc, doesn't
offer an API to detect the location for the shell completions.  This
means that Debian and Fedora use different locations [1, 2].  Namely,
/usr/share/zsh/vendor-completions and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build, if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd.pc as a build dependency [3].

Fallout from bafbbe8

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#1123
containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Oct 21, 2022
The bash-completion and fish dependencies were already optional - the
shell completions for Bash and fish won't be generated and installed if
they are absent; and there's no dependency required for Z shell.  So the
install_completions build option wasn't reducing the dependency burden.

The build option was a way to disable the generation and installation of
the shell completions, regardless of whether the necessary dependencies
are present or not.  The only use-case for this is when installing to a
non-system-wide prefix while hacking on Toolbox as a non-root user,
because the locations for the completions advertised by the shells' APIs
might not be accessible.  Being able to disable the completions prevents
the installation from failing.

A different way of ensuring a smooth developer experience for a Toolbx
hacker is to offer a way to change the locations where the shell
completions are installed, which is necessary and beneficial for other
use-cases.

Z shell, unlike Bash's bash-completion.pc and fish's fish.pc, doesn't
offer an API to detect the location for the shell completions.  This
means that Debian and Fedora use different locations [1, 2].  Namely,
/usr/share/zsh/vendor-completions and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build, if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd.pc as a build dependency [3].

Fallout from bafbbe8

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#1123
containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Oct 21, 2022
The bash-completion and fish dependencies were already optional - the
shell completions for Bash and fish won't be generated and installed if
they are absent; and there's no dependency required for Z shell.  So the
install_completions build option wasn't reducing the dependency burden.

The build option was a way to disable the generation and installation of
the shell completions, regardless of whether the necessary dependencies
are present or not.  The only use-case for this is when installing to a
non-system-wide prefix while hacking on Toolbox as a non-root user,
because the locations for the completions advertised by the shells' APIs
might not be accessible.  Being able to disable the completions prevents
the installation from failing.

A different way of ensuring a smooth developer experience for a Toolbx
hacker is to offer a way to change the locations where the shell
completions are installed, which is necessary and beneficial for other
use-cases.

Z shell, unlike Bash's bash-completion.pc and fish's fish.pc, doesn't
offer an API to detect the location for the shell completions.  This
means that Debian and Fedora use different locations [1, 2].  Namely,
/usr/share/zsh/vendor-completions and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build, if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd.pc as a build dependency [3].

Fallout from bafbbe8

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#1123
containers#840
debarshiray added a commit to debarshiray/toolbox that referenced this pull request Oct 21, 2022
The bash-completion and fish dependencies were already optional - the
shell completions for Bash and fish won't be generated and installed if
they are absent; and there's no dependency required for Z shell.  So the
install_completions build option wasn't reducing the dependency burden.

The build option was a way to disable the generation and installation of
the shell completions, regardless of whether the necessary dependencies
are present or not.  The only use-case for this is when installing to a
non-system-wide prefix while hacking on Toolbox as a non-root user,
because the locations for the completions advertised by the shells' APIs
might not be accessible.  Being able to disable the completions prevents
the installation from failing.

A different way of ensuring a smooth developer experience for a Toolbx
hacker is to offer a way to change the locations where the shell
completions are installed, which is necessary and beneficial for other
use-cases.

Z shell, unlike Bash's bash-completion.pc and fish's fish.pc, doesn't
offer an API to detect the location for the shell completions.  This
means that Debian and Fedora use different locations [1, 2].  Namely,
/usr/share/zsh/vendor-completions and /usr/share/zsh/site-functions.

An option to specify the locations for the shell completions can
optimize the build, if there's an alternate API for the location that
doesn't involve using bash-completion.pc and fish.pc as build
dependencies.  eg., Fedora provides the _tmpfilesdir RPM macro to
specify the location for vendor-supplied tmpfiles.d(5) files, which
makes it possible to avoid having systemd.pc as a build dependency [3].

Fallout from bafbbe8

[1] Debian zsh commit bf0a44a8744469b5
    https://salsa.debian.org/debian/zsh/-/commit/bf0a44a8744469b5
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=620452

[2] https://src.fedoraproject.org/rpms/zsh/blob/f37/f/zsh.spec

[3] Fedora toolbox commit 9bebde5bb60f36e3
    https://src.fedoraproject.org/rpms/toolbox/c/9bebde5bb60f36e3

containers#1123
containers#840
debarshiray pushed a commit to HarryMichal/toolbox that referenced this pull request Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2. CLI Issue is related to the command line interface 3. New Feature New feature Project Pickle 🥒 UX refinement initiative
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bash completion errors for invalid subcommands
5 participants