From b4135ce0875e5cd5bfbb60897f73adefa57ee081 Mon Sep 17 00:00:00 2001 From: typebrook Date: Thu, 14 May 2020 16:42:07 +0800 Subject: Refactor code in _configure() --- gist | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/gist b/gist index b9a607b..fc06889 100755 --- a/gist +++ b/gist @@ -202,22 +202,23 @@ _configure() { local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol|show_untagged' local key=$1; local value="$2" - if [[ $key =~ ^($valid_keys)$ ]]; then - if [[ $key == 'user' ]]; then - [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 - elif [[ $key == 'token' ]]; then - [[ -n $value && ${#value} -ne 40 && ! $value =~ ^(\$|\`) ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 - elif [[ $key =~ ^(auto_sync|show_untagged)$ ]]; then - [[ ! $value =~ ^(true|false)$ ]] && echo "$key must be either true or false" >&2 && return 1 - elif [[ $key == 'protocol' ]]; then - [[ ! $value =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 - elif [[ $key == 'action' ]]; then + + [[ ! $key =~ ^($valid_keys)$ ]] \ + && echo "Not a valid key for configuration, use <$valid_keys> instead." \ + && return 1 + + case $key in + user) + [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 ;; + token) + [[ -n $value && ${#value} -ne 40 && ! $value =~ ^(\$|\`) ]] && echo 'Invalid token format, it is not 40 chars' >&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'" - fi - else - echo "Not a valid key for configuration, use <$valid_keys> instead." - return 1 - fi + esac umask 0077 && touch "$CONFIG" local target=$key="$value" -- cgit v1.2.3-70-g09d2