-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.sh
executable file
·178 lines (128 loc) · 2.5 KB
/
build.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
#!/bin/bash
# constants
AMD64="amd64"
ARM="arm"
ARM64="arm64"
LINUX="linux"
# colors
BLUE=34
CYAN=36
GREEN=32
RED=31
# defaults
DEFAULT_OS="linux"
DEFAULT_ARCH="amd64"
# helpers
# text, color
color()
{
echo "\e[$2m$1\e[0m"
}
timestamp()
{
# t="$date %T"
# echo $t
# echo "$(color $(t) $RED)"
echo "$(date)"
}
error()
{
echo -e "[$(timestamp)] \e[31m$1\e[0m"
exit
}
log()
{
echo -e "[$(timestamp)] \e[32m$1\e[0m"
#echo -e "$(timestamp) $(color $1 $CYAN)"
}
# usage
usage()
{
error "Usage: please set env variables GOOS and GOARCH"
exit
}
# check
check()
{
log "Checking env..."
if [ -z $GOOS ]; then
log "GOOS env variable not set, using $DEFAULT_OS"
GOOS=$DEFAULT_OS
fi
if [ -z $GOARCH ]; then
log "GOARCH env variable not set, using $DEFAULT_ARCH"
GOARCH=$DEFAULT_ARCH
fi
if ! [[ $GOOS == $LINUX ]]; then
log "GOOS not supported: $GOOS, only $LINUX is supported."
fi
if ! [[ $GOARCH == $AMD64 || $GOARCH == $ARM || $GOARCH == $ARM64 ]]; then
log "GOARCH not supported: $GOARCH, only $AMD64, $ARM, and $ARM64 are supported."
fi
go version > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
error "golang has not been installed, please see https://golang.org."
fi
migrate -version > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
error "golang-migrate/migrate has not been installed, please see https://github.com/golang-migrate/migrate"
fi
if ! [ -d debian/usr/local/rpic ]; then
mkdir -p debian/usr/local/rpic
fi
}
# clean
clean()
{
log "Cleaning environment"
if [[ -f "rpic" ]]; then
log "Removing rpic binary"
rm rpic
fi
if [[ -f "rpic.db" ]]; then
log "Removing rpic.db"
rm rpic.db
fi
if [[ -f "debian.deb" ]]; then
log "Removing debian.deb packages"
rm debian.deb
fi
}
# build: GOOS, GOARCH
build()
{
go build
}
# init_database
init_database()
{
log "Initializing database"
migrate -database sqlite://rpic.db -path db/migrations up
}
# stage
stage()
{
log "Copying rpic binary to debian for staging"
cp rpic debian/usr/local/rpic/
log "Copying rpic web templates to debian for staging"
cp -R www debian/usr/local/rpic/
log "Copying rpic database to debian for staging"
cp rpic.db debian/usr/local/rpic/
}
# package
package()
{
log "Building debian package"
dpkg-deb --build debian
if [[ -f debian.deb ]]; then
log "Renaming debian package..."
mv debian.deb "rpic-0.1-$GOOS-$GOARCH.deb"
fi
}
log "Build started..."
clean
check
build
init_database
stage
package