Skip to content

Commit

Permalink
cmd/run, sh: Show an error if $PWD is missing inside the container
Browse files Browse the repository at this point in the history
It might happen that the current working directory in the present
environment is missing inside the container that's going to be used.
So far, Toolbox would fail silently, which wasn't very obvious.

Some changes by Debarshi Ray and Harry Míchal.

#369
  • Loading branch information
bureado authored and debarshiray committed Jun 23, 2020
1 parent 1daa158 commit 89ff98d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,11 @@ func runCommand(container string,
case 126:
err = fmt.Errorf("failed to invoke command %s in container %s", command[0], container)
case 127:
err = fmt.Errorf("command %s not found in container %s", command[0], container)
if pathPresent, _ := isPathPresent(container, workingDirectory); !pathPresent {
err = fmt.Errorf("directory %s not found in container %s", workingDirectory, container)
} else {
err = fmt.Errorf("command %s not found in container %s", command[0], container)
}
default:
err = nil
}
Expand Down Expand Up @@ -398,6 +402,25 @@ func isCommandPresent(container, command string) (bool, error) {
return true, nil
}

func isPathPresent(container, path string) (bool, error) {
logrus.Debugf("Looking for path %s in container %s", path, container)

logLevelString := podman.LogLevel.String()
args := []string{
"--log-level", logLevelString,
"exec",
"--user", currentUser.Username,
container,
"sh", "-c", "test -d \"$1\"", "sh", path,
}

if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
return false, err
}

return true, nil
}

func startContainer(container string) error {
var stderr strings.Builder
if err := podman.Start(container, &stderr); err == nil {
Expand Down
10 changes: 10 additions & 0 deletions toolbox
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,16 @@ run()

$emit_escape_sequence && printf "\033]777;container;pop;;\033\\"

if [ "$ret_val" -eq 127 ] 2>&3; then
# shellcheck disable=SC2016
if ! $podman_command exec \
--user "$USER" \
"$toolbox_container" \
sh -c 'test -d "$1"' sh "$PWD" >/dev/null 2>&3; then
echo "$base_toolbox_command: directory '$PWD' not found in container $toolbox_container" >&2
fi
fi

exit "$ret_val"
)

Expand Down

0 comments on commit 89ff98d

Please sign in to comment.