-
Notifications
You must be signed in to change notification settings - Fork 5
/
mocli.sh
executable file
·340 lines (301 loc) · 9.18 KB
/
mocli.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env bash
CWD=$(cd "$(dirname "${BASH_SOURCE[0]}")/" && pwd)
cd ${CWD}
ENVS=('local' 'dev' 'qa' 'prod')
ENV="qa"
declare -A CMDS_DESC
CMDS_DESC=(
[developer]="Setup your developer environment"
[watch]="Starts a livereload server and watches all assets"
[test]="Runns unit tests"
[coverage]="Runns unit tests and generates code coverage reports"
[release]="Create release versions using git-flow. Usage: mocli release [start|finish] <version>"
[release-p]="Create release version and increases patch number"
[hotfix]="Create a qa release from current branch and uploads it to bitbucket"
[docs]="Create a documentation for the components"
[pack]="Zips the dist/<vendor> directory. Usage: mocli pack [--production] [--vendor=firefox] [--env=qa]"
[sonar-scan]="Running SonarQube Scanner from the Docker image. Usage: sonar-scan [<token>]"
[help]="Show this message"
)
function printHelp() {
echo " ______________________________________________ "
echo "| .__ .__ | "
echo "| _____ ____ ____ | | |__| | "
echo "| / \ / _ \_/ ___\| | | | | "
echo "| | Y Y \| |_) ) \___| |_| | | "
echo "| |__|_| /|____/ \_____>____/__| | "
echo "| \/ MetaDefender Cloud CLI | "
echo "| | "
echo "|______________________________________________| "
echo " "
echo "I am your development wizard (0_0)"
echo "Your commands are:"
echo ""
line=" "
for i in "${!CMDS_DESC[@]}"; do
printf "%s %s %s \n" "$i" "${line:${#i}}" "${CMDS_DESC[$i]}"
done
}
function gen_cmp() {
for i in "${!CMDS_DESC[@]}"; do
CMDS+=($i)
done
mkdir -p ~/.zsh/completion && touch ~/.zsh/completion/_mocli
echo -e "#compdef mocli.sh
_mocli(){
local line
_arguments -C \
\"-h[Show help information]\" \
\"--h[Show help information]\" \
\"1: :("${CMDS[@]}")\" \
\"*::arg:->args\"
}
_mocli" >~/.zsh/completion/_mocli
}
###
# Usage: in_array $NEEDLE "${HEYSTACK[@]}"
# Returns: 0 if NEEDLE is found in HEYSTACK
###
function in_array() {
local needle=$1
shift
local heystack=("$@")
printf '%s\n' ${heystack[@]} | grep -qP "^${needle}$"
echo $?
}
function read_env() {
echo "Create deploy for qa | prod | dev [qa]?"
read ENV
if [ -z "${ENV}" ]; then
ENV=qa
fi
if [ "${ENV}" != "qa" ] && [ "${ENV}" != "prod" ] && [ "${ENV}" != "dev" ]; then
echo "Invalid option. Opperation canceled"
exit 1
fi
}
function copy_secrets() {
echo "-> copy secrets"
aws s3 cp s3://mcl-artifacts-frontend-${ENV}/mcl-browser-extension/secrets.json ./secrets.json >/dev/null 2>&1
if [[ "$?" != "0" ]]; then
echo -e "{\n\t\"googleAnalyticsId\": \"\"\n}" >./secrets.json
echo -e "\nFailed to copy secrets from s3. Please update ./secrets.json manually.\n"
fi
}
function copy_envs() {
echo "-> copy environment specific files"
ENVS=(local dev qa prod)
if [[ "${1}" != "" ]]; then
ENVS=(${1})
fi
for env in ${ENVS[@]}; do
echo s3://mcl-artifacts-frontend-${ENV}/mcl-browser-extension/app/config/${env}.json
aws s3 cp s3://mcl-artifacts-frontend-${ENV}/mcl-browser-extension/app/config/${env}.json ./config/ >/dev/null 2>&1
done
}
function sonar_scan() {
SONARQUBE_URL=https://sonar.opswat.com
SONARQUBE_CACHE_DIR=${CWD}/.sonar/cache
if [[ "${1}" != "" ]]; then
SONARQUBE_TOKEN=${1}
else
echo "Please pass 'SONARQUBE_TOKEN' as an argument and run the scanner again"
exit 1
fi
docker run \
--rm \
-e SONAR_HOST_URL="${SONARQUBE_URL}" \
-e SONAR_LOGIN="${SONARQUBE_TOKEN}" \
-v ${SONARQUBE_CACHE_DIR}:/opt/sonar-scanner/.sonar/cache \
-v "${CWD}:/usr/src" \
sonarsource/sonar-scanner-cli
}
function pack_extension() {
webextension-toolbox build -s ./src --config ./webextension-toolbox-config.js --no-manifest-validation ${VENDOR}
}
if [[ $# == 0 ]]; then
printHelp
exit 0
fi
while [[ $# -gt 0 ]]; do
case "${1}" in
developer)
# copy the google analytics ID. Create this file manually if you don't have access
copy_secrets
copy_envs
echo "-> install webpack-cli"
npm list -g --depth=0 webpack-cli >/dev/null 2>&1 || npm i -g webpack-cli >/dev/null
if [[ $(npm list -g -s | grep -c webextension-toolbox) -eq 0 ]]; then
echo "Installing webextension-toolbox globally"
npm install -g @webextension-toolbox/webextension-toolbox
fi
echo "-> install packages"
rm -rf ./node_modules/
npm ci --legacy-peer-deps
echo "-> copy git-flow hooks"
cp git_hooks/* .git/hooks/
gen_cmp
echo -e "-> Done\n"
exit 0
;;
copy-secrets)
copy_secrets
exit 0
;;
copy-envs)
copy_envs ${2}
exit 0
;;
watch)
export ENVIRONMENT=${2:-${ENV}}
npm run start
exit 0
;;
build)
echo "Removing dist/ folder"
rm -rf dist
echo "Building Chrome Extensions for ${ENVIRONMENT}"
export ENVIRONMENT=${2:-${ENV}}
npm run build
exit 0
;;
test)
npm run test
exit 0
;;
coverage)
npm run test:coverage
exit 0
;;
docs)
if [[ $(npm list -g -s | grep -c jsdoc) -eq 0 ]]; then
echo "Installing jsdoc globally"
npm install -g jsdoc
fi
echo "Generating documentation to docs/"
jsdoc -c jsdoc.conf.json
exit 0
;;
release)
if [[ $# -lt 2 ]]; then
echo "Invalid number of arguments"
echo "Usage: mocli release <start|stop|publish> [version]"
exit 1
fi
C_VERSION=$(cat package.json | jq .version | sed 's/"//g')
VERSION=$(echo ${C_VERSION} | cut -d'.' -f2)
if [[ ${2} == "start" ]]; then
VERSION=$((VERSION + 1))
fi
VERSION=$(echo ${C_VERSION} | sed -r "s/.[0-9]+.[0-9]+/.${VERSION}.0/")
if [[ $# = 3 ]]; then
VERSION=${3}
fi
case "${2}" in
start)
git flow release start ${VERSION}
git flow release publish ${VERSION}
;;
finish)
git flow release finish -Fp ${VERSION}
git checkout customer
;;
publish)
read_env
export ENVIRONMENT=${2:-${ENV}}
VENDOR=${3:-'chrome'}
pack_extension
;;
esac
exit 0
;;
release-p)
if [[ $# -lt 2 ]]; then
echo "Invalid number of arguments"
echo "Usage: mocli patch <start|stop|publish> [version]"
exit 1
fi
C_VERSION=$(cat package.json | jq .version | sed 's/"//g')
VERSION=$(echo ${C_VERSION} | cut -d'.' -f3)
if [[ ${2} == "start" ]]; then
VERSION=$((VERSION + 1))
fi
VERSION=$(echo ${C_VERSION} | sed -r "s/.[0-9]+$/.${VERSION}/")
if [[ $# = 3 ]]; then
VERSION=${3}
fi
case "${2}" in
start)
git flow release start ${VERSION}
git flow release publish ${VERSION}
;;
finish)
git flow release finish -Fp ${VERSION}
git checkout customer
;;
publish)
read_env
export ENVIRONMENT=${2:-${ENV}}
VENDOR=${3:-'chrome'}
pack_extension
;;
esac
;;
hotfix)
if [[ $# -lt 2 ]]; then
echo "Invalid number of arguments"
echo "Usage: mocli hotfix <start|stop|publish> [version]"
fi
C_VERSION=$(cat package.json | jq .version | sed 's/"//g')
VERSION=$(echo ${C_VERSION} | cut -d'.' -f3)
if [[ ${2} == "start" ]]; then
VERSION=$((VERSION + 1))
fi
VERSION=$(echo ${C_VERSION} | sed -r "s/.[0-9]+$/.${VERSION}/")
if [[ $# = 3 ]]; then
VERSION=${3}
fi
case "${2}" in
start)
git flow hotfix start ${VERSION}
git flow hotfix publish ${VERSION}
;;
finish)
git flow hotfix finish -Fp ${VERSION}
git checkout customer
;;
publish)
read_env
export ENVIRONMENT=${2:-${ENV}}
VENDOR=${3:-'chrome'}
pack_extension
;;
esac
exit 0
;;
pack)
read_env
export ENVIRONMENT=${2:-${ENV}}
VENDOR=${3:-'chrome'}
pack_extension
exit 0
;;
gen-cmp)
gen_cmp
;;
sonar-scan)
sonar_scan ${2}
exit $?
;;
help)
printHelp
exit 0
;;
*)
echo "Unknown parameter: ${1}"
printHelp
exit 1
;;
esac
shift
done