-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutility.sh
executable file
·536 lines (459 loc) · 13 KB
/
utility.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/env bash
# Find the latest version of this application: https://github.com/firepress-org/bash-script-template
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
#
set -o errexit # Exit on most errors (see the manual)
set -o errtrace # Make sure any error trap is inherited
set -o pipefail # Use last non-zero exit code in a pipeline
#set -o xtrace # Trace the execution of the script (debug)
#
#set -o nounset # Disallow expansion of unset variables
# --- Find bad variables by using `./utility.sh test two three`, else disable it
# --- or remove $1, $2, $3 var defintions in @main
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# Git
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
function hash {
git rev-parse --short HEAD
}
function outmaster {
git checkout master
}
function outedge {
git checkout edge
}
function stat {
git status
}
function diff {
check && echo
git diff
}
function wip_bisect {
echo "todo"
}
function ci-status {
hub ci-status -v $(git rev-parse HEAD)
}
function push {
# commit all
App_is_input2_empty
git status && \
git add -A && \
git commit -m "${input_2}" && \
clear
git push
}
function log {
git --no-pager log --decorate=short --pretty=oneline -n25
}
function sq {
# squash
App_is_input2_empty
App_is_input3_empty
backwards_steps="${input_2}"
git_message="${input_3}"
usage="sq 3 'Add fct xyz'"
# think rebase_master_from_edge
if [[ $(git status | grep -c "nothing to commit") == "1" ]]; then
echo "good, nothing to commit" | 2>/dev/null
git reset --hard HEAD~"${backwards_steps}" && \
git merge --squash HEAD@{1} && \
git push origin HEAD --force && \
git status && \
git add -A && \
git commit -m "${git_message} / squashed" && \
git push;
else
my_message="You must push your commit(s) before doing a rebase." App_Pink
fi
}
function rbmaster {
# think rebase_master_from_edge
if [[ $(git status | grep -c "nothing to commit") == "1" ]]; then
echo "good, nothing to commit" | 2>/dev/null
git checkout master
git pull origin master
git rebase edge
git push
hash_master_is=$(git rev-parse --short HEAD)
#
hash_edge_is=$(git rev-parse --short HEAD)
my_message="Diligence: ${hash_master_is} | ${hash_master_is} (master vs edge should be the same)" App_Blue
else
my_message="You must push your commit(s) before doing a rebase." App_Pink
fi
}
function rbedge {
# think rebase_edge_from_master
if [[ $(git status | grep -c "nothing to commit") == "1" ]]; then
echo "good, nothing to commit" | 2>/dev/null
git checkout edge
git pull origin edge
git rebase master
git push
hash_edge_is=$(git rev-parse --short HEAD)
#
git checkout master
hash_master_is=$(git rev-parse --short HEAD)
git checkout edge
my_message="Diligence: ${hash_master_is} | ${hash_master_is} (master vs edge should be the same)" App_Blue
else
my_message="You must push your commit(s) before doing a rebase." App_Pink
fi
}
function pushcl {
# push changelog
# Use case: we just updated the CAHNGELOG.md file
# Next, we want to:
#commit change on changelog.md
#tag the commit
#release on github
#rbedge
App_is_input2_empty
currentBranch=$(git rev-parse --abbrev-ref HEAD)
if [[ "${currentBranch}" == "master" ]]; then
tag_version="${input_2}"
version
release
rbedge
else
my_message="You must be a master branch." App_Pink
fi
}
function version {
# tag
# what it does:
# update version in Dockerfile
# save the commit
# tag version on the latest commit
# push tag to remote
App_is_input2_empty
currentBranch=$(git rev-parse --abbrev-ref HEAD)
if [[ "${currentBranch}" == "master" ]]; then
tag_version="${input_2}"
# Prompt a warning
min=1 max=4 message="WARNING: CHANGELOG.md is updated??"
for ACTION in $(seq ${min} ${max}); do
echo -e "${col_pink} ${message} ${col_pink}" && sleep 0.4 && clear && \
echo -e "${col_blue} ${message} ${col_blue}" && sleep 0.4 && clear
done
# update version within the Dockerfile
# We don't want to have "-r1" "-r2" etc within the Dockerfile
ver_in_dockerfile=$(echo $tag_version | sed 's/-r.*//g')
sed -i '' "s/^ARG VERSION=.*$/ARG VERSION=\"$ver_in_dockerfile\"/" Dockerfile
git add . && \
git commit -m "Updated to version: $tag_version" && \
git push && \
sleep 1 && \
# push tag
git tag ${tag_version} && \
git push --tags
else
my_message="You must be a master branch." App_Pink
fi
}
function release {
# ensure that 'version' has tag the latest commit
# then, release on github
App_is_input2_empty
currentBranch=$(git rev-parse --abbrev-ref HEAD)
if [[ "${currentBranch}" == "master" ]]; then
first_name_author=$(git log | awk '/Author:/' | head -n1 | awk '{print $2}')
tag_version="${input_2}"
git_repo_url=$(cat Dockerfile | grep GIT_REPO_URL= | head -n 1 | grep -o '".*"' | sed 's/"//g')
release_message1="Refer to [CHANGELOG.md](${git_repo_url}/blob/master/CHANGELOG.md) for details about this release."
release_message2="This release was packaged and published by using <./utility.sh release>."
release_message3="Enjoy!<br>${first_name_author}"
clear && echo && \
echo "Let's release version: ${tag_version}" && sleep 1 && \
hub release create -oc \
-m "${tag_version}" \
-m "${release_message1}" \
-m "${release_message2}" \
-m "${release_message3}" \
-t "$(git rev-parse HEAD)" \
"${tag_version}"
# https://hub.github.com/hub-release.1.html
# title
# description
# on which commits (use the latest)
# on which tag (use the latest)
echo "${git_repo_url}/releases/tag/${tag_version}"
else
my_message="You must be a master branch." App_Pink
fi
}
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# DOCKER
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
function lint {
docker_image="redcoolbeans/dockerlint"
docker run -it --rm \
-v $(pwd)/Dockerfile:/Dockerfile:ro \
${docker_image}
}
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# OTHER
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
function test {
# uat, idempotent bash script
echo "\$1 is now ${input_1}"
echo "\$2 is now ${input_2}"
echo "\$3 is now ${input_3}"
# Useful when trying to find bad variables along 'set -o nounset'
# validate that hub is installed
if [[ $(hub version | grep -c "hub version") == "1" ]]; then
echo && my_message="Hub is installed." App_Blue
else
echo && my_message="Hub is missing. https://github.com/firepress-org/bash-script-template#requirements" App_Pink
fi
# validate that Docker is installed
if [[ $(docker version | grep -c "Client: Docker Engine") == "1" ]]; then
my_message="Docker is installed." App_Blue
else
my_message="Docker is missing. https://github.com/firepress-org/bash-script-template#requirements" App_Pink
fi
# validate if current directory is a Git repository
# git rev-parse --is-inside-work-tree
my_message="Date is: ${date_sec}" App_Blue
}
function which {
# list, show which functions are available
clear
cat utility.sh | awk '/function /' | awk '{print $2}' | sort -k2 -n | sed '/App_/d' | sed '/main/d' | sed '/utility/d'
}
function App_is_input2_empty {
# ensure the second attribute is not empty to continue
if [[ "${input_2}" == "not-set" ]]; then
my_message="You must provide a valid attribute!" App_Pink
App_stop
fi
}
function App_is_input3_empty {
# ensure the third attribute is not empty to continue
if [[ "${input_3}" == "not-set" ]]; then
my_message="You must provide a valid attribute!" App_Pink
App_stop
fi
}
function example_array {
arr=( "hello" "world" "three" )
for i in "${arr[@]}"; do
echo ${i}
done
}
function example_docs {
cat << EOF
Utility's doc (documentation):
This text is used as a placeholder. Words that will follow won't
make any sense and this is fine. At the moment, the goal is to
build a structure for our site.
Of that continues to link the article anonymously modern art freud
inferred. Eventually primitive brothel scene with a distinction. The
Enlightenment criticized from the history.
EOF
}
function example_figlet {
docker_image="devmtl/figlet:1.0"
message="Hey figlet"
docker run --rm ${docker_image} ${message}
}
function passfull {
grp1=$(openssl rand -base64 32 | sed 's/[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz]//g' | cut -c11-14) && \
grp2=$(openssl rand -base64 32 | sed 's/[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz]//g' | cut -c2-25) && \
grp3=$(openssl rand -base64 32 | sed 's/[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz]//g' | cut -c21-24) && \
clear && \
echo "${grp1}_${grp2}_${grp3}"
}
function passfull_long {
grp1=$(openssl rand -base64 32 | sed 's/[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz]//g' | cut -c11-14) && \
grp2=$(openssl rand -base64 48 | sed 's/[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz]//g' | cut -c2-50) && \
grp3=$(openssl rand -base64 32 | sed 's/[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz]//g' | cut -c21-24) && \
clear && \
echo "${grp1}_${grp2}_${grp3}"
}
function gitignore {
# set and overides .gitignore
cat <<EOF > .gitignore
# Files
############
test
.cache
coverage
dist
node_modules
npm-debug
.env
var-config.sh
# Directories
############
/tmp
/temp
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
# Logs and databases #
######################
*.log
*.sql
*.sqlite
# OS generated files #
######################
.DS_Store
.DS_Store?
.vscode
.Trashes
ehthumbs.db
Thumbs.db
.AppleDouble
.LSOverride
.metadata_never_index
# Thumbnails
############
._*
# Icon must end with two \r
###########################
Icon
# Files that might appear in the root of a volume
#################################################
.DocumentRevisions-V100
.fseventsd
.dbfseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.trash
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.com.apple.timemachine.supported
.PKInstallSandboxManager
.PKInstallSandboxManager-SystemSoftware
.file
.hotfiles.btree
.quota.ops.user
.quota.user
.quota.ops.group
.quota.group
.vol
.efi
# Directories potentially created on remote AFP share
#####################################################
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
.Mobile*
.disk_*
# Sherlock files
################
TheFindByContentFolder
TheVolumeSettingsFolder
.FBCIndex
.FBCSemaphoreFile
.FBCLockFolder
EOF
}
function App_stop {
echo && exit 1
}
function App_DefineVariables {
#==============================================
# Date generators
date_nano="$(date +%Y-%m-%d_%HH%Ms%S-%N)";
date_sec="$(date +%Y-%m-%d_%HH%Ms%S)";
date_min="$(date +%Y-%m-%d_%HH%M)";
date_hour="$(date +%Y-%m-%d_%HH)XX";
date_day="$(date +%Y-%m-%d)";
date_month="$(date +%Y-%m)-XX";
date_year="$(date +%Y)-XX-XX";
# 2017-02-22_10H24_14-500892448
# 2017-02-22_10H24_14
# 2017-02-22_10H24
# 2017-02-22_10HXX
# 2017-02-22
# 2017-02-XX
# 2017-XX-XX
#==============================================
# Define color for echo prompts:
export col_std="\e[39m——>\e[39m"
export col_grey="\e[39m——>\e[39m"
export col_blue="\e[34m——>\e[39m"
export col_pink="\e[35m——>\e[39m"
export col_green="\e[36m——>\e[39m"
export col_white="\e[97m——>\e[39m"
export col_def="\e[39m"
#==============================================
# Placeholders
#export GITHUB_TOKEN="$(cat ~/secrets_open/token_github/token.txt)"
}
function App_Pink {
echo -e "${col_pink} ERROR: ${my_message}"
}
function App_Blue {
echo -e "${col_blue} ${my_message}"
}
function App_Green {
echo -e "${col_green} ${my_message}"
}
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# ENTRYPOINT
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
function main() {
trap script_trap_err ERR
trap script_trap_exit EXIT
# shellcheck source=.bashcheck.sh
source "$(dirname "${BASH_SOURCE[0]}")/.bashcheck.sh"
App_DefineVariables
# input management
input_1=$1
if [[ -z "$1" ]]; then #if empty
clear
my_message="You must provide at least one attribute." App_Pink
App_stop
else
input_1=$1
fi
if [[ -z "$2" ]]; then #if empty
input_2="not-set"
else
input_2=$2
fi
if [[ -z "$3" ]]; then #if empty
input_3="not-set"
else
input_3=$3
fi
script_init "$@"
cron_init
colour_init
#lock_init system
# Attribute #1
# It accept 2 other attributes
clear
$1
}
main "$@"
echo
# https://github.com/firepress-org/bash-script-template