Skip to content

Commit ef4ace8

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

1 file changed

Lines changed: 152 additions & 89 deletions

File tree

cli/builbo

Lines changed: 152 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,41 @@
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=""
3033
# shell for interactive use:
31-
I_SHELL="bash"
34+
DEFAULT_I_SHELL="bash"
35+
I_SHELL="${DEFAULT_I_SHELL}"
3236

3337

3438
detect_container_command
3539

36-
if [ -z "$CONTAINER_CMD" ]; then
40+
if [ -z "${CONTAINER_CMD}" ]; then
3741
echo "error: failed to detect container command (podman or docker)."
3842
exit 1
3943
fi
@@ -43,17 +47,17 @@ function usage() {
4347
4448
builbo: build software projects in buildbox containers.
4549
46-
USAGE: $0 options
50+
USAGE: builbo options
4751
4852
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
53+
[ -l | --lang (c|latex) ] - compilation language (default: ${DEFAULT_LANG})
54+
[ -r | --registry (quay.io|... ] - container registry (default: ${DEFAULT_REGISTRY})
55+
[ -c | --container-cmd (podman|docker) ] - default: ${CONTAINER_CMD} (auto-detected)
56+
[ -o | --os (fedora|debian|ubuntu|rocky|suse) ] - linux distro (default: ${DEFAULT_OS})
57+
[ -n | --registry-namespace (...) ] - default: ${DEFAULT_NAMESPACE}
5458
[ -s | --build-script (command|path) ] - command or local script (in the CWD) for building the project.
5559
[ -d | --deps (pkg,pkg,pkg,...) ] - additional packages to install (comma-separated)
56-
-i | --shell (bash) ] - sinteractive shell (default: $I_SHELL)
60+
[ -i | --shell (bash) ] - interactive shell (default: ${DEFAULT_I_SHELL})
5761
[ -h | --help ] - action help: print this usage information
5862
[-t | --test ] - action test: print some diagnostic info for testing this program
5963
[ -b | --build ] - action build: perform a build in the CWD
@@ -78,15 +82,18 @@ function set_action() {
7882

7983
local action="$1"
8084

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

9198
}
9299

@@ -98,54 +105,100 @@ optstring="l:r:c:o:n:hbtes:d:"
98105
optstring_long="lang:,registry:,container-cmd:,help,test,build,enter,build-script:,deps:"
99106

100107

101-
parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" -- "$@")
102-
args_valid=$?
108+
parsed_args=$(getopt -n "builbo" -o "$optstring" -l "$optstring_long" "$@")
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+
shift 2
145+
;;
146+
-n | --registry-namespace)
147+
NAMESPACE="$2"
148+
echo "namespace '${NAMESPACE}' specified."
149+
shift 2
150+
;;
151+
-s | --build-script)
152+
BUILD_CMD="$2"
153+
echo "build command '${BUILD_CMD}' specified."
154+
shift 2
155+
;;
156+
-d | --deps)
157+
DEPS="$2"
158+
echo "dependencies '${DEPS}' specified."
159+
shift 2
160+
;;
161+
-h | --help)
162+
set_action "help"
163+
echo "action help specified."
164+
shift
165+
;;
166+
-b| --build)
167+
set_action "build"
168+
echo "action build specified."
169+
shift
170+
;;
171+
-t| --test)
172+
set_action "test"
173+
echo "action test specified."
174+
shift
175+
;;
176+
-e|--enter)
177+
set_action "enter"
178+
echo "action enter specified."
179+
shift
180+
;;
127181

128182

129183
# -- 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
184+
--)
185+
shift
186+
break
135187
;;
136-
esac
188+
*)
189+
echo "Unexpected option $1. - this should not happen."
190+
# this should have been caught above with args_valid
191+
usage
192+
exit 1
193+
;;
194+
esac
137195
done
196+
echo "done processing arguments."
138197

139-
# done processing args
140-
141-
142-
143-
144-
198+
#post-processing args
145199

146200

147201

148-
#post-processing args
149202

150203

151204
if [ -z "$CONTAINER_CMD" ]; then
@@ -155,10 +208,10 @@ if [ -z "$CONTAINER_CMD" ]; then
155208
fi
156209

157210
DEPS_PKGS="${DEPS//,/ }"
158-
IMAGE="$REGISTRY/$NAMESPACE/buildbox/$OS-$LANG:latest"
211+
IMAGE="${REGISTRY}/${NAMESPACE}/buildbox/${OS}-${LANG}:latest"
159212

160213

161-
case "$OS" in
214+
case "${OS}" in
162215

163216
fedora)
164217
PKG_CMD="dnf install -y"
@@ -177,28 +230,38 @@ esac
177230

178231

179232
# setting CONTAINER_RUN_CMD assumes CONTAINER_RUN_CMD is set.
180-
if [ -z "$CONTAINER_CMD" ]; then
233+
if [ -z "${CONTAINER_CMD}" ]; then
181234
echo "error: container command not set. this should not happen."
182235
exit 1
183236
fi
184237
if [ "$CONTAINER_CMD" = "podman" ]; then
185238

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

190243
fi
191244

192-
if [ -z "$ACTION" ]; then
193-
echo "error: no action selected. Please use exactly one of -t, -h, and -b."
245+
if [ -n "${SELECTED_ACTION}" ]; then
246+
if [ -n "${SELECTED_ACTION}" ]; then
247+
echo "Error: multiple actions specified on cmdline but only one is allowed."
248+
usage
249+
exit 1
250+
fi
251+
echo "Error: multiple actions specified on cmdline but only one is allowed."
252+
usage
253+
exit 1
254+
fi
255+
if [ -z "${SELECTED_ACTION}" ]; then
256+
echo "error: no action elected. Please use exactly one of -t, -h, -e, and -b."
194257
usage
195258
exit 1
196259
fi
197260

198-
if [ "$ACTION" = "build" ]; then
261+
if [ "${ACTION}" = "build" ]; then
199262

200263

201-
if [ -z "$BUILD_CMD" ]; then
264+
if [ -z "${BUILD_CMD}" ]; then
202265

203266
echo "error: action 'build' requires --build-script (-s)"
204267
usage
@@ -214,18 +277,18 @@ fi
214277
function print_settings() {
215278
echo "settings:"
216279
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"
280+
echo "LANG: ${LANG}"
281+
echo "OS: ${OS}"
282+
echo "REGISTRY: ${REGISTRY}"
283+
echo "NAMESPACE: ${NAMESPACE}"
284+
echo "BUILD_CMD: ${BUILD_CMD}"
285+
echo "DEPS: ${DEPS}"
286+
echo "DEPS_PKGS: ${DEPS_PKGS}"
287+
echo "PKG_CMD: ${PKG_CMD}"
288+
echo "CONTAINER_CMD: ${CONTAINER_CMD}"
289+
echo "CONTAINER_RUN_CMD: '${CONTAINER_RUN_CMD}'"
290+
echo "interactive shell: '${I_SHELL}'"
291+
echo "IMAGE: ${IMAGE}"
229292

230293
}
231294

@@ -235,7 +298,7 @@ if [ "$ACTION" = "test" ]; then
235298
exit 0
236299
fi
237300

238-
if [ "$ACTION" = "help" ]; then
301+
if [ "${ACTION}" = "help" ]; then
239302

240303
echo "performing help action."
241304

@@ -259,8 +322,8 @@ fi
259322

260323

261324
function install_deps() {
262-
if [ -n "$DEPS" ]; then
263-
$CONTAINER_RUN_CMD bash -c "$PKG_CMD $DEPS_PKGS"
325+
if [ -n "${DEPS}" ]; then
326+
${CONTAINER_RUN_CMD} bash -c "${PKG_CMD} ${DEPS_PKGS}"
264327

265328
fi
266329

@@ -270,28 +333,28 @@ function install_deps_and_run_build() {
270333

271334

272335

273-
local CMD="$BUILD_CMD"
336+
local CMD="${BUILD_CMD}"
274337

275-
if [ -n "$DEPS" ]; then
276-
CMD="$PKG_CMD $DEPS_PKGS && $BUILD_CMD"
338+
if [ -n "${DEPS}" ]; then
339+
CMD="${PKG_CMD} ${DEPS_PKGS} && ${BUILD_CMD}"
277340

278341
fi
279342

280-
$CONTAINER_RUN_CMD bash -c "$CMD"
343+
${CONTAINER_RUN_CMD} bash -c "${CMD}"
281344
}
282345

283346
function enter_container() {
284347

285-
$CONTAINER_RUN_CMD "$I_SHELL"
348+
${CONTAINER_RUN_CMD} "${I_SHELL}"
286349

287350
}
288351

289352

290-
if [ "$ACTION" = "build" ]; then
353+
if [ "${ACTION}" = "build" ]; then
291354
echo "performing build action."
292355
install_deps_and_run_build
293356

294-
elif [ "$ACTION" = "enter" ]; then
357+
elif [ "${ACTION}" = "enter" ]; then
295358
echo "performing enter action."
296359
enter_container
297360
fi

0 commit comments

Comments
 (0)