-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.sh
executable file
·96 lines (70 loc) · 2.29 KB
/
template.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
#!/bin/bash
set -euo pipefail
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
GREEN=$(tput setaf 2)
DEFAULT=$(tput sgr0)
function to_snake_case() {
echo "${1//-/_}"
}
function to_camel_case() {
echo "$1" | awk -F - '{printf "%s", $1; for(i=2; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i,2); print"";}'
}
function to_pascal_case() {
camelCase=$(to_camel_case "$1")
echo "${camelCase}" | awk '{print toupper(substr($0,0,1))substr($0,2)}'
}
function warn() {
printf "%s\n" "${RED}$1${DEFAULT}"
}
function attention() {
printf "%s\n" "${YELLOW}$1${DEFAULT}"
}
function info() {
printf "%s\n" "${GREEN}$1${DEFAULT}"
}
function replace() {
attention "Replacing instances of '$1' with '$2'"
find . \
\( -name "*.go" -o -name "*.yml" -o -name "*.proto" -o -name "Dockerfile" -o -name "Makefile" -o -name "go.mod" \) \
-exec sed -i '' 's/'"$1"'/'"$2"'/g' {} \;
}
templateProjectName="new-collector-template"
projectName="${1-}"
if [ -z "$projectName" ]; then
warn "Must supply a hyphenated project name"
warn "ex: ./template.sh my-collector-name"
exit 1
fi
info "Updating template to use name '$projectName'"
warn "Removing generated protobuf code"
rm -f proto/v1alpha1/*.pb.go
rm -f proto/v1alpha1/*.pb.*.go
warn "Renaming protobuf file"
mv "proto/v1alpha1/${templateProjectName}.proto" "proto/v1alpha1/${projectName}.proto"
replace "$templateProjectName" "$projectName"
templateProjectNameCamelCase=$(to_camel_case "$templateProjectName")
projectNameCamelCase=$(to_camel_case "$projectName")
replace "$templateProjectNameCamelCase" "$projectNameCamelCase"
templateProjectNamePascalCase=$(to_pascal_case "$templateProjectName")
projectNamePascalCase=$(to_pascal_case "$projectName")
replace "$templateProjectNamePascalCase" "$projectNamePascalCase"
templateProjectNameSnakeCase=$(to_snake_case "$templateProjectName")
projectNameSnakeCase=$(to_snake_case "$projectName")
replace "$templateProjectNameSnakeCase" "$projectNameSnakeCase"
info "Regenerating protos"
make generate
info "Formatting"
make fmt
info "Building binary"
go build -v .
info "Running tests"
make test
info "Cleaning up binary"
rm "$projectName"
info "Updating README"
echo "# $projectName" > README.md
info "Building Docker image"
docker build -t "$projectName" .
info "Done, deleting this script"
rm -- "$0"