-
Notifications
You must be signed in to change notification settings - Fork 37
feat: add bash completion for p11* tools #66
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
Draft
mdevolde
wants to merge
5
commits into
Mastercard:master
Choose a base branch
from
mdevolde:completion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bb6505
feat: add bash completion for p11* tools
mdevolde c4b8afb
fix (completion): spelling correction
mdevolde 0c33c48
fix (completion): adding a missing space
mdevolde 6d9dc28
fix (completion): wrong command name in doc
mdevolde 24b40c4
Merge branch 'Mastercard:master' into completion
mdevolde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Common functions for p11* completion scripts -*- shell-script -*- | ||
| # | ||
| # Description: | ||
| # This file provides reusable Bash functions to support autocompletion | ||
| # for various `p11*` CLI tools (p11cat, p11cp, p11mv, etc.). | ||
| # | ||
| # Functions parse the command-line arguments dynamically and provide | ||
| # object filtering and completion based on loaded tokens via `p11ls`. | ||
| # | ||
| # Functions: | ||
| # - __p11_parse_args(slot_ref, pin_ref, lib_ref) | ||
| # Parses -l (lib), -s (slot), -p (pin) from COMP_WORDS into referenced variables | ||
| # | ||
| # - __p11_complete_filters(cur) | ||
| # Uses parsed lib/slot/pin to call p11ls and generate autocompletion candidates | ||
| # with existing filters. | ||
| # | ||
| # Environment: | ||
| # These variables are used as fallback if -l, -s or -p are not given: | ||
| # PKCS11LIB, PKCS11SLOT, PKCS11PASSWORD | ||
| # | ||
| # Limitations: | ||
| # | ||
| # Inline environment variable assignments like the following: | ||
| # PKCS11LIB=/usr/lib/softhsm/libsofthsm2.so p11ls -s 0 -p 1234 | ||
| # | ||
| # will NOT be detected by Bash completion logic, because these assignments | ||
| # are not part of the shell's environment when autocompletion is triggered. | ||
| # | ||
| # Instead, define them in the environment using `export`: | ||
| # export PKCS11LIB=/usr/lib/softhsm/libsofthsm2.so | ||
| # export PKCS11SLOT=0 | ||
| # export PKCS11PASSWORD=1234 | ||
| # p11ls <TAB> | ||
| # | ||
| # This ensures that completion functions can access the variables reliably. | ||
| # | ||
| # Usage: | ||
| # Must be sourced by specific completion scripts, e.g.: | ||
| # source /usr/share/bash-completion/completions/p11-common | ||
|
|
||
| __p11_parse_args() { | ||
| local -n _slot=$1 _pin=$2 _lib=$3 | ||
| _slot=""; _pin=""; _lib="" | ||
|
|
||
| for ((i=0; i < ${#COMP_WORDS[@]}; i++)); do | ||
| case "${COMP_WORDS[i]}" in | ||
| -s) _slot="${COMP_WORDS[i+1]}" ;; | ||
| -p) _pin="${COMP_WORDS[i+1]}" ;; | ||
| -l) _lib="${COMP_WORDS[i+1]}" ;; | ||
| esac | ||
| done | ||
|
|
||
| # Fallback to env vars if CLI args not provided | ||
| [[ -z "$_lib" && -n "$PKCS11LIB" ]] && _lib="$PKCS11LIB" | ||
| [[ -z "$_slot" && -n "$PKCS11SLOT" ]] && _slot="$PKCS11SLOT" | ||
| [[ -z "$_pin" && -n "$PKCS11PASSWORD" ]] && _pin="$PKCS11PASSWORD" | ||
| } | ||
|
|
||
| __p11_complete_filters() { | ||
| local slot pin lib cur="$1" | ||
| __p11_parse_args slot pin lib | ||
|
|
||
| if [[ -n "$lib" && -n "$slot" && -n "$pin" ]]; then | ||
| local objects | ||
| objects=$(p11ls -l "$lib" -s "$slot" -p "$pin" 2>/dev/null | awk '{print $1}') | ||
| COMPREPLY=( $(compgen -W "${objects}" -- "${cur}") ) | ||
| fi | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11cat(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11cat`. | ||
| # Provides dynamic suggestions for PKCS#11 object filters | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11cat -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11cat() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11cat p11cat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11cp(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11cp`. | ||
| # Provides dynamic suggestions for the SOURCE argument | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11cp -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11cp() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11cp p11cp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11kcv(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11kcv`. | ||
| # Provides dynamic suggestions for PKCS#11 object filters | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11kcv -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11kcv() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11kcv p11kcv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11ls(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11ls`. | ||
| # Provides dynamic suggestions for PKCS#11 object filters | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11ls -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11ls() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11ls p11ls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11more(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11more`. | ||
| # Provides dynamic suggestions for PKCS#11 object filters | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11more -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11more() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11more p11more |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11mv(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11mv`. | ||
| # Provides dynamic suggestions for the SOURCE argument | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11mv -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11mv() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11mv p11mv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11od(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11od`. | ||
| # Provides dynamic suggestions for PKCS#11 object filters | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11od -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11od() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11od p11od |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # p11setattr(1) completion -*- shell-script -*- | ||
| # | ||
| # Bash autocompletion for `p11setattr`. | ||
| # Provides dynamic suggestions for PKCS#11 object filters | ||
| # based on current lib, slot, and pin arguments. | ||
| # | ||
| # Depends on: | ||
| # - ./p11-common | ||
| # | ||
| # Usage: | ||
| # Autocompletes when user types: | ||
| # p11setattr -l <lib> -s <slot> -p <pin> <TAB> | ||
|
|
||
| source "$(dirname "${BASH_SOURCE[0]}")/p11-common" | ||
|
|
||
| _p11setattr() { | ||
| local cur="${COMP_WORDS[COMP_CWORD]}" | ||
| __p11_complete_filters "$cur" | ||
| } | ||
|
|
||
| complete -F _p11setattr p11setattr |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to have support for slot selection. That would require to modify
p11slotinfoand add an option to enumerate slots and token, for completion.