diff options
author | typebrook <typebrook@gmail.com> | 2020-05-14 16:42:07 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-05-14 16:42:07 +0800 |
commit | b4135ce0875e5cd5bfbb60897f73adefa57ee081 (patch) | |
tree | e3d71bcab20486512367a5c4d0fefcb95bdd9559 | |
parent | d9789e8e9b1e7b47a4b614fe1b31b647d99fb77b (diff) |
Refactor code in _configure()
-rwxr-xr-x | gist | 31 |
1 files changed, 16 insertions, 15 deletions
@@ -202,22 +202,23 @@ _configure() { | |||
202 | 202 | ||
203 | local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol|show_untagged' | 203 | local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol|show_untagged' |
204 | local key=$1; local value="$2" | 204 | local key=$1; local value="$2" |
205 | if [[ $key =~ ^($valid_keys)$ ]]; then | 205 | |
206 | if [[ $key == 'user' ]]; then | 206 | [[ ! $key =~ ^($valid_keys)$ ]] \ |
207 | [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 | 207 | && echo "Not a valid key for configuration, use <$valid_keys> instead." \ |
208 | elif [[ $key == 'token' ]]; then | 208 | && return 1 |
209 | [[ -n $value && ${#value} -ne 40 && ! $value =~ ^(\$|\`) ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 | 209 | |
210 | elif [[ $key =~ ^(auto_sync|show_untagged)$ ]]; then | 210 | case $key in |
211 | [[ ! $value =~ ^(true|false)$ ]] && echo "$key must be either true or false" >&2 && return 1 | 211 | user) |
212 | elif [[ $key == 'protocol' ]]; then | 212 | [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 ;; |
213 | [[ ! $value =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 | 213 | token) |
214 | elif [[ $key == 'action' ]]; then | 214 | [[ -n $value && ${#value} -ne 40 && ! $value =~ ^(\$|\`) ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 ;; |
215 | auto_sync | show_untagged) | ||
216 | [[ ! $value =~ ^(true|false)$ ]] && echo "$key must be either true or false" >&2 && return 1 ;; | ||
217 | protocol) | ||
218 | [[ ! $value =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 ;; | ||
219 | action) | ||
215 | value="'$2'" | 220 | value="'$2'" |
216 | fi | 221 | esac |
217 | else | ||
218 | echo "Not a valid key for configuration, use <$valid_keys> instead." | ||
219 | return 1 | ||
220 | fi | ||
221 | 222 | ||
222 | umask 0077 && touch "$CONFIG" | 223 | umask 0077 && touch "$CONFIG" |
223 | local target=$key="$value" | 224 | local target=$key="$value" |