-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-config
More file actions
executable file
·82 lines (68 loc) · 2.22 KB
/
get-config
File metadata and controls
executable file
·82 lines (68 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
# Gets a script config or lists all configs
# Environment setup
# -----------------------------------------------------------------------------
set -o pipefail # set -o errexit hides errors, don't use it
[[ ${DEBUG-} ]] && set -o xtrace
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)"
[[ ":${PATH}:" != *:${SCRIPT_DIR}:* ]] && export PATH="${SCRIPT_DIR}:${PATH}"
source "${SCRIPT_DIR}/bash_modules/terminal.sh"
source "${SCRIPT_DIR}/bash_modules/config.sh"
source "${SCRIPT_DIR}/bash_modules/utils.sh"
[[ -z ${BASH_MODULES_DIR-} ]] && echo "ERROR: terminal.sh module missing" && exit 1
function print_usage() {
cat <<EOF
Usage: $(basename "$0") [key]
Lists all script configs or returns a specific key/value pair.
Dependencies:
rg RipGrep recursively searches directories for a regex pattern
Optional arguments:
key The specific config key to retrieve
-h, --help Show this help message and exit
EOF
}
if [[ $# -gt 1 || "${1}" == "-h" || "${1}" == "--help" ]]; then
print_usage
exit 1
fi
# Dependency Checks
# -----------------------------------------------------------------------------
dependencies=(rg)
for cmd in "${dependencies[@]}"; do
if ! command -v "${cmd}" >/dev/null; then
log_error "ERROR: Missing dependency - '${cmd}'"
exit 1
fi
done
# Main Logic
# -----------------------------------------------------------------------------
log_title "Get Script Config"
log_message "Source: $(config_get_path)"
# If no argument provided, list all configs
if [[ -z "${1}" ]]; then
log_heading "Default Configuration"
log_message "Source: $(config_get_defaults_path)"
config_list_defaults
log_newline
log_heading "User Configuration"
log_message "Source: $(config_get_path)"
config_list
exit 0
fi
# Validate that key is not empty or whitespace-only
if [[ -z "${1// /}" ]]; then
log_error "ERROR: Key argument cannot be empty or whitespace-only"
print_usage
exit 1
fi
# Get the value for the specified key
declare key="${1}"
declare value
value="$(config_get "${key}")"
if [[ -n "${value}" ]]; then
log_newline
printf '%s="%s"\n' "$(to_upper "${key}")" "${value}"
exit 0
fi
log_error "Key '$(to_upper "${key}")' not found in config"
exit 1