-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpk
More file actions
executable file
·172 lines (153 loc) · 4.63 KB
/
dpk
File metadata and controls
executable file
·172 lines (153 loc) · 4.63 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
# DISPAK
#
# Simple tool to manage code and servers/services easily.
#
# Source code and documentation: https://github.com/Amaury/Dispak
#
# © 2017, Amaury Bouchard <[email protected]>
#
# Licensed under the terms of the MIT license (see LICENSE file).
# ########## GLOBAL VARIABLES (readable by rules' code) ##########
# Path to the Dispak installation root directory.
# @type string
DPK_ROOT="$(cd "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)"
# List of options given on the command-line.
declare -A DPK_OPT
# Name of the asked command to execute
COMMAND=$1
# Path to the root of the current git repository.
GIT_REPO_PATH="$(git rev-parse --git-dir 2>/dev/null)"
if [ "$GIT_REPO_PATH" != "" ]; then
GIT_REPO_PATH="$(readlink -f "$(dirname "$GIT_REPO_PATH")")"
fi
# ########## INTERNAL VARIABLES ##########
# Name of the Git's main branch. Defaults to 'main', but more and more repositories are switching to 'main'.
CONF_GIT_MAIN="main"
# List of servers' platform types. Declared here to be fillable in the configuration file. Used by the check_platform() function.
# @type associative array
declare -A CONF_PLATFORMS
# List of rules, indexed by section.
# @type associative array
declare -A _DPK_RULES
# List of mandatory command-line parameters, indexed by rule.
# @type associative array
declare -A _DPK_RULES_MANDATORY_PARAMS
# List of optional command-line parameters, indexed by rule.
# @type associative array
declare -A _DPK_RULES_OPTIONAL_PARAMS
# Tell if the program has changed its current working directory.
# @type bool
_NEED_POPD=0
# ########## INCLUDE SOURCE FILES ##########
# functions files
. "$DPK_ROOT/lib/utils.sh"
. "$DPK_ROOT/lib/rules.sh"
. "$DPK_ROOT/lib/checks.sh"
# (rules can't be imported from a function, because defined arrays will be not defined)
# import rules from Dispak distribution
for FILE in "$DPK_ROOT"/rules/*.sh ; do
RULE_NAME=""
RULE_SECTION=""
RULE_MANDATORY_PARAMS=""
RULE_OPTIONAL_PARAMS=""
. "$FILE"
if [ "$RULE_NAME" != "" ] && [ "$RULE_NAME" != "help" ]; then
_dpk_rule_add $RULE_NAME "$RULE_SECTION"
_dpk_rule_mandatory_params $RULE_NAME $RULE_MANDATORY_PARAMS
_dpk_rule_optional_params $RULE_NAME $RULE_OPTIONAL_PARAMS
fi
done
# import rules from repository source tree
if [ -d "$GIT_REPO_PATH/etc/dispak-rules" ]; then
for FILE in "$GIT_REPO_PATH"/etc/dispak-rules/*.sh ; do
RULE_NAME=""
RULE_SECTION=""
RULE_MANDATORY_PARAMS=""
RULE_OPTIONAL_PARAMS=""
. "$FILE"
if [ "$RULE_NAME" != "" ] && [ "$RULE_NAME" != "help" ]; then
_dpk_rule_add $RULE_NAME "$RULE_SECTION"
_dpk_rule_mandatory_params $RULE_NAME $RULE_MANDATORY_PARAMS
_dpk_rule_optional_params $RULE_NAME $RULE_OPTIONAL_PARAMS
fi
done
fi
# ########## READ CONFIGURATIONI FILE ##########
if [ "$GIT_REPO_PATH" != "" ]; then
if [ -f "$GIT_REPO_PATH/etc/dispak.conf" ]; then
. "$(eval realpath "$GIT_REPO_PATH/etc/dispak.conf")"
fi
fi
# ########## OPTIONS AND CONFIGURATION ##########
# no option given: show help and quit
if [ "$COMMAND" = "" ] || [ "$COMMAND" = "h" ] || [ "$COMMAND" = "help" ]; then
rule_exec_help $2
exit 0
fi
# fetch command-line options
shift
while getopts "h-:" OPTCHAR; do
case "$OPTCHAR" in
"-")
KEY=$(echo $OPTARG | cut -d"=" -f 1)
if [[ "${OPTARG}" == *"="* ]]; then
VALUE="${OPTARG#*=}"
else
VALUE=""
fi
DPK_OPT[$KEY]="$VALUE"
;;
"h")
rule_exec_help
exit 1
;;
*)
abort "$(ansi red)Bad command line option '-$OPTCHAR'.$(ansi reset)"
;;
esac
done
# check if the asked command is known
CMD_FOUND=0
for _SECTION in "${!_DPK_RULES[@]}"; do
for _RULE in ${_DPK_RULES["$_SECTION"]}; do
if [ "$_RULE" = "$COMMAND" ]; then
CMD_FOUND=1
break
fi
done
done
if [ $CMD_FOUND -eq 0 ]; then
abort "$(ansi red)Unknown command $(ansi reset)'$COMMAND'$(ansi red).$(ansi reset)"
fi
# check if all mandatory options are given
for _OPT in ${_DPK_RULES_MANDATORY_PARAMS[$COMMAND]}; do
if [ "${DPK_OPT[$_OPT]}" = "" ]; then
abort "$(ansi red)Mising parameter $(ansi reset)--$_OPT$(ansi red).$(ansi reset)"
fi
done
# check if extraneous parameters are given
for _OPT in "${!DPK_OPT[@]}"; do
if [ "$_OPT" = "platform" ]; then
continue
fi
FOUND=0
for _KEY in ${_DPK_RULES_MANDATORY_PARAMS[$COMMAND]} ${_DPK_RULES_OPTIONAL_PARAMS[$COMMAND]}; do
if [ "$_KEY" = "$_OPT" ]; then
FOUND=1
break;
fi
done
if [ $FOUND -eq 0 ]; then
abort "$(ansi red)Extraneous parameter $(ansi reset)--$_OPT$(ansi red).$(ansi reset)"
fi
done
# ########## EXECUTION ##########
# moving to root path
if [ "$GIT_REPO_PATH" != "" ]; then
_NEED_POPD=1
pushd "$GIT_REPO_PATH" > /dev/null
fi
EXEC_FUNCTION="rule_exec_${COMMAND}"
$EXEC_FUNCTION
success