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

bash-completion: update and improve supported completion cases #4559

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions debian/snapcraft-completion
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
list_snapcraft_parts() {
# first get indentation
# find first line after 'parts:.*' ignoring comments and empty lines
# this is first part name, determine used indent from it
local indent=$(sed 's/\(.*\)#.*/\1/ ; /^[[:space:]]*$/d' ${1} | sed -n '/parts:.*/{n;p}' | sed 's/\([[:space:]]\+\).*/\1/')
# parse file in stages
#  print all line after 'parts:.*' ignoring comments and empty lines
# stop at first line not starting with white space
# only print lines starting with indent followed by non white space
sed 's/\(.*\)#.*/\1/ ; /^[[:space:]]*$/d ; 1,/parts:.*/d' ${1} | \
sed -n '/^[^[:space:]]/q;p' | \
sed -e 's/'"${indent}"'\(.*\):/\1/ ; /^[[:space:]]/d'
}

_snapcraft()
{
local cur prev opts
Expand All @@ -6,15 +20,15 @@ _snapcraft()
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="help init list-plugins plugins login logout export-login list-keys keys create-key register-key register registered list-registered push release clean cleanbuild pull build sign-build stage prime snap update define search gated validate history status close enable-ci expand-extensions extension extensions list-extensions"
opts="help init list-plugins plugins login logout export-login list-keys keys create-key register-key register registered list-registered push release clean cleanbuild pull build sign-build stage prime pack update define search gated validate history status close enable-ci expand-extensions extension extensions list-extensions --target-arch --build-for --debug --shell --shell-after ---provider -h --help --verbose"
Copy link
Contributor

Choose a reason for hiding this comment

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

Most of these new parameters should only be available if a lifecycle command has already been declared. For example, snapcraft plugins --build-for isn't valid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lengau that is fair point, let me see if this can be fixed in easy way

Copy link
Contributor

@lengau lengau Feb 15, 2024

Choose a reason for hiding this comment

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

Probably the easiest way would be to check whether any of the lifecycle commands are in $COMP_WORDS:

for i in "${COMP_WORDS}"; do
 case "$i" in  
  pull);&
  build);&
  stage);&
  prime);&
  pack);& 
  clean) 
    opts="${opts} --build-for --target-arch --debug --shell --shell-after --provider"
    break
 esac
done

Copy link
Contributor

Choose a reason for hiding this comment

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

@kubiko any update?


case "$prev" in
help)
plugins=$(snapcraft list-plugins)
COMPREPLY=( $(compgen -W "$plugins" -- $cur))
return 0
;;
snap)
pack)
_filedir -d
return 0
;;
Expand All @@ -26,6 +40,26 @@ _snapcraft()
COMREPLY=( travis )
return 0
;;
--target-arch | --build-for)
arch_options="arm64 armhf amd64 i386 ppc64el riscv64 s390x"
COMPREPLY=( $(compgen -W "$arch_options" -- $cur))
return 0
;;
build | clean | pull | stage | prime)
# first find snapcraft.yaml
for sy in snapcraft.yaml snap/snapcraft.yaml build-aux/snapcraft.yaml build-aux/snap/snapcraft.yaml
do
if [ -e ${sy} ]; then
snapcraft_yaml="${sy}"
break
fi
done
if [ -n "${snapcraft_yaml}" ]; then
parts_options=$(list_snapcraft_parts ${snapcraft_yaml})
COMPREPLY=( $(compgen -W "$parts_options" -- $cur))
fi
return 0
;;
*)
;;
esac
Expand Down
Loading