From b7ce476f3eb425754427445901ff813925fffdd8 Mon Sep 17 00:00:00 2001 From: typebrook Date: Wed, 22 Jan 2020 15:42:58 +0800 Subject: update --- gist | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 23 deletions(-) diff --git a/gist b/gist index ad08016..da30e4b 100755 --- a/gist +++ b/gist @@ -14,7 +14,7 @@ # gist (update | u) [star | s] # # * list your gists with format: [number] [url] [file_num] [comment_num] [short description] -# gist +# gist [star | s] # # * clone gist repos which are not in local # * pull master branch if a local repo is behind its remote @@ -48,31 +48,52 @@ # * show this help message # gist (help | h) -# define your environmemnts here -# TODO support auth prompt, remove personal info here +# TODO pipe syntax fix # TODO error handling, unit test # TODO parallel branch works with json parsing on python # TODO parallel branch works with wget and other stuff -# completion -#------------------- -github_api_token=$(cat $SETTING_DIR/tokens/github) -user=typebrook -folder=~/git/gist -#------------------- +# TODO completion + +# Validate settings. +config=~/.config/gistrc +[ "$TRACE" ] && set -x + +# TODO error handling 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 && umask 0077 && echo user=$user > $config + + curl https://api.github.com/authorizations \ + --user "$user:$password" \ + --data "$data" > /dev/null + + read -p "2-factor code: " OTP + 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 +} + +while ! source $config; do + _auth +done github_api=https://api.github.com -auth_header="Authorization: token $github_api_token" +auth_header="Authorization: token $token" + +[[ -z "$folder" ]] && folder=~/git/gist mkdir -p $folder index=$folder/index starred=$folder/starred -# Validate settings. -[ "$TRACE" ] && set -x - # Show the list of gist, but not updated time +# TODO a way to show files # TODO show git status outdated _show_list() { - if [[ ! -e $1 ]]; then + if [[ ! -e "$1" ]]; then echo No local file found for last update echo Please run command: echo " gist update" @@ -106,7 +127,7 @@ _update() { curl -s -H "$auth_header" $github_api/$route |\ _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \ _show_list $list_file - (_sync_repos $1 > /dev/null 2>&1 &) + if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi } # TODO check if a user create a very first gist @@ -144,7 +165,7 @@ _sync_repos() { _gist_id() { GIST_ID=$(cat $index $starred | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') - if [[ -z $GIST_ID ]]; then + if [[ -z "$GIST_ID" ]]; then echo -e "Not a valid index: \e[31m$1\e[0m" echo Use the index number in the first column instead: echo @@ -153,7 +174,7 @@ _gist_id() { fi } -# FIXME error handling +# FIXME error handling, if repo not cloned yet _goto_gist() { _gist_id $1 echo This gist is at $folder/$GIST_ID @@ -192,7 +213,7 @@ _show_detail() { } _set_gist() { - while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in + while [[ "$1" =~ ^- && "$1" != "--" ]]; do case $1 in -d | --desc) description="$2" shift; shift;; @@ -202,7 +223,7 @@ _set_gist() { esac done if [[ "$1" == '--' ]]; then shift; fi - files=$@ + files="$@" && echo $files | xargs ls > /dev/null || exit 1 } _new_file() { @@ -216,11 +237,11 @@ _new_file() { } # create a new gist with files -# FIXME error handling if gist is not created +# FIXME error handling if gist is not created, file doesn't exist _create_gist() { _set_gist "$@" - [[ -z $files ]] && files=$(_new_file $filename) - [[ -z $description ]] && echo -n 'Type description: ' && read description + [[ -z "$files" ]] && files=$(_new_file $filename) + [[ -z "$description" ]] && echo -n 'Type description: ' && read description for file in $files; do FILE=$(basename $file) @@ -254,6 +275,31 @@ _help_message() { sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 } +_cases() { + if [[ $1 == 'token' ]]; then + [[ ${#2} -eq 40 ]] && echo token=$2 ||\ + echo Invalid token format, it is not 40 chars '\n' > /dev/tty + elif [[ $1 == 'auto_sync' ]]; then + [[ $2 == 'false' ]] && echo $1=$2 ||\ + echo $1=true + elif [[ $1 == 'folder' ]]; then + [[ -n "$2" ]] && echo $1=$2 ||\ + echo $1=~/gist + elif [[ $1 == 'user' ]]; then + echo $1=$2 + fi +} + +# TODO consider the case that $2 is empty -> remove original setting +_configure() { + [[ -z "$@" ]] && (vim $config) && exit 0 + target=$(_cases "$@") + + [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $config && echo $target >> $config + cat $config +} + + case "$1" in "") _show_list $index ;; @@ -269,12 +315,16 @@ case "$1" in sync | S) _sync_repos ;; detail | d) - _show_detail "$2" ;; + shift + _show_detail "$@" ;; delete | D) shift _delete_gist "$@" ;; clean | C) _clean_repos ;; + config | c) + shift + _configure "$@" ;; help | h) _help_message ;; *) -- cgit v1.2.3-70-g09d2