From 59e4c6660e2be42896a8e215d22c32480da8d8e4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 24 Sep 2023 10:40:21 +0530 Subject: [PATCH] run-shell: Allow specifying the cwd --- tools/cmd/run_shell/main.go | 8 +++++++- tools/tui/run.go | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/cmd/run_shell/main.go b/tools/cmd/run_shell/main.go index a3a60a545..e70e0cdb3 100644 --- a/tools/cmd/run_shell/main.go +++ b/tools/cmd/run_shell/main.go @@ -19,6 +19,7 @@ type Options struct { Shell string ShellIntegration string Env []string + Cwd string } func main(args []string, opts *Options) (rc int, err error) { @@ -53,7 +54,7 @@ func main(args []string, opts *Options) (rc int, err error) { os.Setenv("TERMINFO_DIRS", terminfo_dir+existing) } } - err = tui.RunShell(tui.ResolveShell(opts.Shell), tui.ResolveShellIntegration(opts.ShellIntegration)) + err = tui.RunShell(tui.ResolveShell(opts.Shell), tui.ResolveShellIntegration(opts.ShellIntegration), opts.Cwd) if changed { os.Clearenv() for _, entry := range env_before { @@ -96,5 +97,10 @@ func EntryPoint(root *cli.Command) *cli.Command { Help: "Specify an env var to set before running the shell. Of the form KEY=VAL. Can be specified multiple times. If no = is present KEY is unset.", Type: "list", }) + sc.Add(cli.OptionSpec{ + Name: "--cwd", + Help: "The working directory to use when executing the shell.", + }) + return sc } diff --git a/tools/tui/run.go b/tools/tui/run.go index b9d00208a..a45c42c7e 100644 --- a/tools/tui/run.go +++ b/tools/tui/run.go @@ -144,7 +144,7 @@ func rc_modification_allowed(ksi string) bool { return ksi != "" } -func RunShell(shell_cmd []string, shell_integration_env_var_val string) (err error) { +func RunShell(shell_cmd []string, shell_integration_env_var_val, cwd string) (err error) { shell_name := get_shell_name(shell_cmd[0]) var shell_env map[string]string if rc_modification_allowed(shell_integration_env_var_val) && shell_integration.IsSupportedShell(shell_name) { @@ -178,6 +178,9 @@ func RunShell(shell_cmd []string, shell_integration_env_var_val string) (err err env = os.Environ() } // fmt.Println(fmt.Sprintf("%s %v\n%#v", utils.FindExe(exe), shell_cmd, env)) + if cwd != "" { + _ = os.Chdir(cwd) + } return unix.Exec(utils.FindExe(exe), shell_cmd, env) }