You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.8 KiB
123 lines
2.8 KiB
#!/usr/bin/env bash |
|
|
|
declare esc=$(printf '\033') |
|
declare c_reset="${esc}[0m" |
|
declare c_red="${esc}[31m" |
|
declare OPTIND |
|
declare opts=() |
|
declare id |
|
|
|
usage() { |
|
more <<'HELP' |
|
ix [OPTIONS] |
|
|
|
-l list all pastes, uses fzf for interactive use |
|
-d [id] delete the paste at [id] |
|
-i [id] replaces the paste with stdin |
|
-h print this help |
|
HELP |
|
} |
|
|
|
err() { |
|
printf "${c_red}%s${c_reset}\n" "$*" >&2 |
|
} |
|
|
|
die() { |
|
exit 1 |
|
} |
|
|
|
has() { |
|
local verbose=false |
|
if [[ $1 == '-v' ]]; then |
|
verbose=true |
|
shift |
|
fi |
|
for c in "$@"; do c="${c%% *}" |
|
if ! command -v "$c" &> /dev/null; then |
|
[[ "$verbose" == true ]] && err "$c not found" |
|
return 1 |
|
fi |
|
done |
|
} |
|
|
|
usage() { |
|
echo TODO |
|
exit |
|
} |
|
|
|
hasAccount() { |
|
grep -qF 'ix.io' ~/.netrc |
|
} |
|
|
|
createAccount() { |
|
echo "It seems you don't have a ~/.netrc with ix.io in it. Let's make one!" |
|
read -r -p 'enter a username: ' username |
|
read -rs -p 'enter a password (this will be hashed with sha256sum): ' password |
|
password=$(sha256sum <<< "$password" | awk '{print $1}') |
|
echo '' |
|
tee -a ~/.netrc <<< "machine ix.io login $username password $password" |
|
chmod 600 ~/.netrc |
|
echo "$username" |
|
} |
|
|
|
getUserName() { |
|
awk '"ix.io"==$2{print $4}' ~/.netrc |
|
} |
|
|
|
getPastes() { |
|
curl -s "http://ix.io/user/$1" | |
|
grep -A1 -P '\<a href="\/[a-zA-Z0-9]+"\>' | |
|
awk -F'--' ' |
|
BEGIN {FS="<a href=\""} |
|
/a href/{sub(/\">\[r\]<\/a>/, "\t", $2); printf "http://ix.io" $2} |
|
/@/{sub(/^\s+/, "", $0); print} |
|
' |
|
} |
|
|
|
listPastes() { |
|
getPastes "$(getUserName)" | fzf \ |
|
--inline-info --cycle \ |
|
--header='Ctrl-E = edit; Ctrl-V = view; Ctrl-D = delete' \ |
|
--preview='p={}; curl -s "${p## *}" | highlight -q --force -O ansi' \ |
|
--bind 'space:jump' \ |
|
--bind 'j:down' --bind 'k:up' \ |
|
--bind 'q:abort' \ |
|
--bind 'Ctrl-V:execute:p={}; p="${p## *}"; less -R < <(curl -s "$p" | highlight -q --force -O ansi) > /dev/tty' \ |
|
--bind 'Ctrl-E:execute:p={}; p="${p## *}"; curl -s "$p" | vipe > /dev/tty | ix -i "${p##*/}"' \ |
|
--bind 'Ctrl-D:execute:p={}; p="${p## *}"; ix -d "${p##*/}"' |
|
} |
|
|
|
has -v curl || die |
|
|
|
hasAccount || createAccount |
|
|
|
[[ -e ~/.netrc ]] && opts+=( '-n' ) |
|
|
|
while getopts ":hld:i:n:" x; do |
|
case "$x" in |
|
h) usage; exit ;; |
|
d) curl "${opts[@]}" -X DELETE "ix.io/$OPTARG"; exit ;; |
|
l) |
|
if [[ -z "$OPTARG" && -e ~/.netrc ]]; then |
|
listPastes | awk '{print $2}' | tee /dev/tty | xclip |
|
else |
|
die 'no netrc found' |
|
fi |
|
exit ;; |
|
i) opts+=( -X PUT ); id="$OPTARG" ;; |
|
n) opts+=( -F "read:1=$OPTARG" ) ;; |
|
esac |
|
done |
|
shift $(( OPTIND - 1)) |
|
|
|
if [[ -t 0 ]]; then |
|
if [[ -n "$1" ]]; then |
|
filename="$1" |
|
shift |
|
curl "${opts[@]}" -F f:1=@"$filename" "$@" "ix.io/$id" |
|
exit |
|
fi |
|
echo "^C to cancel, ^D to send." |
|
fi |
|
|
|
curl "${opts[@]}" -F f:1='<-' "$@" "ix.io/$id"
|
|
|