From a9d2f988ebb1aa92c2d0702978df5e18bc37f370 Mon Sep 17 00:00:00 2001 From: typebrook Date: Tue, 21 Jan 2020 15:43:55 +0800 Subject: update --- scripts/gist | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'scripts/gist') diff --git a/scripts/gist b/scripts/gist index 8f89102..3b98c24 100755 --- a/scripts/gist +++ b/scripts/gist @@ -55,11 +55,34 @@ # TODO completion # define your environmemnts here #------------------- -github_api_token=$(cat $SETTING_DIR/tokens/gist) -user=typebrook folder=~/git/gist +config=~/.config/gist #------------------- +# while password is not true +_auth() { + data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}" + read -p "Github username: " user + read -sp "Github password: " password + mkdir -p ~/.config + echo user=$user > $config + + curl -s https://api.github.com/authorizations \ + --user "$user:$password" \ + --data "$data" > /dev/null + + read -p "2-factor code: " OTP + curl -s https://api.github.com/authorizations \ + --user "$user:$password" -H "X-GitHub-OTP: $OTP" \ + --data "$data" |\ + sed '1 s/[^{]//g' | jq -r .token |\ + sed 's/^/github_api_token=/' >> $config +} + +while ! source $config || [[ -z "$user" ]]; do + _auth +done + github_api=https://api.github.com auth_header="Authorization: token $github_api_token" mkdir -p $folder @@ -254,20 +277,6 @@ _help_message() { sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 } -_auth() { - data="{\"scopes\":[\"gist\"], \"note\": \"gist-$(date -u +'%Y-%m-%dT%H:%M:%SZ')\"}" - read -p "Github username: " user - read -sp "Github password: " password - curl https://api.github.com/authorizations \ - --user "$user:$password" \ - --data "$data" - - read -sp "2-factor code: " OTP - curl https://api.github.com/authorizations \ - --user "$user:$password" -H "X-GitHub-OTP: $OTP" \ - --data "$data" -} - case "$1" in "") _show_list $index ;; -- cgit v1.2.3-70-g09d2