Skip to content

Commit 2b47d3b

Browse files
committed
builbo: some script clean-ups
Signed-off-by: Michael Adam <obnox@samba.org>
1 parent 75619e6 commit 2b47d3b

1 file changed

Lines changed: 144 additions & 92 deletions

File tree

cli/builbo

Lines changed: 144 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,44 @@
33
function detect_container_command() {
44

55

6-
if [ -z "${CONTAINER_CMD}" ]; then
7-
CONTAINER_CMD="$(podman version > /dev/null 2>&1 && echo podman)"
8-
fi
9-
10-
if [ -z "${CONTAINER_CMD}" ]; then
11-
CONTAINER_CMD="$(docker version > /dev/null 2>&1 && echo docker)"
12-
13-
fi
6+
if [ -z "${DEFAULT_CONTAINER_CMD}" ]; then
7+
DEFAULT_CONTAINER_CMD="$(podman version > /dev/null 2>&1 && echo podman)"
8+
fi
9+
if [ -z "${DEFAULT_CONTAINER_CMD}" ]; then
10+
DEFAULT_CONTAINER_CMD="$(docker version > /dev/null 2>&1 && echo docker)"
11+
fi
12+
CONTAINER_CMD="${DEFAULT_CONTAINER_CMD}"
1413
}
1514

1615

1716

1817
# default values for ccommand line options:
19-
LANG=c
20-
REGISTRY=quay.io
18+
#
19+
#DEFAULT_CONTAINER_CMD=""
20+
DEFAULT_LANG="c"
21+
LANG="${DEFAULT_LANG}"
22+
DEFAULT_REGISTRY="quay.io"
23+
REGISTRY="${DEFAULT_REGISTRY}"
2124
CONTAINER_CMD=""
22-
OS=fedora
23-
NAMESPACE=buildbox
25+
DEFAULT_OS=fedora
26+
OS="${DEFAULT_OS}"
27+
DEFAULT_NAMESPACE="buildbox"
28+
NAMESPACE="${DEFAULT_NAMESPACE}"
2429
BUILD_CMD=""
2530
DEPS=""
2631
DEFAULT_ACTION="help"
27-
ACTION="$DEFAULT_ACTION"
28-
# action that was explicxitly set from the command line:
29-
EXPLICIT_ACTION=""
32+
ACTION="${DEFAULT_ACTION}"
33+
# action that was explixitly set from the command line:
34+
SELECTED_ACTION=""
35+
ACTION=""
3036
# shell for interactive use:
31-
I_SHELL="bash"
37+
DEFAULT_I_SHELL="bash"
38+
I_SHELL="${DEFAULT_I_SHELL}"
3239

3340

3441
detect_container_command
3542

36-
if [ -z "$CONTAINER_CMD" ]; then
43+
if [ -z "${DEFAULT_CONTAINER_CMD}" ]; then
3744
echo "error: failed to detect container command (podman or docker)."
3845
exit 1
3946
fi
@@ -43,17 +50,17 @@ function usage() {
4350
4451
builbo: build software projects in buildbox containers.
4552
46-
USAGE: $0 options
53+
USAGE: builbo options
4754
4855
These are the supported options:
49-
[ -l | --lang (c|latex) ] - compilation language (default: $LANG)
50-
[ -r | --registry (quay.io|... ] - container registry (default: $REGISTRY)
51-
[ -c | --container-cmd (podman|docker) ] - default: $CONTAINER_CMD (auto-detected)
52-
[ -o | --os (fedora|debian|ubuntu|rocky|suse) ] - linux distro (default: $OS)
53-
[ -n | --registry-namespace (...) ] - default: $NAMESPACE
56+
[ -l | --lang (c|latex) ] - compilation language (default: ${DEFAULT_LANG})
57+
[ -r | --registry (quay.io|... ] - container registry (default: ${DEFAULT_REGISTRY})
58+
[ -c | --container-cmd (podman|docker) ] - default: ${DEFAULT_CONTAINER_CMD} (auto-detected)
59+
[ -o | --os (fedora|debian|ubuntu|rocky|suse) ] - linux distro (default: ${DEFAULT_OS})
60+
[ -n | --registry-namespace (...) ] - default: ${DEFAULT_NAMESPACE}
5461
[ -s | --build-script (command|path) ] - command or local script (in the CWD) for building the project.
5562
[ -d | --deps (pkg,pkg,pkg,...) ] - additional packages to install (comma-separated)
56-
-i | --shell (bash) ] - sinteractive shell (default: $I_SHELL)
63+
[ -i | --shell (bash) ] - interactive shell (default: ${DEFAULT_I_SHELL})
5764
[ -h | --help ] - action help: print this usage information
5865
[-t | --test ] - action test: print some diagnostic info for testing this program
5966
[ -b | --build ] - action build: perform a build in the CWD
@@ -78,15 +85,17 @@ function set_action() {
7885

7986
local action="$1"
8087

81-
82-
83-
if [ -n "$EXPLICIT_ACTION" ]; then
88+
if [ -n "${SELECTED_ACTION}" ]; then
8489
echo "Error: multiple actions specified on cmdline but only one is allowed."
8590
usage
8691
exit 1
8792
fi
88-
ACTION="$action"
89-
EXPLICIT_ACTION="$action"
93+
94+
95+
96+
echo "selecting action '${action}'"
97+
ACTION="${action}"
98+
SELECTED_ACTION="${action}"
9099

91100
}
92101

@@ -99,53 +108,101 @@ optstring_long="lang:,registry:,container-cmd:,help,test,build,enter,build-scrip
99108

100109

101110
parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")
102-
args_valid=$?
111+
args_are_valid=$?
103112

104-
if [ "$args_valid" != "0" ]; then
113+
if [ "$args_are_valid" != "0" ]; then
105114
echo "error:invalid args."
106115
usage
107116
exit 1
108117
fi
109118

110119
# processing parsed args
111120

112-
eval set -- "$parsed_args"
121+
echo " processing command line arguments.."
122+
eval set -- "${parsed_args}"
113123
while :
114124
do
115-
case "$1" in
116-
-l | --lang) LANG="$2" ; shift 2 ;;
117-
-r | --registry) REGISTRY="$2" ; shift 2 ;;
118-
-c | --container-cmd) CONTAINER_CMD="$2"; shift 2 ;;
119-
-o | --os) OS="$2"; shift 2 ;;
120-
-n | --registry-namespace) NAMESPACE="$2" ; shift 2 ;;
121-
-s | --build-script) BUILD_CMD="$2" ; shift 2 ;;
122-
-d | --deps) DEPS="$2"; shift 2 ;;
123-
-h | --help) set_action "help" ; shift ;;
124-
-b| --build) set_action "build" ; shift ;;
125-
-t| --test) set_action "test" ; shift ;;
126-
-e|--enter) set_action "enter" ; shift ;;
125+
arg="$1"
126+
echo "processing argument '${arg}'."
127+
case "${arg}" in
128+
-l | --lang)
129+
LANG="$2"
130+
echo "language '${LANG}' specified."
131+
shift 2
132+
;;
133+
-r | --registry)
134+
REGISTRY="$2"
135+
echo "registry '${REGISTRY}' specified."
136+
137+
shift 2
138+
;;
139+
-c | --container-cmd)
140+
CONTAINER_CMD="$2"
141+
echo "container command '${CONTAINER_CMD}' specified."
142+
shift 2
143+
;;
144+
-o | --os)
145+
OS="$2"
146+
echo "operating system '${OS}'specified."
147+
shift 2
148+
;;
149+
-n | --registry-namespace)
150+
NAMESPACE="$2"
151+
echo "namespace '${NAMESPACE}' specified."
152+
shift 2
153+
;;
154+
-s | --build-script)
155+
BUILD_CMD="$2"
156+
echo "build command '${BUILD_CMD}' specified."
157+
shift 2
158+
;;
159+
-d | --deps)
160+
DEPS="$2"
161+
echo "dependencies '${DEPS}' specified."
162+
shift 2
163+
;;
164+
-h | --help)
165+
set_action "help"
166+
echo "action help specified."
167+
shift
168+
;;
169+
-b| --build)
170+
echo "action build specified."
171+
set_action "build"
172+
echo
173+
shift
174+
;;
175+
-t| --test)
176+
set_action "test"
177+
echo "action test specified."
178+
shift
179+
;;
180+
-e|--enter)
181+
set_action "enter"
182+
echo "action enter specified."
183+
shift
184+
;;
127185

128186

129187
# -- means end of args. ignore and stop processing.
130-
--) shift ; break ;;
131-
*) echo "Unexpected option $1. - this should not happen."
132-
# this should have been caught above with args_valid
133-
usage
134-
exit 1
188+
--)
189+
shift
190+
break
135191
;;
136-
esac
192+
*)
193+
echo "Unexpected option $1. - this should not happen."
194+
# this should have been caught above with args_valid
195+
usage
196+
exit 1
197+
;;
198+
esac
137199
done
200+
echo "done processing arguments."
138201

139-
# done processing args
140-
141-
142-
143-
144-
202+
#post-processing args
145203

146204

147205

148-
#post-processing args
149206

150207

151208
if [ -z "$CONTAINER_CMD" ]; then
@@ -155,10 +212,10 @@ if [ -z "$CONTAINER_CMD" ]; then
155212
fi
156213

157214
DEPS_PKGS="${DEPS//,/ }"
158-
IMAGE="$REGISTRY/$NAMESPACE/buildbox/$OS-$LANG:latest"
215+
IMAGE="${REGISTRY}/${NAMESPACE}/buildbox/${OS}-${LANG}:latest"
159216

160217

161-
case "$OS" in
218+
case "${OS}" in
162219

163220
fedora)
164221
PKG_CMD="dnf install -y"
@@ -177,28 +234,23 @@ esac
177234

178235

179236
# setting CONTAINER_RUN_CMD assumes CONTAINER_RUN_CMD is set.
180-
if [ -z "$CONTAINER_CMD" ]; then
237+
if [ -z "${CONTAINER_CMD}" ]; then
181238
echo "error: container command not set. this should not happen."
182239
exit 1
183240
fi
184241
if [ "$CONTAINER_CMD" = "podman" ]; then
185242

186-
CONTAINER_RUN_CMD="$CONTAINER_CMD run -it --workdir /work --mount type=bind,source=$(pwd),target=/work,relabel=private --platform linux/amd64 $IMAGE"
243+
CONTAINER_RUN_CMD="${CONTAINER_CMD} run -it --workdir /work --mount type=bind,source=$(pwd),target=/work,relabel=private --platform linux/amd64 ${IMAGE}"
187244
elif [ "$CONTAINER_CMD" = "docker" ]; then
188-
CONTAINER_RUN_CMD="$CONTAINER_CMD run --workdir /work -v $(pwd):/work --platform linux/amd64 $IMAGE"
189-
190-
fi
245+
CONTAINER_RUN_CMD="${CONTAINER_CMD} run --workdir /work -v $(pwd):/work --platform linux/amd64 ${IMAGE}"
191246

192-
if [ -z "$ACTION" ]; then
193-
echo "error: no action selected. Please use exactly one of -t, -h, and -b."
194-
usage
195-
exit 1
196247
fi
197248

198-
if [ "$ACTION" = "build" ]; then
249+
250+
if [ "${ACTION}" = "build" ]; then
199251

200252

201-
if [ -z "$BUILD_CMD" ]; then
253+
if [ -z "${BUILD_CMD}" ]; then
202254

203255
echo "error: action 'build' requires --build-script (-s)"
204256
usage
@@ -214,18 +266,18 @@ fi
214266
function print_settings() {
215267
echo "settings:"
216268
echo
217-
echo "LANG: $LANG"
218-
echo "OS: $OS"
219-
echo "REGISTRY: $REGISTRY"
220-
echo "NAMESPACE: $NAMESPACE"
221-
echo "BUILD_CMD: $BUILD_CMD"
222-
echo "DEPS: $DEPS"
223-
echo "DEPS_PKGS: $DEPS_PKGS"
224-
echo "PKG_CMD: $PKG_CMD"
225-
echo "CONTAINER_CMD: $CONTAINER_CMD"
226-
echo "CONTAINER_RUN_CMD: '$CONTAINER_RUN_CMD'"
227-
echo "interactive shell: '$I_SHELL'"
228-
echo "IMAGE: $IMAGE"
269+
echo "LANG: ${LANG}"
270+
echo "OS: ${OS}"
271+
echo "REGISTRY: ${REGISTRY}"
272+
echo "NAMESPACE: ${NAMESPACE}"
273+
echo "BUILD_CMD: ${BUILD_CMD}"
274+
echo "DEPS: ${DEPS}"
275+
echo "DEPS_PKGS: ${DEPS_PKGS}"
276+
echo "PKG_CMD: ${PKG_CMD}"
277+
echo "CONTAINER_CMD: ${CONTAINER_CMD}"
278+
echo "CONTAINER_RUN_CMD: '${CONTAINER_RUN_CMD}'"
279+
echo "interactive shell: '${I_SHELL}'"
280+
echo "IMAGE: ${IMAGE}"
229281

230282
}
231283

@@ -235,7 +287,7 @@ if [ "$ACTION" = "test" ]; then
235287
exit 0
236288
fi
237289

238-
if [ "$ACTION" = "help" ]; then
290+
if [ "${ACTION}" = "help" ]; then
239291

240292
echo "performing help action."
241293

@@ -259,8 +311,8 @@ fi
259311

260312

261313
function install_deps() {
262-
if [ -n "$DEPS" ]; then
263-
$CONTAINER_RUN_CMD bash -c "$PKG_CMD $DEPS_PKGS"
314+
if [ -n "${DEPS}" ]; then
315+
${CONTAINER_RUN_CMD} bash -c "${PKG_CMD} ${DEPS_PKGS}"
264316

265317
fi
266318

@@ -270,28 +322,28 @@ function install_deps_and_run_build() {
270322

271323

272324

273-
local CMD="$BUILD_CMD"
325+
local CMD="${BUILD_CMD}"
274326

275-
if [ -n "$DEPS" ]; then
276-
CMD="$PKG_CMD $DEPS_PKGS && $BUILD_CMD"
327+
if [ -n "${DEPS}" ]; then
328+
CMD="${PKG_CMD} ${DEPS_PKGS} && ${BUILD_CMD}"
277329

278330
fi
279331

280-
$CONTAINER_RUN_CMD bash -c "$CMD"
332+
${CONTAINER_RUN_CMD} bash -c "${CMD}"
281333
}
282334

283335
function enter_container() {
284336

285-
$CONTAINER_RUN_CMD "$I_SHELL"
337+
${CONTAINER_RUN_CMD} "${I_SHELL}"
286338

287339
}
288340

289341

290-
if [ "$ACTION" = "build" ]; then
342+
if [ "${ACTION}" = "build" ]; then
291343
echo "performing build action."
292344
install_deps_and_run_build
293345

294-
elif [ "$ACTION" = "enter" ]; then
346+
elif [ "${ACTION}" = "enter" ]; then
295347
echo "performing enter action."
296348
enter_container
297349
fi

0 commit comments

Comments
 (0)