From 03203fe48451525d39f86bcb7ca57bd9b225a94e Mon Sep 17 00:00:00 2001 From: typebrook Date: Tue, 28 Jan 2020 13:29:13 +0800 Subject: update --- gist | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/gist b/gist index f0816e5..b82fa0a 100755 --- a/gist +++ b/gist @@ -55,30 +55,35 @@ # TODO parallel branch works with json parsing on python # TODO parallel branch works with wget and other stuff # TODO completion +# TODO grep mode for description, file content # Validate settings. config=~/.config/gistrc set -eo pipefail [ "$TRACE" ] && set -x -# TODO error handling while password is not true -# TODO support access token from input or web _auth() { - local data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}" + echo 'Hi fellow! To access your gists, I need your Github username and a personal token with scope which allows "gist"!' read -p "Github username: " user < /dev/tty - read -sp "Github password: " password < /dev/tty - mkdir -p ~/.config && umask 0077 && echo user=$user > $config - - curl -i https://api.github.com/authorizations \ - --user "$user:$password" \ - --data "$data" - - read -p "2-factor code: " OTP < /dev/tty - curl https://api.github.com/authorizations \ - --user "$user:$password" -H "X-GitHub-OTP: $OTP" \ - --data "$data" \ - | sed '1 s/[^{]//g' | jq -r .token \ - | sed 's/^/token=/' >> $config + if [[ $user =~ ^[[:alnum:]]+$ ]]; then + mkdir -p ~/.config + umask 0077 + echo user=$user > $config + else + echo "Not a valid username" + return 0 + fi + + echo -n "Create a new token from web browser? [Y/n] " + read answer < /dev/tty + if [[ ! $answer =~ ^(N|n|No|NO|no)$ ]]; then + python -mwebbrowser https://github.com/settings/tokens/new\?scopes\=gist; + fi + + read -p "Paste your token here: " new_token < /dev/tty + [[ $new_token =~ ^[[:alnum:]]{40}$ ]] \ + && echo token=$new_token >> $config \ + || echo "Not a valid token" } case "$1" in @@ -200,14 +205,13 @@ _goto_gist() { cd $folder/$GIST_ID && ls && tig --all 2> /dev/null } -# TODO only remove deleted line _delete_gist() { for i in "$@"; do _gist_id "$i" curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID \ - && echo "$i" deleted + && echo "$i" deleted \ + && sed -i -E "/^$i / d" $index done - _update } # remove repos which are not in user gists anymore @@ -240,8 +244,8 @@ _set_gist() { esac done if [[ "$1" == '--' ]]; then shift; fi - # TODO could be simplified? - files="$@" && echo $files | xargs ls > /dev/null || exit 1 + files="$@" + ls $files > /dev/null || exit 1 } _new_file() { @@ -286,7 +290,7 @@ _edit_gist() { echo -n 'Type new description: ' read DESC < /dev/tty - jq -n --arg DESC "$DESC" '{ description: ($DESC) }' \ + echo "{ \"description\": \"$DESC\" }" \ | curl -X PATCH -H "$auth_header" --data @- $github_api/gists/$GIST_ID > /dev/null \ && _update } -- cgit v1.2.3-70-g09d2