-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh
executable file
·190 lines (155 loc) · 2.98 KB
/
run.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
set -eu -o pipefail
# Get the directory that this script file is in
THIS_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd "$THIS_DIR"
function _default() {
# shellcheck disable=SC2119
api
}
function _die() {
echo >&2 "Fatal: ${*}"
exit 1
}
function _installed() {
hash "$1" >/dev/null 2>&1
}
function _git-xargs() {
local PATTERN=$1
shift
git ls-files --exclude="$PATTERN" -ciz | xargs -0 -I _ "$@"
}
function help() {
local SCRIPT=$0
cat <<EOF
Usage
$SCRIPT <task> <args>
Tasks:
EOF
compgen -A function | grep -e '^_' -v | sort | xargs printf ' - %s\n'
exit 2
}
function sql() {
set -x
format:sql
sql:sqlc
set +x
}
function sql:sqlc() {
_installed sqlc || _die "sqlc not installed"
{
cd sql
sqlc generate
sqlc compile
sqlc vet
}
}
function db:migrate() {
cd sql/schema
tern migrate "$@"
}
function db:migrate:prod() {
cd sql/schema
tern migrate -c prod.conf "$@"
}
function build:frontend() {
yarn run build
}
function build:backend() {
go version
set -x
echo "${DEPLOY_PRIME_URL:-http://local.dev}" >pkg/almanack/deploy-url.txt
GOBIN=$THIS_DIR/functions go install ./funcs/...
cp "$THIS_DIR/functions/almanack-api" "$THIS_DIR/functions/almanack-api-background"
set +x
}
function build:prod() {
build:backend
build:frontend
}
function test() {
set -x
test:backend
test:frontend
test:misc
set +x
}
function test:frontend() {
yarn run test
}
function test:backend() {
go test -race -v ./...
}
function test:db() {
ALMANACK_POSTGRES=$PG_LOCAL_URL go test "$@" -v ./internal/db
}
function test:misc() {
_git-xargs '*.sh' shellcheck _
go mod tidy -diff
}
function format() {
set -x
format:js
format:go
format:sh
format:sql
set +x
}
function format:js() {
yarn run lint
}
function format:go() {
gofmt -s -w .
}
function format:sh() {
_git-xargs '*.sh' shfmt -w _
}
function format:sql() {
_git-xargs '*.sql' pg_format -w 80 -s 2 -i _
}
function db:copy-prod() {
local DUMP_FILE
local DATE_NAME
echo "Using $PG_BIN"
set -x
DATE_NAME=$(date -u +"%Y-%m-%dT%H:%M:%S")
DUMP_FILE="$(mktemp -d)/dump-$DATE_NAME.sql.tar"
db:dump-prod "$DUMP_FILE"
db:load-dump "$DUMP_FILE"
set +x
}
function db:dump-prod() {
local DUMP_FILE=$1
"${PG_BIN}pg_dump" \
-d "$PG_PROD_URL" \
--verbose \
--no-owner \
--format=tar \
--file="$DUMP_FILE"
}
function db:load-dump() {
local DUMP_FILE=$1
"${PG_BIN}pg_restore" \
-d "$PG_LOCAL_URL" \
--verbose \
--clean \
--no-owner \
"$DUMP_FILE"
}
# shellcheck disable=SC2120
function api() {
# shellcheck disable=SC1091
[[ -f .env ]] && echo "Using .env file" && source .env
${GO_EXEC:-go} run ./funcs/almanack-api "$@"
}
function frontend() {
yarn run serve
}
function check-deps() {
_installed shellcheck || echo "install https://www.shellcheck.net"
_installed shfmt || echo "install https://github.com/mvdan/sh"
_installed sqlc || echo "install https://sqlc.dev"
_installed tern || echo "install https://github.com/jackc/tern"
}
TIMEFORMAT="Task completed in %1lR"
time "${@:-_default}"