From b108934e8ee4875fc697712eb30d21ed4301f92b Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 30 Jun 2020 14:02:17 +0800 Subject: Reformat config file after source it So user can read valid keys at once --- gist | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/gist b/gist index b27e4e8..9de9e3f 100755 --- a/gist +++ b/gist @@ -70,6 +70,8 @@ else export mark=[^s] # By defaut, only process user's gists, not starred gist fi +VALID_CONFIGS=('user=' 'token=' 'folder=' 'auto_sync=true|false' 'action=' 'EDITOR=' 'protocol=https|ssh' 'show_untagged=true|false') + # Default configuration [[ ! -t 1 && -z $hint ]] && hint=false protocol=https @@ -149,11 +151,10 @@ _process_json() { _configure() { [[ $# == 0 ]] && (${EDITOR:-vi} "$CONFIG") && return 0 - local valid_keys='user|token|folder|auto_sync|EDITOR|action|protocol|show_untagged' - local key=$1; local value="$2" + local key=$1; local value="$2"; local keys=${VALID_CONFIGS[@]%=*} - [[ ! $key =~ ^($valid_keys)$ ]] \ - && echo "Not a valid key for configuration, use <$valid_keys> instead." \ + [[ ! " ${keys} " =~ " ${key} " ]] \ + && echo "Not a valid key for configuration, use <${keys// /|}> instead." \ && return 1 case $key in @@ -202,8 +203,20 @@ _ask_token() { # Check configuration is fine with user setting _validate_config() { - # shellcheck source=/dev/null source "$CONFIG" 2> /dev/null + sed -i -Ee '/^(user|token)=/!d' "$CONFIG" + for pair in "${VALID_CONFIGS[@]}"; do + key=${pair%=*}; [[ $key =~ ^(user|token)$ ]] && continue + value="${!key}" + values=${pair#*=} + if [[ -n $value && $value =~ ($values) ]]; then + echo -n $key="'$value'" + else + echo -n ${pair%|*} + fi + echo -e "${values:+\t\t# Valid: $values}" + done >>"$CONFIG" && source "$CONFIG" + [[ $1 =~ ^(c|config|h|help|u|user|update|version) ]] && return 0 if [[ -z $user ]]; then echo 'Hi fellow! To access your gists, I need your Github username' -- cgit v1.2.3-70-g09d2