forked from naughtygopher/goapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeitmine.sh
executable file
·97 lines (81 loc) · 2.46 KB
/
makeitmine.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
#!/bin/bash
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
cwd=${PWD}
basepath=$(dirname -- ${BASH_SOURCE[0]})
thisfile=`basename "$0"`
fontcolorgreen=$(tput setaf 2)
fontbold=$(tput bold)
fontnormal=$(tput sgr0)
existingmodulename="github.com/bnkamalesh/goapp"
modulename=''
a_flag=false
print_usage() {
printf """
Usage:
./makeitmine.sh -n <module name> -a -e example.org/me/myapp
Options
=======
-n Parameter accepts the new module name, and is mandatory
-a Optional flag if set, will do the following:
- remove .git directory
- empty README.md
- remove .travis.yml
-e Optional parameter accepts an existing module name to be replaced. Default is 'github.com/bnkamalesh/pusaki'
"""
}
# https://stackoverflow.com/a/7069755
while getopts 'an:e:' flag; do
case "${flag}" in
a) a_flag=true;;
n) modulename="${OPTARG}" ;;
e) existingmodulename="${OPTARG}" ;;
*) print_usage
echo "exiting?"
exit 1 ;;
esac
done
if [ -z "$modulename" ]
then
print_usage
exit 2;
fi
if [ -z "$existingmodulename" ]
then
print_usage
exit 3;
fi
totalsteps="2"
if [ $a_flag = true ]
then
totalsteps="3"
fi
currentStep=1
printf "${fontbold}[$currentStep/$totalsteps] Setting up your module '$modulename'${fontnormal}\n"
# Replacing . with \. to escape . while being used in regex
unescapedmodule=$modulename
modulename=${modulename//./\\.}
unescapedexistingmodule=$existingmodulename
existingmodulename=${existingmodulename//./\\.}
# using ; as separator in sed, because `/` is prevalent in most Go module paths
replacer="s;$existingmodulename;$modulename;g"
echo " - Replacing module '$unescapedexistingmodule' with '$unescapedmodule'"
grep -rl $existingmodulename ${basepath}/ --exclude-dir=.git --exclude-dir=vendor --exclude=README.md --exclude=$thisfile | xargs sed -i $replacer
printf "${fontcolorgreen}${fontbold}= Done${fontnormal}\n"
if [ $a_flag = true ]
then
((currentStep++))
printf "\n${fontbold}7 Deleting .git and emptying README${fontnormal}\n"
rm -rf ${basepath}/.git ${basepath}/.travis.yml
echo "" > ${basepath}/README.md
printf "${fontcolorgreen}${fontbold}= Done${fontnormal}\n"
fi
((currentStep++))
printf "\n${fontbold}[$currentStep/$totalsteps] Go clean-up ${fontnormal}\n"
cd $basepath
go mod tidy
go mod verify
printf "${fontcolorgreen}${fontbold}= Done${fontnormal}\n"
printf "\n${fontcolorgreen}${fontbold}=== Done [$currentStep/$totalsteps]${fontnormal}\n\n"
exit 0;