Skip to content

Commit 0424e15

Browse files
authored
enter: show container command on dry run (#2000)
This fix ensures that the printed command in dry-run mode is the same as the one that would be executed without the --dry-run flag. The container command, either default or custom, is always appended to the command in both normal and dry-run execution. The inspect command to fetch the container status is now executed only when not in dry-run mode.
1 parent 82ee78e commit 0424e15

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

distrobox-enter

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,59 @@ generate_enter_command()
542542
container_home="${HOME}"
543543
container_path="${PATH}"
544544
unshare_groups=0
545+
546+
################################################################################
547+
# In this section we will manipulate the positional parameters
548+
# in order to generate our long docker/podman/lilipod command to execute.
549+
#
550+
# We use positional parameters in order to have the shell manage escaping and spaces
551+
# so we remove the problem of we having to handle them.
552+
#
553+
# 1 - handle absence of custom command, we will need to add a getent command to
554+
# execute the right container's user's shell
555+
# 2 - in case of unshared groups (or initful) we need to trigger a proper login
556+
# using `su`, so we will need to manipulate these arguments accorodingly
557+
# 3 - prepend our generated command
558+
# to do this, we use `tac` so we reverse loop it and prepend each argument.
559+
# 4 - now that we're done, we can prepend our container_command
560+
# we will need to use `rev` to reverse it as we reverse loop and prepend each
561+
# argument
562+
################################################################################
563+
#
564+
# Setup default commands if none are specified
565+
# execute a getent command using the /bin/sh shell
566+
# to find out the default shell of the user, and
567+
# do a login shell with it (eg: /bin/bash -l)
568+
if [ "${container_custom_command}" -eq 0 ]; then
569+
set - "$@" "/bin/sh" "-c" "\$(getent passwd '${container_command_user}' | cut -f 7 -d :) -l"
570+
fi
571+
572+
# If we have a command and we're unsharing groups, we need to execute those
573+
# command using su $container_command_user
574+
# if we're in a tty, also allocate one
575+
if [ "${unshare_groups:-0}" -eq 1 ]; then
576+
# shellcheck disable=SC2089,SC2016
577+
set -- "-c" '"$0" "$@"' -- "$@"
578+
set -- "-s" "/bin/sh" "$@"
579+
if [ "${headless}" -eq 0 ]; then
580+
set -- "--pty" "$@"
581+
fi
582+
set -- "-m" "$@"
583+
set -- "${container_command_user}" "$@"
584+
set -- "su" "$@"
585+
fi
586+
587+
################################################################################
588+
# Execution section
589+
################################################################################
590+
591+
# dry run mode, just generate the command and print it. No execution.
592+
if [ "${dryrun}" -ne 0 ]; then
593+
cmd="$(generate_enter_command | sed 's/\t//g')"
594+
printf "%s %s\n" "${cmd}" "$*"
595+
exit 0
596+
fi
597+
545598
# Now inspect the container we're working with.
546599
container_status="unknown"
547600
eval "$(${container_manager} inspect --type container --format \
@@ -551,13 +604,6 @@ eval "$(${container_manager} inspect --type container --format \
551604
{{range .Config.Env}}{{if and (ge (len .) 5) (eq (slice . 0 5) "PATH=")}}container_path={{slice . 5 | printf "%q"}}{{end}}{{end}}' \
552605
"${container_name}")"
553606

554-
# dry run mode, just generate the command and print it. No execution.
555-
if [ "${dryrun}" -ne 0 ]; then
556-
cmd="$(generate_enter_command | sed 's/\t//g')"
557-
printf "%s %s\n" "${cmd}" "$*"
558-
exit 0
559-
fi
560-
561607
# Check if the container is even there
562608
if [ "${container_status}" = "unknown" ]; then
563609
# If not, prompt to create it first
@@ -669,47 +715,6 @@ if [ "${container_status}" != "running" ]; then
669715
printf >&2 "\nContainer Setup Complete!\n"
670716
fi
671717

672-
################################################################################
673-
# Execution section, in this section we will manipulate the positional parameters
674-
# in order to generate our long docker/podman/lilipod command to execute.
675-
#
676-
# We use positional parameters in order to have the shell manage escaping and spaces
677-
# so we remove the problem of we having to handle them.
678-
#
679-
# 1 - handle absence of custom command, we will need to add a getent command to
680-
# execute the right container's user's shell
681-
# 2 - in case of unshared groups (or initful) we need to trigger a proper login
682-
# using `su`, so we will need to manipulate these arguments accorodingly
683-
# 3 - prepend our generated command
684-
# to do this, we use `tac` so we reverse loop it and prepend each argument.
685-
# 4 - now that we're done, we can prepend our container_command
686-
# we will need to use `rev` to reverse it as we reverse loop and prepend each
687-
# argument
688-
################################################################################
689-
#
690-
# Setup default commands if none are specified
691-
# execute a getent command using the /bin/sh shell
692-
# to find out the default shell of the user, and
693-
# do a login shell with it (eg: /bin/bash -l)
694-
if [ "${container_custom_command}" -eq 0 ]; then
695-
set - "$@" "/bin/sh" "-c" "\$(getent passwd '${container_command_user}' | cut -f 7 -d :) -l"
696-
fi
697-
698-
# If we have a command and we're unsharing groups, we need to execute those
699-
# command using su $container_command_user
700-
# if we're in a tty, also allocate one
701-
if [ "${unshare_groups:-0}" -eq 1 ]; then
702-
# shellcheck disable=SC2089,SC2016
703-
set -- "-c" '"$0" "$@"' -- "$@"
704-
set -- "-s" "/bin/sh" "$@"
705-
if [ "${headless}" -eq 0 ]; then
706-
set -- "--pty" "$@"
707-
fi
708-
set -- "-m" "$@"
709-
set -- "${container_command_user}" "$@"
710-
set -- "su" "$@"
711-
fi
712-
713718
# Generate the exec command and run it
714719
cmd="$(generate_enter_command | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--]}')"
715720
# Reverse it so we can reverse loop and prepend the command's arguments

0 commit comments

Comments
 (0)