diff options
author | Hsieh Chin Fan <typebrook@gmail.com> | 2020-06-30 14:02:17 +0800 |
---|---|---|
committer | Hsieh Chin Fan <typebrook@gmail.com> | 2020-06-30 18:38:33 +0800 |
commit | b108934e8ee4875fc697712eb30d21ed4301f92b (patch) | |
tree | 4d5bcf57c5cad27662ccc7df5533afc1820d3464 | |
parent | cd07b16df332b37010cd082538abb2fb14882ce5 (diff) |
Reformat config file after source it
So user can read valid keys at once
-rwxr-xr-x | gist | 23 |
1 files changed, 18 insertions, 5 deletions
@@ -70,6 +70,8 @@ else | |||
70 | export mark=[^s] # By defaut, only process user's gists, not starred gist | 70 | export mark=[^s] # By defaut, only process user's gists, not starred gist |
71 | fi | 71 | fi |
72 | 72 | ||
73 | VALID_CONFIGS=('user=' 'token=' 'folder=' 'auto_sync=true|false' 'action=' 'EDITOR=' 'protocol=https|ssh' 'show_untagged=true|false') | ||
74 | |||
73 | # Default configuration | 75 | # Default configuration |
74 | [[ ! -t 1 && -z $hint ]] && hint=false | 76 | [[ ! -t 1 && -z $hint ]] && hint=false |
75 | protocol=https | 77 | protocol=https |
@@ -149,11 +151,10 @@ _process_json() { | |||
149 | _configure() { | 151 | _configure() { |
150 | [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 | 152 | [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 |
151 | 153 | ||
152 | local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol|show_untagged' | 154 | local key=$1; local value="$2"; local keys=${VALID_CONFIGS[@]%=*} |
153 | local key=$1; local value="$2" | ||
154 | 155 | ||
155 | [[ ! $key =~ ^($valid_keys)$ ]] \ | 156 | [[ ! " ${keys} " =~ " ${key} " ]] \ |
156 | && echo "Not a valid key for configuration, use <$valid_keys> instead." \ | 157 | && echo "Not a valid key for configuration, use <${keys// /|}> instead." \ |
157 | && return 1 | 158 | && return 1 |
158 | 159 | ||
159 | case $key in | 160 | case $key in |
@@ -202,8 +203,20 @@ _ask_token() { | |||
202 | 203 | ||
203 | # Check configuration is fine with user setting | 204 | # Check configuration is fine with user setting |
204 | _validate_config() { | 205 | _validate_config() { |
205 | # shellcheck source=/dev/null | ||
206 | source "$CONFIG" 2> /dev/null | 206 | source "$CONFIG" 2> /dev/null |
207 | sed -i -Ee '/^(user|token)=/!d' "$CONFIG" | ||
208 | for pair in "${VALID_CONFIGS[@]}"; do | ||
209 | key=${pair%=*}; [[ $key =~ ^(user|token)$ ]] && continue | ||
210 | value="${!key}" | ||
211 | values=${pair#*=} | ||
212 | if [[ -n $value && $value =~ ($values) ]]; then | ||
213 | echo -n $key="'$value'" | ||
214 | else | ||
215 | echo -n ${pair%|*} | ||
216 | fi | ||
217 | echo -e "${values:+\t\t# Valid: $values}" | ||
218 | done >>"$CONFIG" && source "$CONFIG" | ||
219 | |||
207 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 | 220 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 |
208 | if [[ -z $user ]]; then | 221 | if [[ -z $user ]]; then |
209 | echo 'Hi fellow! To access your gists, I need your Github username' | 222 | echo 'Hi fellow! To access your gists, I need your Github username' |