From e4f206f4edff14f3d5eaed05710d77044dcead86 Mon Sep 17 00:00:00 2001 From: Ioannis Bonatakis Date: Mon, 18 Nov 2024 11:12:17 +0100 Subject: [PATCH 1/2] Add bash and zsh completion scripts for openqa-cli * Allow static completion of the openqa-cli * Provide full cli options for each subcommand * Group options for each subcommand * Not help text in bash due to `compgen` function doesn't support descriptions Signed-off-by: Ioannis Bonatakis --- docs/UsersGuide.asciidoc | 8 ++++ openqa-cli-completion.bash | 37 ++++++++++++++++++ openqa-cli-completion.zsh | 78 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 openqa-cli-completion.bash create mode 100644 openqa-cli-completion.zsh diff --git a/docs/UsersGuide.asciidoc b/docs/UsersGuide.asciidoc index 0b41a2b4706..50389e19a22 100644 --- a/docs/UsersGuide.asciidoc +++ b/docs/UsersGuide.asciidoc @@ -963,6 +963,14 @@ without needing to install openQA itself. Call `openqa-cli --help` for help. The sub-commands provide further help, e.g. `openqa-cli api --help` contains a lot of examples. +`openqa-cli` supports auto completion for `bash` and `zsh`. +If you use `bash` run or add the line below on your `.bashrc` profile. + +[source,sh] +---- +source openqa-cli-completion.bash +---- + This section focuses on particular API use-cases. Checkout the <> section for further information about the client itself, how authentication works and how plain `curl` can be used. diff --git a/openqa-cli-completion.bash b/openqa-cli-completion.bash new file mode 100644 index 00000000000..9b3bc32619e --- /dev/null +++ b/openqa-cli-completion.bash @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +_openqa_cli_completions() { + local cur prev subcommands main_options api_options archive_options monitor_options schedule_options + cur="${COMP_WORDS[COMP_CWORD]}" + subcommands="api archive monitor schedule" + main_options="--apibase --apikey --apisecret --help --host --o3 --osd" + api_options="--json --retries --method --header --links --data-file --data --form --param-file" + archive_options="--asset-size-limit --name --with-thumbnails" + monitor_options="--name --poll-interval" + schedule_options="--monitor --name --poll-interval" + + if [[ ${COMP_CWORD} -eq 1 ]]; then + COMPREPLY=($(compgen -W "$subcommands" -- "$cur")) + return 0 + fi + case "${COMP_WORDS[1]}" in + api) + COMPREPLY=($(compgen -W "$main_options $api_options" -- "$cur")) + ;; + archive) + COMPREPLY=($(compgen -W "$main_options $archive_options" -- "$cur")) + ;; + monitor) + COMPREPLY=($(compgen -W "$main_options $monitor_options" -- "$cur")) + ;; + schedule) + COMPREPLY=($(compgen -W "$main_options $schedule_options" -- "$cur")) + ;; + *) + COMPREPLY=($(compgen -W "$subcommands" -- "$cur")) + ;; + esac +} + +# Register the completion function for openqa-cli +complete -F _openqa_cli_completions openqa-cli diff --git a/openqa-cli-completion.zsh b/openqa-cli-completion.zsh new file mode 100644 index 00000000000..287d0185b13 --- /dev/null +++ b/openqa-cli-completion.zsh @@ -0,0 +1,78 @@ +#!/usr/bin/env zsh + +_openqa_cli_completions() { + local curcontext="$curcontext" state line + typeset -A opt_args + local -a subcommands + local -a main_options + local -a output_options + local -a api_options + local -a archive_options + local -a monitor_options + local -a schedule_options + subcommands=("api" "archive" "monitor" "schedule") + main_options=( + '--apibase[Base URL for the API]' + '--apikey[API key for authentication]' + '--apisecret[API secret for authentication]' + '--help[Show help message]' + '--host[Target host, defaults to http://localhost]' + '--o3[Set target host to https://openqa.opensuse.org]' + '--osd[Set target host to http://openqa.suse.de]' + ) + output_options=( + '--pretty[Pretty print JSON content]' + '--quiet[Do not print error messages to STDERR]' + '--verbose[Print HTTP response headers]' + ) + api_options=( + '--json[Request content is JSON]' + '--retries[Retry up to the specified value on some error]' + '--method[HTTP method to use, defaults to GET]' + '--header[One or more additional HTTP headers]' + '--links[Print pagination links to STDERR]' + '--data-file[Load content to send with request from file]' + '--data[Content to send with request]' + '--form[Turn JSON object into form parameters]' + '--param-file[Load content of params from files instead of from command line arguments.]' + ) + archive_options=( + '--asset-size-limit[Asset size limit in bytes]' + '--name[Name of this client, used by openQA to identify different clients via User-Agent header, defaults to "openqa-cli"]' + '--with-thumbnails[Download thumbnails as well]' + ) + monitor_options=( + '--name[Name of this client, used by openQA to identify different clients via User-Agent header, defaults to "openqa-cli"]' + '--poll-interval[Specifies the poll interval in seconds]' + ) + schedule_options=( + '--monitor[it until all jobs are done/cancelled and return non-zero exit code if at least on job has not passed/softfailed]' + '--name[Name of this client, used by openQA to identify different clients via User-Agent header, defaults to "openqa-cli"]' + '--poll-interval[Specifies the poll interval in seconds]' + ) + # Complete with subcommands + if (( CURRENT == 2 )); then + _describe 'subcommand' subcommands + else + case "$words[2]" in + api) + _arguments '*:option:->options' $main_options $api_options + ;; + archive) + _arguments '*:option:->options' $main_options $archive_options + ;; + monitor) + _arguments '*:option:->options' $main_options $archive_options + ;; + schedule) + _arguments '*:option:->options' $main_options $archive_options + ;; + *) + _values 'subcommand' api archive monitor schedule + ;; + esac + fi +} + +# Register the completion function for openqa-cli +compdef _openqa_cli_completions openqa-cli From b354e72b383b4d120e4873681ed8e4ac824042a2 Mon Sep 17 00:00:00 2001 From: Ioannis Bonatakis Date: Thu, 21 Nov 2024 16:00:32 +0100 Subject: [PATCH 2/2] Create openqa-cli-bash-completion package Provide openqa-cli bash completion for openqa-cli as a package when openQA is install and bash is used. Signed-off-by: Ioannis Bonatakis Signed-off-by: ybonatakis Signed-off-by: Ioannis Bonatakis Signed-off-by: ybonatakis --- Makefile | 11 +++++++++ .../completions/openqa-cli-completion.bash | 2 -- .../completions/openqa-cli-completion.zsh | 6 ++--- dist/rpm/openQA.spec | 23 +++++++++++++++++++ docs/UsersGuide.asciidoc | 13 +++++------ 5 files changed, 43 insertions(+), 12 deletions(-) rename openqa-cli-completion.bash => contrib/completions/openqa-cli-completion.bash (98%) rename openqa-cli-completion.zsh => contrib/completions/openqa-cli-completion.zsh (94%) diff --git a/Makefile b/Makefile index e370a597a23..5f4cb20cb8a 100644 --- a/Makefile +++ b/Makefile @@ -125,6 +125,17 @@ install-generic: ln -s ../postgresql.service "$(DESTDIR)"/usr/lib/systemd/system/openqa-scheduler.service.requires/postgresql.service install -d -m 755 "$(DESTDIR)"/usr/lib/systemd/system/openqa-websockets.service.requires ln -s ../postgresql.service "$(DESTDIR)"/usr/lib/systemd/system/openqa-websockets.service.requires/postgresql.service + install -D -m 644 contrib/completions/openqa-cli-completion.bash "$(DESTDIR)"/usr/share//bash-completion/completions/openqa-cli + install -D -m 644 contrib/completions/openqa-cli-completion.zsh "$(DESTDIR)"/usr/share/zsh/site-functions/_openqa-cli + if [ "$(SHELL)" = "/bin/bash" ] || [ "$(SHELL)" = "/usr/bin/bash" ]; then \ + echo "source $(DESTDIR)/usr/share/bash-completion/completions/openqa-cli" >> "$(HOME)/.bashrc"; \ + elif [ "$(SHELL)" = "/bin/zsh" ] || [ "$(SHELL)" = "/usr/bin/zsh" ]; then \ + echo "fpath+=($(DESTDIR)/usr/share/zsh/site-functions)" >> "$(HOME)/.zshrc"; \ + echo "autoload -Uz compinit && compinit" >> "$(HOME)/.zshrc"; \ + else \ + echo "Unsupported shell: $(SHELL). Skipping completion setup."; \ + fi + # # install openQA apparmor profile install -d -m 755 "$(DESTDIR)"/etc/apparmor.d diff --git a/openqa-cli-completion.bash b/contrib/completions/openqa-cli-completion.bash similarity index 98% rename from openqa-cli-completion.bash rename to contrib/completions/openqa-cli-completion.bash index 9b3bc32619e..c2eecc6d5ec 100644 --- a/openqa-cli-completion.bash +++ b/contrib/completions/openqa-cli-completion.bash @@ -1,5 +1,3 @@ -#!/usr/bin/env bash - _openqa_cli_completions() { local cur prev subcommands main_options api_options archive_options monitor_options schedule_options cur="${COMP_WORDS[COMP_CWORD]}" diff --git a/openqa-cli-completion.zsh b/contrib/completions/openqa-cli-completion.zsh similarity index 94% rename from openqa-cli-completion.zsh rename to contrib/completions/openqa-cli-completion.zsh index 287d0185b13..f0f33efcc6c 100644 --- a/openqa-cli-completion.zsh +++ b/contrib/completions/openqa-cli-completion.zsh @@ -1,4 +1,4 @@ -#!/usr/bin/env zsh +#compdef openqa-cli _openqa_cli_completions() { local curcontext="$curcontext" state line @@ -62,10 +62,10 @@ _openqa_cli_completions() { _arguments '*:option:->options' $main_options $archive_options ;; monitor) - _arguments '*:option:->options' $main_options $archive_options + _arguments '*:option:->options' $main_options $monitor_options ;; schedule) - _arguments '*:option:->options' $main_options $archive_options + _arguments '*:option:->options' $main_options $schedule_options ;; *) _values 'subcommand' api archive monitor schedule diff --git a/dist/rpm/openQA.spec b/dist/rpm/openQA.spec index 1af37ecb2c5..d6a0d5bbd05 100644 --- a/dist/rpm/openQA.spec +++ b/dist/rpm/openQA.spec @@ -314,6 +314,20 @@ Use this package to install munin scripts that allow to monitor some openQA statistics. %endif +%package client-bash-completion +Summary: Bash Completion for %{name} +Requires: bash-completion +Supplements: (%{name}-client and bash) + +%description client-bash-completion +The official bash completion script for openqa-cli. + +%package client-zsh-completion +Summary: Zsh Completion for %{name} +Supplements: (%{name}-client and zsh) + +%description client-zsh-completion +The official zsh completion script for openqa-cli. %prep %setup -q @@ -408,6 +422,10 @@ install -m 644 contrib/munin/config/minion.config %{buildroot}/%{_sysconfdir}/mu install -m 755 contrib/munin/utils/munin-mail %{buildroot}/%{_datadir}/openqa/script/munin-mail %endif +# completion +install -Dm 0644 contrib/completions/openqa-cli-completion.bash %{buildroot}%{_datadir}/bash-completion/completions/openqa-cli +install -Dm 0644 contrib/completions/openqa-cli-completion.zsh %{buildroot}%{_datadir}/zsh/site-functions/_openqa-cli + cd %{buildroot} grep -rl %{_bindir}/env . | while read file; do sed -e 's,%{_bindir}/env perl,%{_bindir}/perl,' -i $file @@ -673,6 +691,11 @@ fi %files devel %endif +%files client-bash-completion +%{_datadir}/bash-completion/completions/openqa-cli +%files client-zsh-completion +%{_datadir}/zsh/site-functions/_openqa-cli + %files common %dir %{_datadir}/openqa %{_datadir}/openqa/lib diff --git a/docs/UsersGuide.asciidoc b/docs/UsersGuide.asciidoc index 50389e19a22..6f2c40225c6 100644 --- a/docs/UsersGuide.asciidoc +++ b/docs/UsersGuide.asciidoc @@ -963,13 +963,12 @@ without needing to install openQA itself. Call `openqa-cli --help` for help. The sub-commands provide further help, e.g. `openqa-cli api --help` contains a lot of examples. -`openqa-cli` supports auto completion for `bash` and `zsh`. -If you use `bash` run or add the line below on your `.bashrc` profile. - -[source,sh] ----- -source openqa-cli-completion.bash ----- +`openqa-cli` supports auto completion for `bash` and `zsh` with openSUSE/SLE. +We distribute two packages `openQA-client-bash-completion` and +`openQA-client-zsh-completion` as part of openQA-client. In case the installed +system differs or source repo is used, you may still have auto-completion via +`make install`, which will install and source the completion script for one of +the above shells. This section focuses on particular API use-cases. Checkout the <> section for further information about