diff --git a/README.md b/README.md index 85ceec922..6348ed866 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This program provides command-line access to the B2 service. Version 0.3.11 -Usages: +# Usage b2 authorize_account [--dev | --staging | --production] [accountId] [applicationKey] @@ -131,3 +131,15 @@ Usages: b2 version Echos the version number of this program. + + +## Contrib + +You can find a [bash completion](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion) +script in the `contrib` directory. You can install it for example with + + [ ! -z $BASH_COMPLETION_DIR ] && sudo cp contrib/b2 $BASH_COMPLETION_DIR + +Once installed, you will be able to enter both commands and parameters just +typing some (unambiguous) prefix of them and having the shell complete the +rest by pressing the `tab` key. diff --git a/contrib/b2 b/contrib/b2 new file mode 100644 index 000000000..9819adae6 --- /dev/null +++ b/contrib/b2 @@ -0,0 +1,70 @@ +have b2 && +_b2 () { + local _b2_subcommands=( + 'authorize_account' + 'clear_account' + 'create_bucket' + 'delete_bucket' + 'delete_file_version' + 'download_file_by_id' + 'download_file_by_name' + 'get_file_info' + 'hide_file' + 'list_buckets' + 'list_file_names' + 'list_file_versions' + 'ls' + 'make_url' + 'update_bucket' + 'upload_file' + 'version' + ) + local _b2_lsopts=(--long --version) + local _b2_bucket_types=(allPrivate allPublic) + _b2_buckets() { + b2 list_buckets | cut -f1 -d' '; + } + _b2_files() { + b2 ls "$1" + } + + local wants_bucket="@(delete_bucket|hide_file|list_file_names|list_file_versions|update_bucket|upload_file|hide_file|download_file_by_name)" + local wants_bucket_type="@(create_bucket|update_bucket)" + local wants_file="@(download_file_by_name|hide_file|list_file_names|list_file_versions)" + + local cur prev words subcommand + _get_comp_words_by_ref cur prev words + + subcommand="${words[1]}" + + if [[ $COMP_CWORD -eq 1 ]]; then + COMPREPLY=($(compgen -W '${_b2_subcommands[@]}' -- "$cur")) + elif [[ $subcommand == ls ]]; then + if [[ $cur =~ ^- && $COMP_CWORD -eq 2 ]]; then + COMPREPLY=($(compgen -W '${_b2_lsopts[@]}' -- "$cur")) + elif [[ ( $prev =~ ^- && $COMP_CWORD -eq 3 ) || $COMP_CWORD -eq 2 ]]; then + COMPREPLY=($(compgen -W '$(_b2_buckets)' -- "$cur")) + fi + elif [[ $subcommand == create_bucket && $COMP_CWORD -eq 3 ]]; then + COMPREPLY=($(compgen -W '${_b2_bucket_types[@]}' -- "$cur")) + elif [[ $subcommand == $wants_bucket && $COMP_CWORD -eq 2 ]]; then + COMPREPLY=($(compgen -W '$(_b2_buckets)' -- "$cur")) + elif [[ $subcommand == $wants_bucket_type && $COMP_CWORD -eq 3 ]]; then + COMPREPLY=($(compgen -W '${_b2_bucket_types[@]}' -- "$cur")) + elif [[ $subcommand == upload_file && $COMP_CWORD -eq 3 ]]; then + _filedir + elif [[ $subcommand == $wants_file && $COMP_CWORD -eq 3 ]]; then + COMPREPLY=($(compgen -W '$(_b2_files $prev)' -- "$cur")) + fi + + # It's not clear how to complete fileName/fileId not having the bucket + # b2 delete_file_version + # b2 download_file_by_id + # b2 get_file_info + # b2 make_url + + unset -f _b2_buckets + unset -f _b2_files + return 0 +} && +complete -F _b2 b2