Skip to content

Instantly share code, notes, and snippets.

@arcizan
Last active June 6, 2016 07:54
Show Gist options
  • Save arcizan/60a2aaa16d079dd0294dbf43e2010bd8 to your computer and use it in GitHub Desktop.
Save arcizan/60a2aaa16d079dd0294dbf43e2010bd8 to your computer and use it in GitHub Desktop.
aws-cli wrapper for S3
#!/bin/zsh -fi
emulate -L zsh
setopt prompt_subst hist_reduce_blanks hist_ignore_dups hist_ignore_all_dups hist_ignore_space \
hist_expire_dups_first hist_save_no_dups # xtrace
typeset -r s3_scheme='s3:/'
typeset -r s3cli_home="${S3CLI_HOME:-$HOME/.s3cli}"
typeset -r s3cli_config_file="$s3cli_home/config"
typeset -r s3cli_hist_file="$s3cli_home/history"
typeset -r s3cli_dirs_file="$s3cli_home/dirs"
typeset -i s3cli_hist_size=10000
typeset -i s3cli_dirs_size=100
typeset -U s3cli_dirstack
s3cli_pwd='/'
s3cli_prompt=$'%B%F{8}[%B%F{10}%h%B%F{8}](%B%F{12}$s3_scheme$s3cli_pwd%B%F{8})\n> %b%f'
autoload -U {up,down}-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey '^N' down-line-or-beginning-search
bindkey '^P' up-line-or-beginning-search
[[ -d "$s3cli_home" ]] || mkdir -p $s3cli_home || exit $?
[[ -r "$s3cli_config_file" ]] && source $s3cli_config_file
[[ -r "$s3cli_dirs_file" ]] && s3cli_dirstack=( ${(f)"$(< $s3cli_dirs_file)"} )
tmp_dir=$(mktemp -d)
{
function s3(){
aws s3 $@
}
function s3cli_help(){
<<- EOF
Available commands:
cd DIR change present working directory
dirs show directory stack
ls [LS-OPTIONS] [PATH] list objects in PATH
cat FILE show contents of FILE
grep [GREP-OPTIONS] FILE grep FILE
help show this help
exit exit this script
EOF
}
function s3cli_cd(){
local new_dir
case "$1" in
-) new_dir=${s3cli_dirstack[1]};;
+<->) new_dir=${s3cli_dirstack[${1#+}]};;
*) new_dir=$(s3cli_get_path $1);;
esac
s3cli_ls $new_dir > /dev/null 2>&1 || { s3cli_error "no such directory: $new_dir"; return 1; }
s3cli_dirstack=( $s3cli_pwd ${s3cli_dirstack:#$new_dir} )
s3cli_pwd=$new_dir
print -lr -- ${(A)s3cli_dirstack::=${s3cli_dirstack[1,${s3cli_dirs_size}]}} >| $s3cli_dirs_file
}
function s3cli_dirs(){
[[ "${#s3cli_dirstack}" -gt 0 ]] && print -lr -- $s3cli_dirstack | cat -n
}
function s3cli_ls(){
local arg=${${@:#--*}[1]} suffix
[[ "$arg" == (|*/) ]] && suffix='/'
s3 ls ${(M)@:#--*} $s3_scheme$(s3cli_get_path $arg)$suffix
}
function s3cli_cat(){
local file=$(s3cli_get_path $1)
[[ -n "$file" ]] && s3cli_fetch $file && cat $tmp_dir/${file:t}
}
function s3cli_grep(){
s3cli_cat ${@[-1]} | grep --color=auto ${@[1,-2]}
}
function s3cli_get_path(){
case "$1" in
/*) print -r -- $1;;
*) print -r -- ${${${:-$s3cli_pwd/$1}:a}#$PWD};;
esac
}
function s3cli_fetch(){
s3 cp $s3_scheme$1 $tmp_dir > /dev/null
}
function s3cli_error(){
print -u 2 -P -- "%B%F{9}$*%b%f"
}
function s3cli_main(){
fc -ap $s3cli_hist_file $s3cli_hist_size
local cmd func
while true; do
vared -achep $s3cli_prompt cmd
if [[ "${cmd[1]}" == 'exit' ]]; then
print -s -- $cmd
return ${cmd[2]}
elif [[ -n ${func::=${(k)functions[s3cli_${cmd[1]}]}} ]]; then
$func ${cmd[2,-1]}
else
s3cli_error "command not found: ${cmd[1]}"
fi
print -s -- $cmd
cmd=
done
}
s3cli_main
} always {
rm -rf -- $tmp_dir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment