-
Notifications
You must be signed in to change notification settings - Fork 0
/
.manage_add
93 lines (87 loc) · 2.43 KB
/
.manage_add
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
89
90
91
92
#!/usr/bin/env bash
function usage() {
echo "Usage: $SCRIPT add [-h|--help] [-a|--add | -s|--set | -r|--remove] {configs...}"
# if $1 is non empty, print long usage
if [ -z "$1" ]; then return; fi
echo "Add configs to the current host. At least one should be provided."
echo ""
echo "Options:"
echo " -a, --add Add configs to the current set (default behavior)"
echo " -s, --set Set configs as the new current set, instead of"
echo " adding them to the current set"
echo " -r, --remove Remove configs of the current set, instead of"
echo " adding them to the current set"
echo " -Q, --super-quiet Suppress almost all output"
echo " -q, --quiet Suppress most output"
echo " -v, --verbose Enable verbose output"
echo " -h, --help Display this message"
echo ""
echo "Note: Removing a config won't uninstall it. This is not currently"
echo " supported by the script."
}
function run() {
# parse arguments
local action="a"
local verbose=""
local -a configs
while [ -n "$1" ]; do
case "$1" in
-h | --help)
usage long
return;;
-a | --add)
action="a"
shift;;
-s | --set)
action="s"
shift;;
-r | --remove)
action="r"
shift;;
-q | --quiet | -Q | --super-quiet | -v | --verbose)
verbose="$1"
shift;;
--* )
echo "$SCRIPT: invalid option: ${1:2}" >&2
usage
return 1;;
-* )
echo "$SCRIPT: invalid option: ${1:1}" >&2
usage
return 1;;
* )
configs+=("$1")
shift;;
esac
done
# ensure configs are given
if [[ ${#configs[@]} == 0 ]]; then
usage long
return 1
fi
# update current host
local -a to_load
case "$action" in
s|a)
if [[ "$action" == "s" ]]; then clear_current_host; fi
for config in "${configs[@]}"; do
if config_exists "$config"; then
add_current_host "$config"
to_load+=("$config")
else
echo "[${_ERR_RED}!${_ERR_RST}] Unknown config '$config'" >&2
return 1
fi
done;;
r)
for config in "${configs[@]}"; do
remove_current_host "$config"
done;;
esac
# save new current host
save_current_host
# load new configs
if ! install_all_configs to_load "$verbose"; then
return 1
fi
}