-
Notifications
You must be signed in to change notification settings - Fork 0
/
runme.sh
executable file
·190 lines (162 loc) · 4 KB
/
runme.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
#!/bin/bash -e
# Syntax runme.sh [-d|--dryrun] [-h|--help]
DRY_RUN=0
STEP_COUNTER=1
NEW_APP_NAME=
DEFAULT_APP_NAME=
RANDOM_BRANCH=
help() {
echo """
> runme.sh [-d|--dryrun] [-h|--help]
"""
}
user_confirmation() {
read -p "$1" USER_CONFIRMATION
if [ -z $USER_CONFIRMATION ] || [ $USER_CONFIRMATION = "n" ] || [ $USER_CONFIRMATION = "no" ]
then
echo "Aborting..."
exit 1
elif [ $USER_CONFIRMATION = "y" ] || [ $USER_CONFIRMATION = "yes" ]
then
echo "Proceeding..."
return 0
else
echo "Aborting..."
exit 2
fi
}
check_command_exists() {
CMD=$1
BREW_PKG=$2
VERSION_CMD=$3
if ! $CMD $VERSION_CMD &> /dev/null
then
echo "!!! $CMD command not found"
user_confirmation "Would you like to install it (y/N): "
brew install $BREW_PKG
else
echo "✅ $CMD already installed"
fi
}
verify_prerequisites() {
OS_FLAVOR=`uname`
if [ $OS_FLAVOR = "Darwin" ]
then
check_command_exists gsed gnu-sed --version
check_command_exists xxd xxd --version
fi
}
emptycheck() {
if [ -z "$2" ]
then
echo
echo "!!! Error: $1 cannot be empty."
return "-1"
else
return 0
fi
}
step() {
echo
echo "⭐ Step $STEP_COUNTER: $1"
echo
STEP_COUNTER=$((STEP_COUNTER+1))
if [ "$1" == "Done" ]
then
echo "My job here is done. Removing myself...good-bye!"
git rm -f runme.sh
git commit -q -m "$NEW_APP_NAME: remove runme.sh"
git push -q origin $RANDOM_BRANCH
fi
}
read_user_input() {
return $
}
# detect origin git url and appname
GIT_URL=$(git config --get remote.origin.url)
if [[ $GIT_URL =~ ^git.* ]]
then
tmp=${GIT_URL#"git@"}
tmp=${tmp%".git"}
tmp=${tmp/:/\/}
GIT_URL="https://${tmp}"
fi
DEFAULT_APP_NAME=${GIT_URL##*\/}
get_user_input() {
step "User Input"
read -p "☞ Enter new micro-service name [$DEFAULT_APP_NAME]: " NEW_APP_NAME
NEW_APP_NAME=${NEW_APP_NAME:-$DEFAULT_APP_NAME}
emptycheck "New micro-service Name" $NEW_APP_NAME
}
while [[ $# -gt 0 ]]
do
key="${1}"
case ${key} in
-d|--dryrun)
DRY_RUN=1
shift
;;
-h|--help)
help
exit 0
;;
-s|--skip-git)
SKIP_GIT=1
shift
;;
*) # unknown
echo Unknown Parameter $1
exit 4
esac
done
echo "*************************************************"
echo "* Create a ETI SRE template micro-service *"
echo "*************************************************"
echo
if [ "$DRY_RUN" = 1 ]
then
echo "!!! DRY RUN ONLY"
fi
if [ "$SKIP_GIT" = 1 ]
then
echo "!!! SKIP GIT OPS"
fi
step "Verifying Pre-requisites"
verify_prerequisites
get_user_input
step "User Confirmation"
echo "***********************************************************"
echo "New micro-service Name : $NEW_APP_NAME"
echo "***********************************************************"
echo
echo
user_confirmation "Please confirm to proceed with configuring this directory for \"$NEW_APP_NAME\" (y/N): "
if [ "$DRY_RUN" = 1 ]
then
echo
echo "DRY RUN Complete"
exit 0
fi
if [ -z $SKIP_GIT ]
then
RANDOM_BRANCH_POSTFIX=$(xxd -l 4 -c 4 -p < /dev/random)
RANDOM_BRANCH=$NEW_APP_NAME-$RANDOM_BRANCH_POSTFIX
step "Create a new git branch: $RANDOM_BRANCH"
git checkout -b $RANDOM_BRANCH
fi
step "Update template for $NEW_APP_NAME"
find . -type f ! -name 'runme.sh' ! -name 'README.md' ! -path '*/.git/*' -exec gsed -i "s/sre-go-helloworld/${NEW_APP_NAME}/g" {} +
find . -type f ! -name 'runme.sh' ! -name 'README.md' ! -path '*/.git/*' -exec gsed -i "s/helloworld/${NEW_APP_NAME}/g" {} +
find . -type d -iname '*sre-go-helloworld*' ! -path '*/.git/*' -depth -exec bash -c 'mv "$1" "${1/sre-go-helloworld/'${NEW_APP_NAME}'}"' -- '{}' ';'
find . -type f -iname '*sre-go-helloworld*' ! -path '*/.git/*' -depth -exec bash -c 'mv "$1" "${1/sre-go-helloworld/'${NEW_APP_NAME}'}"' -- '{}' ';'
step "Run gofmt"
gofmt -s -w .
if [ -z $SKIP_GIT ]
then
step "Commit changes to git"
git add .
git commit -m "$NEW_APP_NAME: Executed runme.sh to update template for $NEW_APP_NAME"
step "Push changes to origin branch $RANDOM_BRANCH"
git push origin $RANDOM_BRANCH
fi
step "Done"