Skip to content

Commit f7746ae

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

1 file changed

Lines changed: 144 additions & 89 deletions

File tree

cli/builbo

Lines changed: 144 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,42 @@
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 "${CONTAINER_CMD}" ]; then
7+
CONTAINER_CMD="$(podman version > /dev/null 2>&1 && echo podman)"
8+
fi
9+
if [ -z "${CONTAINER_CMD}" ]; then
10+
CONTAINER_CMD="$(docker version > /dev/null 2>&1 && echo docker)"
11+
fi
1412
}
1513

1614

1715

1816
# default values for ccommand line options:
19-
LANG=c
20-
REGISTRY=quay.io
17+
#
18+
DEFAULT_LANG="c"
19+
LANG="${DEFAULT_LANG}"
20+
DEFAULT_REGISTRY="quay.io"
21+
REGISTRY="${DEFAULT_REGISTRY}"
2122
CONTAINER_CMD=""
22-
OS=fedora
23-
NAMESPACE=buildbox
23+
DEFAULT_OS=fedora
24+
OS="${DEFAULT_OS}"
25+
DEFAULT_NAMESPACE="buildbox"
26+
NAMESPACE="${DEFAULT_NAMESPACE}"
2427
BUILD_CMD=""
2528
DEPS=""
2629
DEFAULT_ACTION="help"
27-
ACTION="$DEFAULT_ACTION"
28-
# action that was explicxitly set from the command line:
29-
EXPLICIT_ACTION=""
30+
ACTION="${DEFAULT_ACTION}"
31+
# action that was explixitly set from the command line:
32+
SELECTED_ACTION=""
33+
ACTION=""
3034
# shell for interactive use:
31-
I_SHELL="bash"
35+
DEFAULT_I_SHELL="bash"
36+
I_SHELL="${DEFAULT_I_SHELL}"
3237

3338

3439
detect_container_command
3540

36-
if [ -z "$CONTAINER_CMD" ]; then
41+
if [ -z "${CONTAINER_CMD}" ]; then
3742
echo "error: failed to detect container command (podman or docker)."
3843
exit 1
3944
fi
@@ -44,16 +49,16 @@ function usage() {
4449
builbo: build software projects in buildbox containers.
4550
4651
USAGE: $0 options
47-
52+
{
4853
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
54+
[ -l | --lang (c|latex) ] - compilation language (default: ${DEFAULT_LANG})
55+
[ -r | --registry (quay.io|... ] - container registry (default: ${DEFAULT_REGISTRY})
56+
[ -c | --container-cmd (podman|docker) ] - default: ${CONTAINER_CMD} (auto-detected)
57+
[ -o | --os (fedora|debian|ubuntu|rocky|suse) ] - linux distro (default: ${DEFAULT_OS})
58+
[ -n | --registry-namespace (...) ] - default: ${DEFAULT_NAMESPACE}
5459
[ -s | --build-script (command|path) ] - command or local script (in the CWD) for building the project.
5560
[ -d | --deps (pkg,pkg,pkg,...) ] - additional packages to install (comma-separated)
56-
-i | --shell (bash) ] - sinteractive shell (default: $I_SHELL)
61+
-i | --shell (bash) ] - sinteractive shell (default: ${DEFAULT_I_SHELL})
5762
[ -h | --help ] - action help: print this usage information
5863
[-t | --test ] - action test: print some diagnostic info for testing this program
5964
[ -b | --build ] - action build: perform a build in the CWD
@@ -78,15 +83,17 @@ function set_action() {
7883

7984
local action="$1"
8085

81-
82-
83-
if [ -n "$EXPLICIT_ACTION" ]; then
86+
if [ -n "${SELECTED_ACTION}" ]; then
8487
echo "Error: multiple actions specified on cmdline but only one is allowed."
8588
usage
8689
exit 1
8790
fi
88-
ACTION="$action"
89-
EXPLICIT_ACTION="$action"
91+
92+
93+
94+
echo "selecting action '${action}'"
95+
ACTION="${action}"
96+
SELECTED_ACTION="${action}"
9097

9198
}
9299

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

100107

101108
parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")
102-
args_valid=$?
109+
args_are_valid=$?
103110

104-
if [ "$args_valid" != "0" ]; then
111+
if [ "$args_are_valid" != "0" ]; then
105112
echo "error:invalid args."
106113
usage
107114
exit 1
108115
fi
109116

110117
# processing parsed args
111118

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

128184

129185
# -- 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
186+
--)
187+
shift
188+
break
135189
;;
136-
esac
190+
*)
191+
echo "Unexpected option $1. - this should not happen."
192+
# this should have been caught above with args_valid
193+
usage
194+
exit 1
195+
;;
196+
esac
137197
done
198+
echo "done processing arguments."
138199

139-
# done processing args
140-
141-
142-
143-
144-
200+
#post-processing args
145201

146202

147203

148-
#post-processing args
149204

150205

151206
if [ -z "$CONTAINER_CMD" ]; then
@@ -155,10 +210,10 @@ if [ -z "$CONTAINER_CMD" ]; then
155210
fi
156211

157212
DEPS_PKGS="${DEPS//,/ }"
158-
IMAGE="$REGISTRY/$NAMESPACE/buildbox/$OS-$LANG:latest"
213+
IMAGE="${REGISTRY}/${NAMESPACE}/buildbox/${OS}-${LANG}:latest"
159214

160215

161-
case "$OS" in
216+
case "${OS}" in
162217

163218
fedora)
164219
PKG_CMD="dnf install -y"
@@ -177,28 +232,28 @@ esac
177232

178233

179234
# setting CONTAINER_RUN_CMD assumes CONTAINER_RUN_CMD is set.
180-
if [ -z "$CONTAINER_CMD" ]; then
235+
if [ -z "${CONTAINER_CMD}" ]; then
181236
echo "error: container command not set. this should not happen."
182237
exit 1
183238
fi
184239
if [ "$CONTAINER_CMD" = "podman" ]; then
185240

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

192-
if [ -z "$ACTION" ]; then
193-
echo "error: no action selected. Please use exactly one of -t, -h, and -b."
247+
if [ -z "${SELECTED_ACTION}" ]; then
248+
echo "error: no action elected. Please use exactly one of -t, -h, -e, and -b."
194249
usage
195250
exit 1
196251
fi
197252

198-
if [ "$ACTION" = "build" ]; then
253+
if [ "${ACTION}" = "build" ]; then
199254

200255

201-
if [ -z "$BUILD_CMD" ]; then
256+
if [ -z "${BUILD_CMD}" ]; then
202257

203258
echo "error: action 'build' requires --build-script (-s)"
204259
usage
@@ -214,18 +269,18 @@ fi
214269
function print_settings() {
215270
echo "settings:"
216271
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"
272+
echo "LANG: ${LANG}"
273+
echo "OS: ${OS}"
274+
echo "REGISTRY: ${REGISTRY}"
275+
echo "NAMESPACE: ${NAMESPACE}"
276+
echo "BUILD_CMD: ${BUILD_CMD}"
277+
echo "DEPS: ${DEPS}"
278+
echo "DEPS_PKGS: ${DEPS_PKGS}"
279+
echo "PKG_CMD: ${PKG_CMD}"
280+
echo "CONTAINER_CMD: ${CONTAINER_CMD}"
281+
echo "CONTAINER_RUN_CMD: '${CONTAINER_RUN_CMD}'"
282+
echo "interactive shell: '${I_SHELL}'"
283+
echo "IMAGE: ${IMAGE}"
229284

230285
}
231286

@@ -235,7 +290,7 @@ if [ "$ACTION" = "test" ]; then
235290
exit 0
236291
fi
237292

238-
if [ "$ACTION" = "help" ]; then
293+
if [ "${ACTION}" = "help" ]; then
239294

240295
echo "performing help action."
241296

@@ -259,8 +314,8 @@ fi
259314

260315

261316
function install_deps() {
262-
if [ -n "$DEPS" ]; then
263-
$CONTAINER_RUN_CMD bash -c "$PKG_CMD $DEPS_PKGS"
317+
if [ -n "${DEPS}" ]; then
318+
${CONTAINER_RUN_CMD} bash -c "${PKG_CMD} ${DEPS_PKGS}"
264319

265320
fi
266321

@@ -270,28 +325,28 @@ function install_deps_and_run_build() {
270325

271326

272327

273-
local CMD="$BUILD_CMD"
328+
local CMD="${BUILD_CMD}"
274329

275-
if [ -n "$DEPS" ]; then
276-
CMD="$PKG_CMD $DEPS_PKGS && $BUILD_CMD"
330+
if [ -n "${DEPS}" ]; then
331+
CMD="${PKG_CMD} ${DEPS_PKGS} && ${BUILD_CMD}"
277332

278333
fi
279334

280-
$CONTAINER_RUN_CMD bash -c "$CMD"
335+
${CONTAINER_RUN_CMD} bash -c "${CMD}"
281336
}
282337

283338
function enter_container() {
284339

285-
$CONTAINER_RUN_CMD "$I_SHELL"
340+
${CONTAINER_RUN_CMD} "${I_SHELL}"
286341

287342
}
288343

289344

290-
if [ "$ACTION" = "build" ]; then
345+
if [ "${ACTION}" = "build" ]; then
291346
echo "performing build action."
292347
install_deps_and_run_build
293348

294-
elif [ "$ACTION" = "enter" ]; then
349+
elif [ "${ACTION}" = "enter" ]; then
295350
echo "performing enter action."
296351
enter_container
297352
fi

0 commit comments

Comments
 (0)