Skip to content

Commit bcc309b

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

1 file changed

Lines changed: 153 additions & 89 deletions

File tree

cli/builbo

Lines changed: 153 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,18 @@ 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+
96+
ACTION="${action}"
97+
SELECTED_ACTION="${action}"
9098

9199
}
92100

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

100108

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

104-
if [ "$args_valid" != "0" ]; then
112+
if [ "$args_are_valid" != "0" ]; then
105113
echo "error:invalid args."
106114
usage
107115
exit 1
108116
fi
109117

110118
# processing parsed args
111119

112-
eval set -- "$parsed_args"
120+
echo " processing command line arguments.."
121+
eval set -- "${parsed_args}"
113122
while :
114123
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 ;;
124+
arg="$1"
125+
echo "processing argument '${arg}'."
126+
case "${arg}" in
127+
-l | --lang)
128+
LANG="$2"
129+
echo "language '${LANG}' specified."
130+
shift 2
131+
;;
132+
-r | --registry)
133+
REGISTRY="$2"
134+
echo "registry '${REGISTRY}' specified."
135+
136+
shift 2
137+
;;
138+
-c | --container-cmd)
139+
CONTAINER_CMD="$2"
140+
echo "container command '${CONTAINER_CMD}' specified."
141+
shift 2
142+
;;
143+
-o | --os)
144+
OS="$2"
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 commans '${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+
set_action "build specified."
169+
echo
170+
shift
171+
;;
172+
-t| --test)
173+
set_action "test"
174+
echo "action test specified."
175+
shift
176+
;;
177+
-e|--enter)
178+
set_action "enter"
179+
echo "action enter specified."
180+
shift
181+
;;
127182

128183

129184
# -- 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
185+
--)
186+
shift
187+
break
135188
;;
136-
esac
189+
*)
190+
echo "Unexpected option $1. - this should not happen."
191+
# this should have been caught above with args_valid
192+
usage
193+
exit 1
194+
;;
195+
esac
137196
done
197+
echo "done processing arguments."
138198

139-
# done processing args
140-
141-
142-
143-
144-
199+
#post-processing args
145200

146201

147202

148-
#post-processing args
149203

150204

151205
if [ -z "$CONTAINER_CMD" ]; then
@@ -155,10 +209,10 @@ if [ -z "$CONTAINER_CMD" ]; then
155209
fi
156210

157211
DEPS_PKGS="${DEPS//,/ }"
158-
IMAGE="$REGISTRY/$NAMESPACE/buildbox/$OS-$LANG:latest"
212+
IMAGE="${REGISTRY}/${NAMESPACE}/buildbox/${OS}-${LANG}:latest"
159213

160214

161-
case "$OS" in
215+
case "${OS}" in
162216

163217
fedora)
164218
PKG_CMD="dnf install -y"
@@ -177,28 +231,38 @@ esac
177231

178232

179233
# setting CONTAINER_RUN_CMD assumes CONTAINER_RUN_CMD is set.
180-
if [ -z "$CONTAINER_CMD" ]; then
234+
if [ -z "${CONTAINER_CMD}" ]; then
181235
echo "error: container command not set. this should not happen."
182236
exit 1
183237
fi
184238
if [ "$CONTAINER_CMD" = "podman" ]; then
185239

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

190244
fi
191245

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

198-
if [ "$ACTION" = "build" ]; then
262+
if [ "${ACTION}" = "build" ]; then
199263

200264

201-
if [ -z "$BUILD_CMD" ]; then
265+
if [ -z "${BUILD_CMD}" ]; then
202266

203267
echo "error: action 'build' requires --build-script (-s)"
204268
usage
@@ -214,18 +278,18 @@ fi
214278
function print_settings() {
215279
echo "settings:"
216280
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"
281+
echo "LANG: ${LANG}"
282+
echo "OS: ${OS}"
283+
echo "REGISTRY: ${REGISTRY}"
284+
echo "NAMESPACE: ${NAMESPACE}"
285+
echo "BUILD_CMD: ${BUILD_CMD}"
286+
echo "DEPS: ${DEPS}"
287+
echo "DEPS_PKGS: ${DEPS_PKGS}"
288+
echo "PKG_CMD: ${PKG_CMD}"
289+
echo "CONTAINER_CMD: ${CONTAINER_CMD}"
290+
echo "CONTAINER_RUN_CMD: '${CONTAINER_RUN_CMD}'"
291+
echo "interactive shell: '${I_SHELL}'"
292+
echo "IMAGE: ${IMAGE}"
229293

230294
}
231295

@@ -235,7 +299,7 @@ if [ "$ACTION" = "test" ]; then
235299
exit 0
236300
fi
237301

238-
if [ "$ACTION" = "help" ]; then
302+
if [ "${ACTION}" = "help" ]; then
239303

240304
echo "performing help action."
241305

@@ -259,8 +323,8 @@ fi
259323

260324

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

265329
fi
266330

@@ -270,28 +334,28 @@ function install_deps_and_run_build() {
270334

271335

272336

273-
local CMD="$BUILD_CMD"
337+
local CMD="${BUILD_CMD}"
274338

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

278342
fi
279343

280-
$CONTAINER_RUN_CMD bash -c "$CMD"
344+
${CONTAINER_RUN_CMD} bash -c "${CMD}"
281345
}
282346

283347
function enter_container() {
284348

285-
$CONTAINER_RUN_CMD "$I_SHELL"
349+
${CONTAINER_RUN_CMD} "${I_SHELL}"
286350

287351
}
288352

289353

290-
if [ "$ACTION" = "build" ]; then
354+
if [ "${ACTION}" = "build" ]; then
291355
echo "performing build action."
292356
install_deps_and_run_build
293357

294-
elif [ "$ACTION" = "enter" ]; then
358+
elif [ "${ACTION}" = "enter" ]; then
295359
echo "performing enter action."
296360
enter_container
297361
fi

0 commit comments

Comments
 (0)