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.

ix 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bash
  2. declare OPTIND
  3. declare opts=()
  4. declare id
  5. set -e
  6. usage() {
  7. echo TODO
  8. exit
  9. }
  10. hasAccount() {
  11. grep -qF 'ix.io' ~/.netrc
  12. }
  13. createAccount() {
  14. echo "It seems you don't have a ~/.netrc with ix.io in it. Let's make one!"
  15. read -r -p 'enter a username: ' username
  16. read -rs -p 'enter a password (this will be hashed with sha256sum): ' password
  17. password=$(sha256sum <<< "$password" | awk '{print $1}')
  18. echo ''
  19. tee -a ~/.netrc <<< "machine ix.io login $username password $password"
  20. chmod 600 ~/.netrc
  21. echo "$username"
  22. }
  23. getUserName() {
  24. awk '"ix.io"==$2{print $4}' ~/.netrc
  25. }
  26. listPastes() {
  27. # TODO: needs better output. how should I extract dates, etc?
  28. curl -s "http://ix.io/user/$1" |
  29. grep -Po '\<a href="\/[a-zA-Z0-9]+"\>' |
  30. sed -r 's|<a href="/(\w+)">|http://ix.io/\1|'
  31. }
  32. hasAccount || createAccount
  33. [[ -e ~/.netrc ]] && opts+=( '-n' )
  34. while getopts ":hld:i:n:" x; do
  35. case "$x" in
  36. d) curl "${opts[@]}" -X DELETE "ix.io/$OPTARG"; exit ;;
  37. l)
  38. if [[ -z "$OPTARG" && -e ~/.netrc ]]; then
  39. listPastes "$(getUserName)" | nl -p -s' ' -w1 | fzf \
  40. --inline-info --cycle \
  41. --header='Ctrl-E = edit; Ctrl-V = view; Ctrl-D = delete' \
  42. --bind 'j:down' \
  43. --bind 'k:up' \
  44. --bind 'q:abort' \
  45. --bind 'Ctrl-V:execute:p={}; less < <(curl -s "${p##* }")' \
  46. --bind 'Ctrl-E:execute:p={}; curl -s "${p##* }" | vipe | ix -i "${p##*/}"' \
  47. --bind 'Ctrl-D:execute:p={}; ix -d "${p##*/}"'
  48. else
  49. die 'no netrc found'
  50. fi
  51. exit ;;
  52. i) opts+=( -X PUT ); id="$OPTARG" ;;
  53. n) opts+=( -F "read:1=$OPTARG" ) ;;
  54. esac
  55. done
  56. shift $(( OPTIND - 1))
  57. if [[ -t 0 ]]; then
  58. if [[ -n "$1" ]]; then
  59. filename="$1"
  60. shift
  61. curl "${opts[@]}" -F f:1=@"$filename" "$@" "ix.io/$id"
  62. exit
  63. fi
  64. echo "^C to cancel, ^D to send."
  65. fi
  66. curl "${opts[@]}" -F f:1='<-' "$@" "ix.io/$id"