From 1fdf9ca65d16a910eb1d2274b5c78f131d908fbc Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 1 Jul 2020 07:58:51 +0800 Subject: Refactor configuation flow --- gist | 115 +++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 60 insertions(+), 55 deletions(-) diff --git a/gist b/gist index 0b5dee4..643e3f4 100755 --- a/gist +++ b/gist @@ -48,6 +48,8 @@ # Since now a gist is a local cloned repo # It is your business to do git commit and git push +# FIXME g g s | g l +# TODO IF in pipe, do not apply configure # TODO change gist from public to private or reverse versa # TODO feature to exclude tag-value or grep-string # TODO Save HEADER with wget and httpie @@ -55,14 +57,19 @@ configuredClient="" -NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name -GITHUB_API=https://api.github.com -GIST_DOMAIN=https://gist.github.com -CONFIG=~/.config/gist.conf; mkdir -p ~/.config -per_page=100 +declare -r NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name +declare -r GITHUB_API=https://api.github.com +declare -r GIST_DOMAIN=https://gist.github.com +declare -r CONFIG=~/.config/gist.conf; mkdir -p ~/.config +declare -r per_page=100 + +declare -ar INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') +declare -ar VALID_CONFIGS=('user' 'token' 'folder' 'auto_sync' 'action' 'EDITOR' 'protocol' 'show_untagged') +declare -A CONFIG_VALUES +CONFIG_VALUES[auto_sync]='true|false' +CONFIG_VALUES[protocol]='https|ssh' +CONFIG_VALUES[show_untagged]='true|false' -INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') -VALID_CONFIGS=('user=' 'token=' 'folder=' 'auto_sync=true|false' 'action=' 'EDITOR=' 'protocol=https|ssh' 'show_untagged=true|false') TAG_CHAR='-_[:alnum:]' if [[ ! -t 0 ]]; then INPUT=$(cat) @@ -73,7 +80,6 @@ fi # Default configuration [[ ! -t 1 && -z $hint ]] && hint=false -protocol=https init= # Shell configuration @@ -148,40 +154,26 @@ _process_json() { # Handle configuration cases _configure() { - [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 - - local key=$1; local value="$2"; local keys=${VALID_CONFIGS[@]%=*} + [[ $# == 0 ]] && ${EDITOR:-vi} $CONFIG && return 0 - [[ ! " ${keys} " =~ " ${key} " ]] \ - && echo "Not a valid key for configuration, use <${keys// /|}> instead." \ + local key=$1; local value="$2" + [[ ! " ${VALID_CONFIGS[@]} " =~ " ${key} " ]] \ + && echo "Not a valid key for configuration, use <${VALID_CONFIGS[@]}> instead." \ && return 1 case $key in user) - [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 ;; + while [[ ! $value =~ ^[[:alnum:]]+$ ]]; do + [[ -n $value ]] && echo "Invalid username" + read -r -p "Github username: " value &2 && return 1 ;; - auto_sync | show_untagged) - [[ ! $value =~ ^(true|false)$ ]] && echo "$key must be either true or false" >&2 && return 1 ;; - protocol) - [[ ! $value =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 ;; action) value="'$2'" esac - umask 0077 && touch "$CONFIG" - local target=$key="$value" - sed -i'' -e "/^$key=/ d" "$CONFIG" && [[ -n $target ]] && echo "$target" >> "$CONFIG" - cat "$CONFIG" -} - -# Prompt for username -_ask_username() { - while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do - [[ -n $user ]] && echo "Invalid username" - read -r -p "Github username: " user < /dev/tty - done - _configure user "$user" + echo $key="$value" >>$CONFIG } # Prompt for token @@ -195,48 +187,61 @@ _ask_token() { while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do [[ -n $token ]] && echo "Invalid token" - read -r -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty + read -r -p "Paste your token here (Ctrl-C to skip): " token >$CONFIG } # Check configuration is fine with user setting _validate_config() { - source "$CONFIG" 2> /dev/null - sed -i -Ee '/^(user|token)=/!d' "$CONFIG" - for pair in "${VALID_CONFIGS[@]}"; do - key=${pair%=*}; [[ $key =~ ^(user|token)$ ]] && continue - value="${!key}" - values=${pair#*=} - if [[ -n $value && $value =~ ($values) ]]; then - echo -n $key="'$value'" - else - echo -n ${pair%|*} - fi - echo -e "${values:+\t\t# Valid: $values}" - done >>"$CONFIG" && source "$CONFIG" - [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 if [[ -z $user ]]; then echo 'Hi fellow! To access your gists, I need your Github username' echo "Also a personal token with scope which allows \"gist\"!" echo - _ask_username && _ask_token && init=true + _configure user && _ask_token && init=true elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then - _ask_token && return 0 - echo 'To create/edit/delete a gist, a token is needed' && return 1 + echo 'To create/edit/delete a gist, a token is needed' >&2 && return 1 + _ask_token elif [[ -z $token && $1 =~ ^(f|fetch)$ && $2 =~ ^(s|star)$ ]]; then - _ask_token && return 0 - echo 'To get user starred gists, a token is needed' && return 1 + echo 'To get user starred gists, a token is needed' >&2 && return 1 + _ask_token fi + + [[ -z $folder || ! -w $(dirname "$folder") ]] && folder=~/gist && mkdir -p $folder + INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX +} + +# Apply current configuration into config file +_reformat_config() { + local line_token=$(sed -ne '/^token=/{p;q}' $CONFIG); line_token=${line_token:-token=} + for key in ${VALID_CONFIGS[@]}; do + [[ $key == token ]] && echo $line_token && continue # Because format may like this: token=$(COMMAND-TO-GET-TOKEN), just leave it untouched + local value="${!key}"; local valid_values=${CONFIG_VALUES[$key]} + if [[ -n "$value" ]] && [[ -z $valid_values || "$value" =~ ^($valid_values)$ ]]; then + echo -n $key="'$value'" + declare -g $key=$value # apply current value + else + echo -n $key= + declare -g $key=${valid_values%%|*} # apply the first valid value + fi + echo -e "${valid_values:+\t\t# Valid: $valid_values}" + done >"$CONFIG" } # Load configuration _apply_config() { + unset ${VALID_CONFIGS[@]} + if [[ -e $CONFIG ]]; then + source "$CONFIG" 2>/dev/null + else + umask 0077 && touch "$CONFIG" + fi + _validate_config "$@" || return 1 + _reformat_config - [[ -z $folder || ! -w $(dirname "$folder") ]] && folder=~/gist; mkdir -p $folder - INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX getConfiguredClient } @@ -887,7 +892,7 @@ case "$1" in _clean_repos ;; config | c) shift - _configure "$@" && { _apply_config config && _check_protocol &>/dev/null; } ;; + _configure "$@" && { _apply_config && (_check_protocol &>/dev/null &); } ;; user | u) shift _query_user "$@" ;; -- cgit v1.2.3-70-g09d2