No Description
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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. declare -r esc=$'\033'
  3. declare -r c_reset="${esc}[0m"
  4. declare -r c_red="${esc}[31m"
  5. declare OPTIND
  6. declare -a opts=( -\# )
  7. declare id
  8. usage() {
  9. LESS=-FEXR less <<'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. printf "${c_red}%s${c_reset}\n" "$@" >&2
  19. }
  20. die() {
  21. err "$@"
  22. exit 1
  23. }
  24. has() {
  25. local verbose=0
  26. if [[ $1 == '-v' ]]; then
  27. verbose=1
  28. shift
  29. fi
  30. for c; do c="${c%% *}"
  31. if ! command -v "$c" &> /dev/null; then
  32. (( verbose > 0 )) && err "$c not found"
  33. return 1
  34. fi
  35. done
  36. }
  37. select_from() {
  38. local cmd='command -v'
  39. for a; do
  40. case "$a" in
  41. -c) cmd="$2"; shift 2 ;;
  42. esac
  43. done
  44. for c; do
  45. if $cmd "${c%% *}" &> /dev/null; then
  46. echo "$c"
  47. return 0
  48. fi
  49. done
  50. return 1
  51. }
  52. has_account() {
  53. [[ -r ~/.netrc ]] && grep -qF 'ix.io' ~/.netrc
  54. }
  55. create_account() {
  56. echo "It seems you don't have a ~/.netrc with ix.io in it. Let's make one!"
  57. read -r -p 'enter a username: ' username
  58. read -rs -p 'enter a password (this will be hashed with sha256sum): ' password
  59. echo ''
  60. password=$(sha256sum <<< "$password")
  61. password="${password/% -}"
  62. printf "machine ix.io login %s password %s" "$username" "$password" >> ~/.netrc
  63. chmod 600 ~/.netrc
  64. if has_account; then
  65. echo 'success!'
  66. else
  67. die 'could not create account!'
  68. fi
  69. }
  70. get_user_name() {
  71. awk '"ix.io"==$2{print $4}' ~/.netrc
  72. }
  73. get_pastes() {
  74. curl -s "http://ix.io/user/$1" |
  75. grep -A1 -P '\<a href="\/[a-zA-Z0-9]+"\>' |
  76. awk -F'--' '
  77. BEGIN {FS="<a href=\""}
  78. /a href/{ sub(/\">\[r\]<\/a>/, "\t", $2); printf "http://ix.io" $2 }
  79. /@/{sub(/^\s+/, "", $0); print}
  80. '
  81. }
  82. list_pastes() {
  83. local highlighter
  84. highlighter=$(select_from 'highlight -q --force -O ansi' pygmentize cat)
  85. url=$(get_pastes "$(get_user_name)" | fzf \
  86. --inline-info --cycle \
  87. --header='Ctrl-E = edit; Ctrl-V = view; Ctrl-D = delete' \
  88. --preview="curl -s '{1}' | ${highlighter}" \
  89. --bind 'space:jump' \
  90. --bind 'q:abort' \
  91. --bind "Ctrl-V:execute:less -R < <(curl -s '{1}' ${highlighter:+ | $highlighter}) > /dev/tty" \
  92. --bind 'Ctrl-E:execute:p={1}; edit=$(curl -s "$p" | vipe); ix -i "${p##*/}" <<< "$edit"' \
  93. --expect='Ctrl-D')
  94. if [[ -n "$url" ]]; then
  95. mapfile -t res <<< "$url"
  96. if [[ "${res[0]}" = 'Ctrl-D' ]]; then
  97. id="${res[1]}"
  98. id="${id#*ix.io/}"
  99. id="${id%%$'\t'*}"
  100. ix -d "$id"
  101. list_pastes
  102. exit
  103. fi
  104. fi
  105. }
  106. has -v curl || die
  107. has_account || create_account
  108. [[ -e ~/.netrc ]] && opts+=( '-n' )
  109. while getopts ":hld:i:n:" x; do
  110. case "$x" in
  111. h) usage; exit ;;
  112. d) curl -s "${opts[@]}" -X DELETE "$OPTARG"; exit ;;
  113. l)
  114. if [[ -e ~/.netrc ]]; then
  115. url=$(list_pastes)
  116. [[ -n "$url" ]] && cut -f1 <<< "$url" | tee | xclip
  117. else
  118. die 'no netrc found'
  119. fi
  120. exit ;;
  121. i) opts+=( -X PUT ); id="$OPTARG" ;;
  122. n) opts+=( -F "read:1=$OPTARG" ) ;;
  123. esac
  124. done
  125. shift $(( OPTIND - 1 ))
  126. if [[ -t 0 ]]; then
  127. if [[ -n "$1" ]]; then
  128. filename="$1"
  129. shift
  130. response=$(curl "${opts[@]}" -F f:1=@"$filename" "$@" "ix.io/$id")
  131. clipboard=$(select_from 'xclip -r ' 'xsel')
  132. if [[ -n "$clipboard" ]]; then
  133. tee /dev/tty <<< "$response" | $clipboard
  134. else
  135. echo "$response"
  136. fi
  137. exit
  138. fi
  139. echo "^C to cancel, ^D to send."
  140. fi
  141. curl "${opts[@]}" -F f:1='<-' "$@" "ix.io/$id"