diff options
author | Hsieh Chin Fan <typebrook@gmail.com> | 2020-07-01 23:34:00 +0800 |
---|---|---|
committer | Hsieh Chin Fan <typebrook@gmail.com> | 2020-07-01 23:34:00 +0800 |
commit | 54e79536b1810d80f0df5d3611936bbb86fae79c (patch) | |
tree | b1d19f9200c26026e39015d1ccf8fbc9b8f28c22 | |
parent | 2b75c047ee1b1efcab93af41fb2c84600e3770a8 (diff) | |
parent | 1fdf9ca65d16a910eb1d2274b5c78f131d908fbc (diff) |
Merge branch 'config'
-rwxr-xr-x | gist | 115 |
1 files changed, 60 insertions, 55 deletions
@@ -48,6 +48,8 @@ | |||
48 | # Since now a gist is a local cloned repo | 48 | # Since now a gist is a local cloned repo |
49 | # It is your business to do git commit and git push | 49 | # It is your business to do git commit and git push |
50 | 50 | ||
51 | # FIXME g g s | g l | ||
52 | # TODO IF in pipe, do not apply configure | ||
51 | # TODO change gist from public to private or reverse versa | 53 | # TODO change gist from public to private or reverse versa |
52 | # TODO feature to exclude tag-value or grep-string | 54 | # TODO feature to exclude tag-value or grep-string |
53 | # TODO Save HEADER with wget and httpie | 55 | # TODO Save HEADER with wget and httpie |
@@ -55,14 +57,19 @@ | |||
55 | 57 | ||
56 | configuredClient="" | 58 | configuredClient="" |
57 | 59 | ||
58 | NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name | 60 | declare -r NAME=${GISTSCRIPT:-$(basename $0)} #show hint and helper message with current script name |
59 | GITHUB_API=https://api.github.com | 61 | declare -r GITHUB_API=https://api.github.com |
60 | GIST_DOMAIN=https://gist.github.com | 62 | declare -r GIST_DOMAIN=https://gist.github.com |
61 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 63 | declare -r CONFIG=~/.config/gist.conf; mkdir -p ~/.config |
62 | per_page=100 | 64 | declare -r per_page=100 |
65 | |||
66 | declare -ar INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') | ||
67 | declare -ar VALID_CONFIGS=('user' 'token' 'folder' 'auto_sync' 'action' 'EDITOR' 'protocol' 'show_untagged') | ||
68 | declare -A CONFIG_VALUES | ||
69 | CONFIG_VALUES[auto_sync]='true|false' | ||
70 | CONFIG_VALUES[protocol]='https|ssh' | ||
71 | CONFIG_VALUES[show_untagged]='true|false' | ||
63 | 72 | ||
64 | INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') | ||
65 | VALID_CONFIGS=('user=' 'token=' 'folder=' 'auto_sync=true|false' 'action=' 'EDITOR=' 'protocol=https|ssh' 'show_untagged=true|false') | ||
66 | TAG_CHAR='-_[:alnum:]' | 73 | TAG_CHAR='-_[:alnum:]' |
67 | if [[ ! -t 0 ]]; then | 74 | if [[ ! -t 0 ]]; then |
68 | INPUT=$(cat) | 75 | INPUT=$(cat) |
@@ -73,7 +80,6 @@ fi | |||
73 | 80 | ||
74 | # Default configuration | 81 | # Default configuration |
75 | [[ ! -t 1 && -z $hint ]] && hint=false | 82 | [[ ! -t 1 && -z $hint ]] && hint=false |
76 | protocol=https | ||
77 | init= | 83 | init= |
78 | 84 | ||
79 | # Shell configuration | 85 | # Shell configuration |
@@ -148,40 +154,26 @@ _process_json() { | |||
148 | 154 | ||
149 | # Handle configuration cases | 155 | # Handle configuration cases |
150 | _configure() { | 156 | _configure() { |
151 | [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 | 157 | [[ $# == 0 ]] && ${EDITOR:-vi} $CONFIG && return 0 |
152 | |||
153 | local key=$1; local value="$2"; local keys=${VALID_CONFIGS[@]%=*} | ||
154 | 158 | ||
155 | [[ ! " ${keys} " =~ " ${key} " ]] \ | 159 | local key=$1; local value="$2" |
156 | && echo "Not a valid key for configuration, use <${keys// /|}> instead." \ | 160 | [[ ! " ${VALID_CONFIGS[@]} " =~ " ${key} " ]] \ |
161 | && echo "Not a valid key for configuration, use <${VALID_CONFIGS[@]}> instead." \ | ||
157 | && return 1 | 162 | && return 1 |
158 | 163 | ||
159 | case $key in | 164 | case $key in |
160 | user) | 165 | user) |
161 | [[ -z $value ]] && echo 'Must specify username' >&2 && return 1 ;; | 166 | while [[ ! $value =~ ^[[:alnum:]]+$ ]]; do |
167 | [[ -n $value ]] && echo "Invalid username" | ||
168 | read -r -p "Github username: " value </dev/tty | ||
169 | done ;; | ||
162 | token) | 170 | token) |
163 | [[ -n $value && ${#value} -ne 40 && ! $value =~ ^(\$|\`) ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 ;; | 171 | [[ -n $value && ${#value} -ne 40 && ! $value =~ ^(\$|\`) ]] && echo 'Invalid token format, it is not 40 chars' >&2 && return 1 ;; |
164 | auto_sync | show_untagged) | ||
165 | [[ ! $value =~ ^(true|false)$ ]] && echo "$key must be either true or false" >&2 && return 1 ;; | ||
166 | protocol) | ||
167 | [[ ! $value =~ ^(https|ssh)$ ]] && echo 'protocol must be either https or ssh' >&2 && return 1 ;; | ||
168 | action) | 172 | action) |
169 | value="'$2'" | 173 | value="'$2'" |
170 | esac | 174 | esac |
171 | 175 | ||
172 | umask 0077 && touch "$CONFIG" | 176 | echo $key="$value" >>$CONFIG |
173 | local target=$key="$value" | ||
174 | sed -i'' -e "/^$key=/ d" "$CONFIG" && [[ -n $target ]] && echo "$target" >> "$CONFIG" | ||
175 | cat "$CONFIG" | ||
176 | } | ||
177 | |||
178 | # Prompt for username | ||
179 | _ask_username() { | ||
180 | while [[ ! $user =~ ^[[:alnum:]]+$ ]]; do | ||
181 | [[ -n $user ]] && echo "Invalid username" | ||
182 | read -r -p "Github username: " user < /dev/tty | ||
183 | done | ||
184 | _configure user "$user" | ||
185 | } | 177 | } |
186 | 178 | ||
187 | # Prompt for token | 179 | # Prompt for token |
@@ -195,48 +187,61 @@ _ask_token() { | |||
195 | 187 | ||
196 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do | 188 | while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do |
197 | [[ -n $token ]] && echo "Invalid token" | 189 | [[ -n $token ]] && echo "Invalid token" |
198 | read -r -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty | 190 | read -r -p "Paste your token here (Ctrl-C to skip): " token </dev/tty |
199 | done | 191 | done |
200 | _configure token "$token" | 192 | |
193 | echo token=$token >>$CONFIG | ||
201 | } | 194 | } |
202 | 195 | ||
203 | # Check configuration is fine with user setting | 196 | # Check configuration is fine with user setting |
204 | _validate_config() { | 197 | _validate_config() { |
205 | source "$CONFIG" 2> /dev/null | ||
206 | sed -i -Ee '/^(user|token)=/!d' "$CONFIG" | ||
207 | for pair in "${VALID_CONFIGS[@]}"; do | ||
208 | key=${pair%=*}; [[ $key =~ ^(user|token)$ ]] && continue | ||
209 | value="${!key}" | ||
210 | values=${pair#*=} | ||
211 | if [[ -n $value && $value =~ ($values) ]]; then | ||
212 | echo -n $key="'$value'" | ||
213 | else | ||
214 | echo -n ${pair%|*} | ||
215 | fi | ||
216 | echo -e "${values:+\t\t# Valid: $values}" | ||
217 | done >>"$CONFIG" && source "$CONFIG" | ||
218 | |||
219 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 | 198 | [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 |
220 | if [[ -z $user ]]; then | 199 | if [[ -z $user ]]; then |
221 | echo 'Hi fellow! To access your gists, I need your Github username' | 200 | echo 'Hi fellow! To access your gists, I need your Github username' |
222 | echo "Also a personal token with scope which allows \"gist\"!" | 201 | echo "Also a personal token with scope which allows \"gist\"!" |
223 | echo | 202 | echo |
224 | _ask_username && _ask_token && init=true | 203 | _configure user && _ask_token && init=true |
225 | elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then | 204 | elif [[ -z $token && $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then |
226 | _ask_token && return 0 | 205 | echo 'To create/edit/delete a gist, a token is needed' >&2 && return 1 |
227 | echo 'To create/edit/delete a gist, a token is needed' && return 1 | 206 | _ask_token |
228 | elif [[ -z $token && $1 =~ ^(f|fetch)$ && $2 =~ ^(s|star)$ ]]; then | 207 | elif [[ -z $token && $1 =~ ^(f|fetch)$ && $2 =~ ^(s|star)$ ]]; then |
229 | _ask_token && return 0 | 208 | echo 'To get user starred gists, a token is needed' >&2 && return 1 |
230 | echo 'To get user starred gists, a token is needed' && return 1 | 209 | _ask_token |
231 | fi | 210 | fi |
211 | |||
212 | [[ -z $folder || ! -w $(dirname "$folder") ]] && folder=~/gist && mkdir -p $folder | ||
213 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX | ||
214 | } | ||
215 | |||
216 | # Apply current configuration into config file | ||
217 | _reformat_config() { | ||
218 | local line_token=$(sed -ne '/^token=/{p;q}' $CONFIG); line_token=${line_token:-token=} | ||
219 | for key in ${VALID_CONFIGS[@]}; do | ||
220 | [[ $key == token ]] && echo $line_token && continue # Because format may like this: token=$(COMMAND-TO-GET-TOKEN), just leave it untouched | ||
221 | local value="${!key}"; local valid_values=${CONFIG_VALUES[$key]} | ||
222 | if [[ -n "$value" ]] && [[ -z $valid_values || "$value" =~ ^($valid_values)$ ]]; then | ||
223 | echo -n $key="'$value'" | ||
224 | declare -g $key=$value # apply current value | ||
225 | else | ||
226 | echo -n $key= | ||
227 | declare -g $key=${valid_values%%|*} # apply the first valid value | ||
228 | fi | ||
229 | echo -e "${valid_values:+\t\t# Valid: $valid_values}" | ||
230 | done >"$CONFIG" | ||
232 | } | 231 | } |
233 | 232 | ||
234 | # Load configuration | 233 | # Load configuration |
235 | _apply_config() { | 234 | _apply_config() { |
235 | unset ${VALID_CONFIGS[@]} | ||
236 | if [[ -e $CONFIG ]]; then | ||
237 | source "$CONFIG" 2>/dev/null | ||
238 | else | ||
239 | umask 0077 && touch "$CONFIG" | ||
240 | fi | ||
241 | |||
236 | _validate_config "$@" || return 1 | 242 | _validate_config "$@" || return 1 |
243 | _reformat_config | ||
237 | 244 | ||
238 | [[ -z $folder || ! -w $(dirname "$folder") ]] && folder=~/gist; mkdir -p $folder | ||
239 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX | ||
240 | getConfiguredClient | 245 | getConfiguredClient |
241 | } | 246 | } |
242 | 247 | ||
@@ -887,7 +892,7 @@ case "$1" in | |||
887 | _clean_repos ;; | 892 | _clean_repos ;; |
888 | config | c) | 893 | config | c) |
889 | shift | 894 | shift |
890 | _configure "$@" && { _apply_config config && _check_protocol &>/dev/null; } ;; | 895 | _configure "$@" && { _apply_config && (_check_protocol &>/dev/null &); } ;; |
891 | user | u) | 896 | user | u) |
892 | shift | 897 | shift |
893 | _query_user "$@" ;; | 898 | _query_user "$@" ;; |