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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env bash
  2. declare esc=$(printf '\033')
  3. declare c_reset="${esc}[0m"
  4. declare c_red="${esc}[31m"
  5. declare OPTIND
  6. declare opts=()
  7. declare id
  8. usage() {
  9. more <<'HELP'
  10. ix [OPTIONS]
  11. -l list all pastes, uses fzf for interactive use
  12. -d [id] delete the paste at [id]
  13. -i [id] replaces the paste with stdin
  14. -h print this help
  15. HELP
  16. }
  17. err() {
  18. echo -e "${c_red}$1${c_reset}" >&2
  19. }
  20. die() {
  21. exit 1
  22. }
  23. has() {
  24. local verbose=false
  25. if [[ $1 == '-v' ]]; then
  26. verbose=true
  27. shift
  28. fi
  29. for c in "$@"; do c="${c%% *}"
  30. if ! command -v "$c" &> /dev/null; then
  31. [[ "$verbose" == true ]] && err "$c not found"
  32. return 1
  33. fi
  34. done
  35. }
  36. usage() {
  37. echo TODO
  38. exit
  39. }
  40. hasAccount() {
  41. grep -qF 'ix.io' ~/.netrc
  42. }
  43. createAccount() {
  44. echo "It seems you don't have a ~/.netrc with ix.io in it. Let's make one!"
  45. read -r -p 'enter a username: ' username
  46. read -rs -p 'enter a password (this will be hashed with sha256sum): ' password
  47. password=$(sha256sum <<< "$password" | awk '{print $1}')
  48. echo ''
  49. tee -a ~/.netrc <<< "machine ix.io login $username password $password"
  50. chmod 600 ~/.netrc
  51. echo "$username"
  52. }
  53. getUserName() {
  54. awk '"ix.io"==$2{print $4}' ~/.netrc
  55. }
  56. listPastes() {
  57. # TODO: needs better output. how should I extract dates, etc?
  58. curl -s "http://ix.io/user/$1" |
  59. grep -Po '\<a href="\/[a-zA-Z0-9]+"\>' |
  60. sed -r 's|<a href="/(\w+)">|http://ix.io/\1|'
  61. }
  62. has -v curl || die
  63. hasAccount || createAccount
  64. [[ -e ~/.netrc ]] && opts+=( '-n' )
  65. while getopts ":hld:i:n:" x; do
  66. case "$x" in
  67. h) usage; exit ;;
  68. d) curl "${opts[@]}" -X DELETE "ix.io/$OPTARG"; exit ;;
  69. l)
  70. if [[ -z "$OPTARG" && -e ~/.netrc ]]; then
  71. listPastes "$(getUserName)" | nl -p -s' ' -w1 | fzf \
  72. --inline-info --cycle \
  73. --header='Ctrl-E = edit; Ctrl-V = view; Ctrl-D = delete' \
  74. --preview='p={}; less < <(curl -s "${p##* }")' \
  75. --bind 'j:down' \
  76. --bind 'k:up' \
  77. --bind 'q:abort' \
  78. --bind 'Ctrl-V:execute:p={}; less < <(curl -s "${p##* }")' \
  79. --bind 'Ctrl-E:execute:p={}; curl -s "${p##* }" | vipe | ix -i "${p##*/}"' \
  80. --bind 'Ctrl-D:execute:p={}; ix -d "${p##*/}"'
  81. else
  82. die 'no netrc found'
  83. fi
  84. exit ;;
  85. i) opts+=( -X PUT ); id="$OPTARG" ;;
  86. n) opts+=( -F "read:1=$OPTARG" ) ;;
  87. esac
  88. done
  89. shift $(( OPTIND - 1))
  90. if [[ -t 0 ]]; then
  91. if [[ -n "$1" ]]; then
  92. filename="$1"
  93. shift
  94. curl "${opts[@]}" -F f:1=@"$filename" "$@" "ix.io/$id"
  95. exit
  96. fi
  97. echo "^C to cancel, ^D to send."
  98. fi
  99. curl "${opts[@]}" -F f:1='<-' "$@" "ix.io/$id"