From 3de08f23b49b62243778a60ca5496628a5aaa7dd Mon Sep 17 00:00:00 2001 From: Dalibor Kricka Date: Wed, 9 Jul 2025 15:37:02 +0200 Subject: [PATCH] cmd/run: --workdir command-line argument --- src/cmd/enter.go | 2 +- src/cmd/rootMigrationPath.go | 2 +- src/cmd/run.go | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/cmd/enter.go b/src/cmd/enter.go index 8602b744b..38bba2b2e 100644 --- a/src/cmd/enter.go +++ b/src/cmd/enter.go @@ -121,7 +121,7 @@ func enter(cmd *cobra.Command, args []string) error { command := []string{userShell, "-l"} - if err := runCommand(container, defaultContainer, image, release, 0, command, true, true, false); err != nil { + if err := runCommand(container, defaultContainer, image, release, 0, "", command, true, true, false); err != nil { return err } diff --git a/src/cmd/rootMigrationPath.go b/src/cmd/rootMigrationPath.go index 38b5d4956..f72733f58 100644 --- a/src/cmd/rootMigrationPath.go +++ b/src/cmd/rootMigrationPath.go @@ -68,7 +68,7 @@ func rootRunImpl(cmd *cobra.Command, args []string) error { command := []string{userShell, "-l"} - if err := runCommand(container, true, image, release, 0, command, true, true, false); err != nil { + if err := runCommand(container, true, image, release, 0, "", command, true, true, false); err != nil { return err } diff --git a/src/cmd/run.go b/src/cmd/run.go index 389ea1615..22cda2ead 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -56,6 +56,7 @@ var ( distro string preserveFDs uint release string + workDir string } runFallbackCommands = [][]string{{"/bin/bash", "-l"}} @@ -96,6 +97,12 @@ func init() { "", "Run command inside a Toolbx container for a different operating system release than the host") + flags.StringVarP(&runFlags.workDir, + "workdir", + "w", + "", + "Run command inside a Toolbx container within the given working directory.") + runCmd.SetHelpFunc(runHelp) if err := runCmd.RegisterFlagCompletionFunc("container", completionContainerNames); err != nil { @@ -156,6 +163,7 @@ func run(cmd *cobra.Command, args []string) error { image, release, runFlags.preserveFDs, + runFlags.workDir, command, false, false, @@ -170,6 +178,7 @@ func runCommand(container string, defaultContainer bool, image, release string, preserveFDs uint, + workDir string, command []string, emitEscapeSequence, fallbackToBash, pedantic bool) error { @@ -344,6 +353,7 @@ func runCommand(container string, environ := append(cdiEnviron, p11KitServerEnviron...) if err := runCommandWithFallbacks(container, preserveFDs, + workDir, command, environ, emitEscapeSequence, @@ -356,6 +366,7 @@ func runCommand(container string, func runCommandWithFallbacks(container string, preserveFDs uint, + workDir string, command, environ []string, emitEscapeSequence, fallbackToBash bool) error { @@ -391,7 +402,10 @@ func runCommandWithFallbacks(container string, runFallbackCommandsIndex := 0 runFallbackWorkDirsIndex := 0 - workDir := workingDirectory + + if workDir == "" { + workDir = workingDirectory + } for { execArgs := constructExecArgs(container,