diff options
author | typebrook <typebrook@gmail.com> | 2020-03-20 12:59:44 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-03-20 20:10:56 +0800 |
commit | 994b4f984a0c06a1e35a65bb953afc4220cd97f3 (patch) | |
tree | 89d404da586067a5b08a9591df3bf7187d1d3f00 | |
parent | 13fe88ceb0cfa973a7f886bc5987c306d335a9e8 (diff) |
Improve code readibility
-rwxr-xr-x | gist | 37 |
1 files changed, 17 insertions, 20 deletions
@@ -159,17 +159,18 @@ _configure() { | |||
159 | [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 | 159 | [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 |
160 | 160 | ||
161 | local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol' | 161 | local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol' |
162 | if [[ $1 =~ ^($valid_keys)$ ]]; then | 162 | local key=$1; local value="$2" |
163 | if [[ $1 == 'user' ]]; then | 163 | if [[ $key =~ ^($valid_keys)$ ]]; then |
164 | [[ -z $2 ]] && echo 'Must specify username' >&2 && return 1 | 164 | if [[ $key == 'user' ]]; then |
165 | elif [[ $1 == 'token' ]]; then | 165 | [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 |
166 | [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 | 166 | elif [[ $key == 'token' ]]; then |
167 | elif [[ $1 == 'auto_sync' ]]; then | 167 | [[ ${#value} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 |
168 | [[ ! $2 =~ ^(true|false)$ ]] && echo 'auto_sync must be either true or false' >&2 && return 1 | 168 | elif [[ $key == 'auto_sync' ]]; then |
169 | elif [[ $1 == 'protocol' ]]; then | 169 | [[ ! $value =~ ^(true|false)$ ]] && echo 'auto_sync must be either true or false' >&2 && return 1 |
170 | [[ ! $2 =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 | 170 | elif [[ $key == 'protocol' ]]; then |
171 | [[ ! $value =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 | ||
171 | fi | 172 | fi |
172 | local key=$1 && shift && local target=$key="'$*'" | 173 | shift && local target=$key="'$*'" |
173 | else | 174 | else |
174 | echo "Not a valid key for configuration, use <$valid_keys> instead." | 175 | echo "Not a valid key for configuration, use <$valid_keys> instead." |
175 | return 1 | 176 | return 1 |
@@ -216,15 +217,11 @@ _validate_config(){ | |||
216 | echo | 217 | echo |
217 | _ask_username && _ask_token && init=true | 218 | _ask_username && _ask_token && init=true |
218 | elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then | 219 | elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then |
219 | if ! (_ask_token); then | 220 | _ask_token && return 0 |
220 | echo 'To create/edit/delete a gist, a token is needed' | 221 | echo 'To create/edit/delete a gist, a token is needed' && return 1 |
221 | return 1 | ||
222 | fi | ||
223 | elif [[ -z $token && $1 =~ ^(f|fetch)$ && $2 =~ ^(s|star) ]]; then | 222 | elif [[ -z $token && $1 =~ ^(f|fetch)$ && $2 =~ ^(s|star) ]]; then |
224 | if ! (_ask_token); then | 223 | _ask_token && return 0 |
225 | echo 'To get user starred gists, a token is needed' | 224 | echo 'To get user starred gists, a token is needed' && return 1 |
226 | return 1 | ||
227 | fi | ||
228 | fi | 225 | fi |
229 | } | 226 | } |
230 | 227 | ||
@@ -295,7 +292,7 @@ _show_list() { | |||
295 | if [[ $tag == 'true' && -n $pin ]]; then | 292 | if [[ $tag == 'true' && -n $pin ]]; then |
296 | local pinned_tags=( $pin ) | 293 | local pinned_tags=( $pin ) |
297 | echo | 294 | echo |
298 | echo Pinned tags: ${pinned_tags[@]/#/#} | 295 | echo Pinned tags: "${pinned_tags[@]/#/#}" |
299 | elif [[ $hint == 'true' ]]; then | 296 | elif [[ $hint == 'true' ]]; then |
300 | echo | 297 | echo |
301 | echo 'run "gist fetch" to update gists or "gist help" for more details' > /dev/tty | 298 | echo 'run "gist fetch" to update gists or "gist help" for more details' > /dev/tty |
@@ -347,7 +344,7 @@ _parse_response() { | |||
347 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 344 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
348 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p | 345 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p |
349 | [[ -n $1 ]] && local index=${1}; index=${prefix}${index} | 346 | [[ -n $1 ]] && local index=${1}; index=${prefix}${index} |
350 | eval echo ${INDEX_FORMAT[@]/#/$} | 347 | eval echo "${INDEX_FORMAT[@]/#/$}" |
351 | done | 348 | done |
352 | } | 349 | } |
353 | 350 | ||