diff options
| author | typebrook <typebrook@gmail.com> | 2020-01-28 13:29:13 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-01-28 13:29:13 +0800 |
| commit | 03203fe48451525d39f86bcb7ca57bd9b225a94e (patch) | |
| tree | c161dfd2010ac5c0738e2701eaf63f4b5ce387ac | |
| parent | 507e32d7f867db0c44d6f20adae6d30b15eb9584 (diff) | |
update
| -rwxr-xr-x | gist | 48 |
1 files changed, 26 insertions, 22 deletions
| @@ -55,30 +55,35 @@ | |||
| 55 | # TODO parallel branch works with json parsing on python | 55 | # TODO parallel branch works with json parsing on python |
| 56 | # TODO parallel branch works with wget and other stuff | 56 | # TODO parallel branch works with wget and other stuff |
| 57 | # TODO completion | 57 | # TODO completion |
| 58 | # TODO grep mode for description, file content | ||
| 58 | 59 | ||
| 59 | # Validate settings. | 60 | # Validate settings. |
| 60 | config=~/.config/gistrc | 61 | config=~/.config/gistrc |
| 61 | set -eo pipefail | 62 | set -eo pipefail |
| 62 | [ "$TRACE" ] && set -x | 63 | [ "$TRACE" ] && set -x |
| 63 | 64 | ||
| 64 | # TODO error handling while password is not true | ||
| 65 | # TODO support access token from input or web | ||
| 66 | _auth() { | 65 | _auth() { |
| 67 | local data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}" | 66 | echo 'Hi fellow! To access your gists, I need your Github username and a personal token with scope which allows "gist"!' |
| 68 | read -p "Github username: " user < /dev/tty | 67 | read -p "Github username: " user < /dev/tty |
| 69 | read -sp "Github password: " password < /dev/tty | 68 | if [[ $user =~ ^[[:alnum:]]+$ ]]; then |
| 70 | mkdir -p ~/.config && umask 0077 && echo user=$user > $config | 69 | mkdir -p ~/.config |
| 71 | 70 | umask 0077 | |
| 72 | curl -i https://api.github.com/authorizations \ | 71 | echo user=$user > $config |
| 73 | --user "$user:$password" \ | 72 | else |
| 74 | --data "$data" | 73 | echo "Not a valid username" |
| 75 | 74 | return 0 | |
| 76 | read -p "2-factor code: " OTP < /dev/tty | 75 | fi |
| 77 | curl https://api.github.com/authorizations \ | 76 | |
| 78 | --user "$user:$password" -H "X-GitHub-OTP: $OTP" \ | 77 | echo -n "Create a new token from web browser? [Y/n] " |
| 79 | --data "$data" \ | 78 | read answer < /dev/tty |
| 80 | | sed '1 s/[^{]//g' | jq -r .token \ | 79 | if [[ ! $answer =~ ^(N|n|No|NO|no)$ ]]; then |
| 81 | | sed 's/^/token=/' >> $config | 80 | python -mwebbrowser https://github.com/settings/tokens/new\?scopes\=gist; |
| 81 | fi | ||
| 82 | |||
| 83 | read -p "Paste your token here: " new_token < /dev/tty | ||
| 84 | [[ $new_token =~ ^[[:alnum:]]{40}$ ]] \ | ||
| 85 | && echo token=$new_token >> $config \ | ||
| 86 | || echo "Not a valid token" | ||
| 82 | } | 87 | } |
| 83 | 88 | ||
| 84 | case "$1" in | 89 | case "$1" in |
| @@ -200,14 +205,13 @@ _goto_gist() { | |||
| 200 | cd $folder/$GIST_ID && ls && tig --all 2> /dev/null | 205 | cd $folder/$GIST_ID && ls && tig --all 2> /dev/null |
| 201 | } | 206 | } |
| 202 | 207 | ||
| 203 | # TODO only remove deleted line | ||
| 204 | _delete_gist() { | 208 | _delete_gist() { |
| 205 | for i in "$@"; do | 209 | for i in "$@"; do |
| 206 | _gist_id "$i" | 210 | _gist_id "$i" |
| 207 | curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID \ | 211 | curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID \ |
| 208 | && echo "$i" deleted | 212 | && echo "$i" deleted \ |
| 213 | && sed -i -E "/^$i / d" $index | ||
| 209 | done | 214 | done |
| 210 | _update | ||
| 211 | } | 215 | } |
| 212 | 216 | ||
| 213 | # remove repos which are not in user gists anymore | 217 | # remove repos which are not in user gists anymore |
| @@ -240,8 +244,8 @@ _set_gist() { | |||
| 240 | esac | 244 | esac |
| 241 | done | 245 | done |
| 242 | if [[ "$1" == '--' ]]; then shift; fi | 246 | if [[ "$1" == '--' ]]; then shift; fi |
| 243 | # TODO could be simplified? | 247 | files="$@" |
| 244 | files="$@" && echo $files | xargs ls > /dev/null || exit 1 | 248 | ls $files > /dev/null || exit 1 |
| 245 | } | 249 | } |
| 246 | 250 | ||
| 247 | _new_file() { | 251 | _new_file() { |
| @@ -286,7 +290,7 @@ _edit_gist() { | |||
| 286 | 290 | ||
| 287 | echo -n 'Type new description: ' | 291 | echo -n 'Type new description: ' |
| 288 | read DESC < /dev/tty | 292 | read DESC < /dev/tty |
| 289 | jq -n --arg DESC "$DESC" '{ description: ($DESC) }' \ | 293 | echo "{ \"description\": \"$DESC\" }" \ |
| 290 | | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null \ | 294 | | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null \ |
| 291 | && _update | 295 | && _update |
| 292 | } | 296 | } |