-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentrypoint.sh
executable file
·194 lines (160 loc) · 6.09 KB
/
entrypoint.sh
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
set -e
#/*
# * This file is part of TangoMan Entrypoint package.
# *
# * Copyright (c) 2025 "Matthias Morin" <mat@tangoman.io>
# *
# * This source file is subject to the MIT license that is bundled
# * with this source code in the file LICENSE.
# */
#/**
# * TangoMan CI Manager
# *
# * Run Continuous Integration
# *
# * @license MIT
# * @author "Matthias Morin" <mat@tangoman.io>
# * @version 2.4.1-light
# * @link https://github.com/TangoMan75/entrypoint
# */
## Install git hooks
hooks() {
echo_info 'rm -fr .git/hooks\n'
rm -fr .git/hooks
echo_info 'cp -r .githooks .git/hooks\n'
cp -r .githooks .git/hooks
echo_info 'chmod +x .git/hooks/*\n'
chmod +x .git/hooks/*
}
## Sniff errors with linter
lint() {
if [ ! -x "$(command -v shellcheck)" ]; then
echo_danger "error: \"$(basename "${0}")\" requires shellcheck, try: 'sudo apt-get install -y shellcheck'\n"
return 1
fi
find . -maxdepth 3 -type f -name '*.sh' | sort -t '\0' -n | while read -r FILE
do
echo_info "shellcheck \"${FILE}\"\n"
shellcheck "${FILE}"
done
}
## Initialise git submodules
submodules() {
if [ -z "$1" ]; then
set -- "$(dirname "$(realpath "$0")")"
else
set -- "$(dirname "$(realpath "$1")")"
fi
if [ ! -d "$1" ]; then
echo_danger "error: \"$1\" is not a valid directory.\n"
return 1
fi
if [ ! -f "$1/.gitmodules" ]; then
echo_danger 'error: ".gitmodules" not found'
return 1
fi
if [ ! -x "$(command -v git)" ] || [ ! -d "$1/.git" ]; then
if [ ! -x "$(command -v wget)" ]; then
echo_danger "error: \"$(basename "${0}")\" requires wget, try: 'sudo apt-get install -y wget'\n"
return 1
fi
if [ ! -x "$(command -v unzip)" ]; then
echo_danger "error: \"$(basename "${0}")\" requires unzip, try: 'sudo apt-get install -y unzip'\n"
return 1
fi
# parse .gitmodules
grep -Eo 'https://.+' "$1/.gitmodules" | while read -r url; do
server=$(echo "${url}" | sed -E 's/https:\/\///' | sed -E 's/\.git$//' | tr ':' '/' | cut -d/ -f1)
# this script handles github repositories only
if [ "${server}" != 'github.com' ]; then
continue
fi
repository=$(echo "${url}" | sed -E 's/https:\/\///' | sed -E 's/\.git$//' | tr ':' '/' | cut -d/ -f3)
# do not update existing submodule
if [ -n "$(ls -A "${repository}" 2>/dev/null)" ]; then
continue
fi
echo_info "wget -q \"${url}/archive/refs/heads/master.zip\"\n"
wget -q "${url}/archive/refs/heads/master.zip"
echo_info "unzip -qq \"$1/master.zip\"\n"
unzip -qq "$1/master.zip"
echo_info "rm \"$1/master.zip\"\n"
rm "$1/master.zip"
echo_info "mv \"$1/${repository}-\"* \"$1/${repository}\"\n"
mv "$1/${repository}-"* "$1/${repository}"
done
return 0
fi
echo_info 'git submodule update --init --recursive\n'
git submodule update --init --recursive
}
#--------------------------------------------------
# copy/paste here TangoMan helper functions
# (like a nice set of semantic colors)
#--------------------------------------------------
# version v7.0.0-c
echo_info() { printf '%b%b\033[0m' '\033[95m' "${*}"; }
echo_error() { printf '%berror:\t%b\033[0m' '\033[31m' "${*}"; }
#--------------------------------------------------
# You do not need to worry about anything that's
# placed after this line. ;-)
#--------------------------------------------------
## Print this help (default)
help() {
printf '\033[0m\n%b%64s\033[0m\n%b %-63s\033[0m\n%b%64s\033[0m\n\n' '\033[1;104;97m' '' '\033[1;104;97m' 'TangoMan CI Manager' '\033[1;104;97m' '';
printf '\033[33mInfos:\n'
printf '\033[32m author \033[97m"Matthias Morin" <mat@tangoman.io> \n'
printf '\033[32m license \033[97mMIT \n'
printf '\033[32m version \033[97m2.4.1-light \n'
printf '\033[32m link \033[97mhttps://github.com/TangoMan75/shoe \n\n'
printf '\033[33mDescription:\n'
printf '\033[97m Run Continuous Integration \n\n'
printf '\033[33mUsage:\n'
printf '\033[95m sh entrypoint.sh [\033[32mcommand\033[95m] \n\n'
printf '\033[33mCommands:\n'
printf '\033[32m hooks \033[97mInstall git hooks\n'
printf '\033[32m lint \033[97mSniff errors with linter\n'
printf '\033[32m submodules \033[97mSniff errors with linter\n'
printf '\033[32m help \033[97mPrint this help (default)\n'
}
#--------------------------------------------------
_get_functions() {
# this regular expression matches functions with either bash or sh syntax
awk '/^(function )? *[a-zA-Z0-9_]+ *\(\) *\{/ {
sub("^function ",""); gsub("[ ()]",""); # remove leading "function ", round brackets and extra spaces
FUNCTION = substr($0, 1, index($0, "{")); # truncate string after opening curly brace
sub("{$", "", FUNCTION); # remove trailing curly brace
if (substr(PREV, 1, 3) == "## " && substr($0, 1, 1) != "_") print FUNCTION
} { PREV = $0 }' "$0"
}
#--------------------------------------------------
_main() {
if [ $# -lt 1 ]; then
help
exit 0
fi
_execute=''
for _argument in "$@"; do
_is_valid=false
for _function in $(_get_functions); do
# get shorthand character
_shorthand="$(echo "${_function}" | awk '{$0=substr($0, 1, 1); print}')"
if [ "${_argument}" = "${_function}" ] || [ "${_argument}" = "${_shorthand}" ]; then
# append argument to the execute stack
_execute="${_execute} ${_function}"
_is_valid=true
break
fi
done
if [ "${_is_valid}" = false ]; then
printf '\033[1;31merror:\t\033[0;91m"%s" is not a valid command\033[0m\n' "${_argument}"
help
exit 1
fi
done
for _function in ${_execute}; do
eval "${_function}"
done
}
_main "$@"