| 1 |
_s4cmd () |
| 2 |
{ |
| 3 |
local IFS=$' \n' |
| 4 |
local cur prev words cword possibleparams |
| 5 |
command=${COMP_WORDS[1]} |
| 6 |
prev=${COMP_WORDS[COMP_CWORD-1]} |
| 7 |
prevprev=${COMP_WORDS[COMP_CWORD-2]} |
| 8 |
_init_completion || return |
| 9 |
|
| 10 |
if [[ $cword -eq 1 ]]; then |
| 11 |
#command |
| 12 |
COMPREPLY=($( compgen -W 'ls put get cp mv sync del du' -- "$cur" )) |
| 13 |
COMPREPLY=("${COMPREPLY[@]/%/ }") |
| 14 |
elif [[ $cword -ge 2 ]]; then |
| 15 |
params=${COMP_WORDS[@]:2} |
| 16 |
if [ "$prevprev" == "s3" ] |
| 17 |
then |
| 18 |
#get initial bucket names |
| 19 |
if [[ $cur =~ ^\/\/$ ]]; then |
| 20 |
buckets=($(s4cmd ls 2>/dev/null | grep -Eo "s3://.*" | grep -Eo "//.*")) |
| 21 |
COMPREPLY=($(compgen -W "$buckets" -- "$cur")) |
| 22 |
return 0 |
| 23 |
|
| 24 |
#help get buckets full names |
| 25 |
elif [[ $cur =~ ^\/\/[^\/]*$ ]]; then |
| 26 |
buckets=($(s4cmd ls 2>/dev/null | grep -Eo "s3://.*" | grep -Eo "//.*")) |
| 27 |
COMPREPLY=($(compgen -W "$buckets" -- "$cur")) |
| 28 |
return 0 |
| 29 |
|
| 30 |
#get contents of dir |
| 31 |
elif [[ $cur =~ ^\/\/.*\/$ ]]; then |
| 32 |
buckets=($(s4cmd ls s3:$cur 2>/dev/null | grep -Eo "s3://.*" | grep -Eo "//.*")) |
| 33 |
COMPREPLY=($(compgen -W "$buckets" -- "$cur")) |
| 34 |
return 0 |
| 35 |
|
| 36 |
#help get buckets contents |
| 37 |
elif [[ $cur =~ ^\/\/.*\/.*$ ]]; then |
| 38 |
checkdir=($(dirname "$cur")) |
| 39 |
buckets=($(s4cmd ls s3:$checkdir 2>/dev/null | grep -Eo "s3://.*" | grep -Eo "//.*")) |
| 40 |
COMPREPLY=($(compgen -W "$buckets" -- "$cur")) |
| 41 |
return 0 |
| 42 |
fi |
| 43 |
fi |
| 44 |
case "$command" in |
| 45 |
ls) |
| 46 |
COMPREPLY=($(compgen -W "--recursive --show-directory" -- "$cur")) |
| 47 |
COMPREPLY=("${COMPREPLY[@]/%/ }") |
| 48 |
return 0 |
| 49 |
;; |
| 50 |
put|get|sync|cp|mv) |
| 51 |
COMPREPLY=($(compgen -W "--recursive --sync-check --force --dry-run" -- "$cur")) |
| 52 |
COMPREPLY=("${COMPREPLY[@]/%/ }") |
| 53 |
return 0 |
| 54 |
;; |
| 55 |
del) |
| 56 |
COMPREPLY=($(compgen -W "--recursive --dry-run" -- "$cur")) |
| 57 |
COMPREPLY=("${COMPREPLY[@]/%/ }") |
| 58 |
return 0 |
| 59 |
;; |
| 60 |
du) |
| 61 |
COMPREPLY=($(compgen -W "--recursive" -- "$cur")) |
| 62 |
COMPREPLY=("${COMPREPLY[@]/%/ }") |
| 63 |
return 0 |
| 64 |
;; |
| 65 |
esac |
| 66 |
fi |
| 67 |
} && |
| 68 |
complete -o nospace -o default -F _s4cmd s4cmd |
| 69 |
|