Skip to content

Commit 05f3530

Browse files
committed
Shorten the prefix for debug and error messages
Using "$0" leads to printing the entire path to the fedora-toolbox script, which adds visual noise to the output. Command line tools like GNU Coreutils and Git remove any leading directory components from the path, which seems like a reasonable trade-off between aesthetics and verbosity. #50
1 parent 5e4e63a commit 05f3530

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

fedora-toolbox

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#
1717

1818

19+
exec 42>/dev/null
20+
21+
base_toolbox_command=$(basename "$0" 2>&42)
22+
1923
source /etc/os-release
2024
release=$VERSION_ID
2125

@@ -70,7 +74,7 @@ spinner_start()
7074
fi
7175

7276
if ! touch "$directory/spinner-start" 2>&42; then
73-
echo "$0: unable to start spinner: spinner start file couldn't be created"
77+
echo "$base_toolbox_command: unable to start spinner: spinner start file couldn't be created"
7478
return 1
7579
fi
7680

@@ -98,7 +102,7 @@ spinner_start()
98102
tput cnorm 2>&42
99103

100104
if ! touch "$directory/spinner-stop" 2>&42; then
101-
echo "$0: unable to stop spinner: spinner stop file couldn't be created"
105+
echo "$base_toolbox_command: unable to stop spinner: spinner stop file couldn't be created"
102106
fi
103107
) &
104108

@@ -112,7 +116,7 @@ spinner_stop()
112116
directory="$1"
113117

114118
if ! rm "$directory/spinner-start" 2>&42; then
115-
echo "$0: unable to stop spinner: spinner start file couldn't be removed"
119+
echo "$base_toolbox_command: unable to stop spinner: spinner start file couldn't be removed"
116120
return
117121
fi
118122

@@ -138,7 +142,7 @@ configure_working_container()
138142
if ! $prefix_sudo buildah run $working_container_name -- \
139143
sh -c 'rmdir /home && mkdir -m 0755 -p /var/home && ln -s var/home /home' 2>&42; then
140144
$prefix_sudo buildah rm $working_container_name >/dev/null 2>&42
141-
echo "$0: failed to make /home a symlink"
145+
echo "$base_toolbox_command: failed to make /home a symlink"
142146
return 1
143147
fi
144148
fi
@@ -151,52 +155,52 @@ configure_working_container()
151155
--groups wheel \
152156
$USER \
153157
>/dev/null 2>&42; then
154-
echo "$0: failed to create user $USER with UID $UID"
158+
echo "$base_toolbox_command: failed to create user $USER with UID $UID"
155159
return 1
156160
fi
157161

158162
if ! $prefix_sudo buildah run $working_container_name -- passwd -d $USER >/dev/null 2>&42; then
159-
echo "$0: failed to remove password for user $USER"
163+
echo "$base_toolbox_command: failed to remove password for user $USER"
160164
return 1
161165
fi
162166

163167
if ! $prefix_sudo buildah run $working_container_name -- passwd -d root >/dev/null 2>&42; then
164-
echo "$0: failed to remove password for user root"
168+
echo "$base_toolbox_command: failed to remove password for user root"
165169
return 1
166170
fi
167171

168172
if ! $prefix_sudo buildah config --volume $HOME $working_container_name >/dev/null 2>&42; then
169-
echo "$0: failed to configure volume for $HOME"
173+
echo "$base_toolbox_command: failed to configure volume for $HOME"
170174
return 1
171175
fi
172176

173177
if ! $prefix_sudo buildah config --volume $XDG_RUNTIME_DIR $working_container_name >/dev/null 2>&42; then
174-
echo "$0: failed to configure volume for $XDG_RUNTIME_DIR"
178+
echo "$base_toolbox_command: failed to configure volume for $XDG_RUNTIME_DIR"
175179
return 1
176180
fi
177181

178182
if ! $prefix_sudo buildah config --volume $dbus_system_bus_path $working_container_name >/dev/null 2>&42; then
179-
echo "$0: failed to configure volume for $dbus_system_bus_path"
183+
echo "$base_toolbox_command: failed to configure volume for $dbus_system_bus_path"
180184
return 1
181185
fi
182186

183187
if ! $prefix_sudo buildah config --volume /dev/dri $working_container_name >/dev/null 2>&42; then
184-
echo "$0: failed to configure volume for /dev/dri"
188+
echo "$base_toolbox_command: failed to configure volume for /dev/dri"
185189
return 1
186190
fi
187191

188192
if ! $prefix_sudo buildah config --volume /dev/fuse $working_container_name >/dev/null 2>&42; then
189-
echo "$0: failed to configure volume for /dev/fuse"
193+
echo "$base_toolbox_command: failed to configure volume for /dev/fuse"
190194
return 1
191195
fi
192196

193197
if ! $prefix_sudo buildah config --user $USER $working_container_name >/dev/null 2>&42; then
194-
echo "$0: failed to configure the default user as $USER"
198+
echo "$base_toolbox_command: failed to configure the default user as $USER"
195199
return 1
196200
fi
197201

198202
if ! $prefix_sudo buildah config --workingdir $HOME $working_container_name >/dev/null 2>&42; then
199-
echo "$0: failed to configure the initial working directory to $HOME"
203+
echo "$base_toolbox_command: failed to configure the initial working directory to $HOME"
200204
return 1
201205
fi
202206

@@ -216,24 +220,24 @@ create()
216220
dbus_system_bus_path=$(echo $dbus_system_bus_address | cut --delimiter = --fields 2 2>&42)
217221
dbus_system_bus_path=$(readlink --canonicalize $dbus_system_bus_path 2>&42)
218222

219-
echo "$0: checking if image $toolbox_image already exists" >&42
223+
echo "$base_toolbox_command: checking if image $toolbox_image already exists" >&42
220224

221225
if ! $prefix_sudo buildah inspect --type image $toolbox_image >/dev/null 2>&42; then
222-
echo "$0: trying to create working container $working_container_name" >&42
223-
echo "$0: looking for image localhost/$base_toolbox_image" >&42
226+
echo "$base_toolbox_command: trying to create working container $working_container_name" >&42
227+
echo "$base_toolbox_command: looking for image localhost/$base_toolbox_image" >&42
224228

225229
if ! $prefix_sudo buildah from \
226230
--name $working_container_name \
227231
localhost/$base_toolbox_image >/dev/null 2>&42; then
228-
echo "$0: looking for image $registry/$fgc/$base_toolbox_image" >&42
232+
echo "$base_toolbox_command: looking for image $registry/$fgc/$base_toolbox_image" >&42
229233

230234
if spinner_directory=$(mktemp --directory --tmpdir fedora-toolbox-spinner-XXXXXXXXXX 2>&42); then
231-
spinner_message="$0: pulling from $registry: "
235+
spinner_message="$base_toolbox_command: pulling from $registry: "
232236
if ! spinner_start "$spinner_directory" "$spinner_message"; then
233237
spinner_directory=""
234238
fi
235239
else
236-
echo "$0: unable to start spinner: spinner directory not created"
240+
echo "$base_toolbox_command: unable to start spinner: spinner directory not created"
237241
spinner_directory=""
238242
fi
239243

@@ -247,20 +251,20 @@ create()
247251
fi
248252

249253
if [ $ret_val -ne 0 ]; then
250-
echo "$0: failed to create working container"
254+
echo "$base_toolbox_command: failed to create working container"
251255
exit 1
252256
fi
253257
fi
254258

255-
echo "$0: trying to configure working container $working_container_name" >&42
259+
echo "$base_toolbox_command: trying to configure working container $working_container_name" >&42
256260

257261
if spinner_directory=$(mktemp --directory --tmpdir fedora-toolbox-spinner-XXXXXXXXXX 2>&42); then
258-
spinner_message="$0: configuring working container: "
262+
spinner_message="$base_toolbox_command: configuring working container: "
259263
if ! spinner_start "$spinner_directory" "$spinner_message"; then
260264
spinner_directory=""
261265
fi
262266
else
263-
echo "$0: unable to start spinner: spinner directory not created"
267+
echo "$base_toolbox_command: unable to start spinner: spinner directory not created"
264268
spinner_directory=""
265269
fi
266270

@@ -276,15 +280,15 @@ create()
276280
exit 1
277281
fi
278282

279-
echo "$0: trying to create image $toolbox_image" >&42
283+
echo "$base_toolbox_command: trying to create image $toolbox_image" >&42
280284

281285
if spinner_directory=$(mktemp --directory --tmpdir fedora-toolbox-spinner-XXXXXXXXXX 2>&42); then
282-
spinner_message="$0: creating image $toolbox_image: "
286+
spinner_message="$base_toolbox_command: creating image $toolbox_image: "
283287
if ! spinner_start "$spinner_directory" "$spinner_message"; then
284288
spinner_directory=""
285289
fi
286290
else
287-
echo "$0: unable to start spinner: spinner directory not created"
291+
echo "$base_toolbox_command: unable to start spinner: spinner directory not created"
288292
spinner_directory=""
289293
fi
290294

@@ -297,17 +301,17 @@ create()
297301

298302
if [ $ret_val -ne 0 ]; then
299303
$prefix_sudo buildah rm $working_container_name >/dev/null 2>&42
300-
echo "$0: failed to create image $toolbox_image"
304+
echo "$base_toolbox_command: failed to create image $toolbox_image"
301305
exit 1
302306
fi
303307

304-
echo "$0: created image $toolbox_image" >&42
308+
echo "$base_toolbox_command: created image $toolbox_image" >&42
305309
fi
306310

307-
echo "$0: checking if container $toolbox_container already exists" >&42
311+
echo "$base_toolbox_command: checking if container $toolbox_container already exists" >&42
308312

309313
if $prefix_sudo podman inspect --type container $toolbox_container >/dev/null 2>&42; then
310-
echo "$0: container $toolbox_container already exists"
314+
echo "$base_toolbox_command: container $toolbox_container already exists"
311315
exit 1
312316
fi
313317

@@ -320,15 +324,15 @@ create()
320324
max_minus_uid=$((max_uid_count - UID))
321325
uid_plus_one=$((UID + 1))
322326

323-
echo "$0: trying to create container $toolbox_container" >&42
327+
echo "$base_toolbox_command: trying to create container $toolbox_container" >&42
324328

325329
if spinner_directory=$(mktemp --directory --tmpdir fedora-toolbox-spinner-XXXXXXXXXX 2>&42); then
326-
spinner_message="$0: creating container $toolbox_container: "
330+
spinner_message="$base_toolbox_command: creating container $toolbox_container: "
327331
if ! spinner_start "$spinner_directory" "$spinner_message"; then
328332
spinner_directory=""
329333
fi
330334
else
331-
echo "$0: unable to start spinner: spinner directory not created"
335+
echo "$base_toolbox_command: unable to start spinner: spinner directory not created"
332336
spinner_directory=""
333337
fi
334338

@@ -359,38 +363,38 @@ create()
359363
fi
360364

361365
if [ $ret_val -ne 0 ]; then
362-
echo "$0: failed to create container $toolbox_container"
366+
echo "$base_toolbox_command: failed to create container $toolbox_container"
363367
exit 1
364368
fi
365369

366-
echo "$0: created container $toolbox_container" >&42
370+
echo "$base_toolbox_command: created container $toolbox_container" >&42
367371
)
368372

369373

370374
enter()
371375
(
372376
shell_to_exec=/bin/bash
373377

374-
echo "$0: trying to start container $toolbox_container" >&42
378+
echo "$base_toolbox_command: trying to start container $toolbox_container" >&42
375379

376380
if ! $prefix_sudo podman start $toolbox_container >/dev/null 2>&42; then
377-
echo "$0: failed to start container $toolbox_container"
381+
echo "$base_toolbox_command: failed to start container $toolbox_container"
378382
exit 1
379383
fi
380384

381385
if [ "$DBUS_SYSTEM_BUS_ADDRESS" != "" ]; then
382386
set_dbus_system_bus_address="--env DBUS_SYSTEM_BUS_ADDRESS=$DBUS_SYSTEM_BUS_ADDRESS"
383387
fi
384388

385-
echo "$0: looking for $SHELL in container $toolbox_container" >&42
389+
echo "$base_toolbox_command: looking for $SHELL in container $toolbox_container" >&42
386390

387391
if $prefix_sudo podman exec $toolbox_container test -f $SHELL 2>&42; then
388392
shell_to_exec=$SHELL
389393
else
390-
echo "$0: $SHELL not found in $toolbox_container; using $shell_to_exec instead" >&42
394+
echo "$base_toolbox_command: $SHELL not found in $toolbox_container; using $shell_to_exec instead" >&42
391395
fi
392396

393-
echo "$0: trying to exec $shell_to_exec in container $toolbox_container" >&42
397+
echo "$base_toolbox_command: trying to exec $shell_to_exec in container $toolbox_container" >&42
394398

395399
$prefix_sudo podman exec \
396400
--env COLORTERM=$COLORTERM \
@@ -474,17 +478,17 @@ list_containers()
474478
exit_if_extra_operand()
475479
{
476480
if [ "$1" != "" ]; then
477-
echo "$0: extra operand '$1'"
478-
echo "Try '$0 --help' for more information."
481+
echo "$base_toolbox_command: extra operand '$1'"
482+
echo "Try '$base_toolbox_command --help' for more information."
479483
exit 1
480484
fi
481485
}
482486

483487

484488
exit_if_unrecognized_option()
485489
{
486-
echo "$0: unrecognized option '$1'"
487-
echo "Try '$0 --help' for more information."
490+
echo "$base_toolbox_command: unrecognized option '$1'"
491+
echo "Try '$base_toolbox_command --help' for more information."
488492
exit 1
489493
}
490494

@@ -506,15 +510,13 @@ usage()
506510
}
507511

508512

509-
exec 42>/dev/null
510-
511513
while has_prefix "$1" -; do
512514
case $1 in
513515
--container )
514516
shift
515517
if [ "$1" = "" ]; then
516-
echo "$0: missing argument for '--container'"
517-
echo "Try '$0 --help' for more information."
518+
echo "$base_toolbox_command: missing argument for '--container'"
519+
echo "Try '$base_toolbox_command --help' for more information."
518520
exit 1
519521
fi
520522
toolbox_container=$1
@@ -526,19 +528,19 @@ while has_prefix "$1" -; do
526528
--release )
527529
shift
528530
if [ "$1" = "" ]; then
529-
echo "$0: missing argument for '--release'"
530-
echo "Try '$0 --help' for more information."
531+
echo "$base_toolbox_command: missing argument for '--release'"
532+
echo "Try '$base_toolbox_command --help' for more information."
531533
exit 1
532534
fi
533535
arg=$(echo $1 | sed 's/^F\|^f//' 2>&42)
534536
if ! is_integer $arg; then
535-
echo "$0: invalid argument for '--release'"
536-
echo "Try '$0 --help' for more information."
537+
echo "$base_toolbox_command: invalid argument for '--release'"
538+
echo "Try '$base_toolbox_command --help' for more information."
537539
exit 1
538540
fi
539541
if [ $arg -le 0 2>&42 ]; then
540-
echo "$0: invalid argument for '--release'"
541-
echo "Try '$0 --help' for more information."
542+
echo "$base_toolbox_command: invalid argument for '--release'"
543+
echo "Try '$base_toolbox_command --help' for more information."
542544
exit 1
543545
fi
544546
release=$arg
@@ -562,8 +564,8 @@ base_toolbox_image="fedora-toolbox:$release"
562564
toolbox_image="fedora-toolbox-$USER:$release"
563565

564566
if [ "$1" = "" ]; then
565-
echo "$0: missing command"
566-
echo "Try '$0 --help' for more information."
567+
echo "$base_toolbox_command: missing command"
568+
echo "Try '$base_toolbox_command --help' for more information."
567569
exit 1
568570
fi
569571

@@ -580,8 +582,8 @@ case $op in
580582
--image )
581583
shift
582584
if [ "$1" = "" ]; then
583-
echo "$0: missing argument for '--image'"
584-
echo "Try '$0 --help' for more information."
585+
echo "$base_toolbox_command: missing argument for '--image'"
586+
echo "Try '$base_toolbox_command --help' for more information."
585587
exit 1
586588
fi
587589
toolbox_image=$1
@@ -635,7 +637,7 @@ case $op in
635637
exit
636638
;;
637639
* )
638-
echo "$0: unrecognized command '$op'"
639-
echo "Try '$0 --help' for more information."
640+
echo "$base_toolbox_command: unrecognized command '$op'"
641+
echo "Try '$base_toolbox_command --help' for more information."
640642
exit 1
641643
esac

0 commit comments

Comments
 (0)