-
Notifications
You must be signed in to change notification settings - Fork 2
/
branchout-configuration
executable file
·88 lines (76 loc) · 2.89 KB
/
branchout-configuration
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
83
84
85
86
87
88
#!/bin/bash
test -n "${DEBUG}" && set -x
set -e
function fail() {
test -n "${1}" && echo "${1}" && echo
exit 2
}
function getConfigValue() {
test -z "${BRANCHOUT_STATE}" && fail "BRANCHOUT_STATE is not defined"
if test -f "${BRANCHOUT_STATE}/branchoutrc"; then
grep "^BRANCHOUT_CONFIG_${1}=" "${BRANCHOUT_STATE}/branchoutrc" | sed -e 's,^.*="\(.*\)"$,\1,'
fi
}
function setConfigValue() {
test -z "${BRANCHOUT_STATE}" && fail "BRANCHOUT_STATE is not defined"
test -z "${2}" && usage "You must supply a value to set"
test -f "${BRANCHOUT_STATE}/branchoutrc" && grep -v "^BRANCHOUT_CONFIG_${1}=" "${BRANCHOUT_STATE}/branchoutrc" > "${BRANCHOUT_STATE}/branchoutrc.tmp" && true
echo "BRANCHOUT_CONFIG_${1}=\"${2}\"" >> "${BRANCHOUT_STATE}/branchoutrc.tmp"
sort "${BRANCHOUT_STATE}/branchoutrc.tmp" > "${BRANCHOUT_STATE}/branchoutrc"
rm "${BRANCHOUT_STATE}/branchoutrc.tmp"
}
function getValue() {
test -z "${BRANCHOUT_STATE}" && fail "BRANCHOUT_STATE is not defined"
grep "^BRANCHOUT_${1}=" "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}" | sed -e 's,^.*="\(.*\)"$,\1,'
}
function setValue() {
test -z "${PROJECTION_DIRECTORY}" && fail "PROJECTION_DIRECTORY is not defined"
grep -v "^BRANCHOUT_${1}=" "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}" > "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}".tmp && true
echo "BRANCHOUT_${1}=\"${2}\"" >> "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}.tmp"
sort "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}.tmp" > "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}"
rm "${PROJECTION_DIRECTORY}/${BRANCHOUT_FILE}.tmp"
}
function readValue() {
if test -z "${3}"; then
printf "Please provide %s: " "${1}"
else
printf "Please provide %s [%s]: " "${1}" "${3}"
fi
# if the user does not enter a value sure to add a newline before the error message if there is no default
read -r VALUE || true
echo
test -z "${VALUE}" && VALUE="${3}"
test -z "${VALUE}" && fail "Error: You must supply a value for $1"
export "${2}=${VALUE}"
}
function readSecret() {
printf "Please provide %s: " "${1}"
read -s -r VALUE
echo
test -z "${VALUE}" && fail "Error: You must supply a value for $1"
export "${2}=${VALUE}"
}
function ensureValue() {
valueName="BRANCHOUT_${2}"
if test -z "${!valueName}"; then
readValue "${1}" "${valueName}" "${3}"
setValue "${2}" "${!valueName}"
fi
}
function ensureConfigValue() {
configName="BRANCHOUT_CONFIG_${2}"
if test -z "${!configName}"; then
readValue "${1}" "${configName}" "${3}"
setConfigValue "${2}" "${!configName}"
fi
}
export BO_THEME_RESET="\\033[0m"
export BO_THEME_OK="\\033[0m"
export BO_THEME_NOTICE="\\033[35m"
export BO_THEME_ERROR="\\033[31m"
export BO_THEME_WARNING="\\033[38;5;202m"
export BO_THEME_HIGHLIGHT="\\033[38;5;11m"
BRANCHOUT_PROJECTS_DIRECTORY=projects
# shellcheck source=examples/branchoutrc
test -f ~/.config/branchoutrc && source ~/.config/branchoutrc
export BRANCHOUT_PROJECTS_DIRECTORY