run-shell: Allow specifying the cwd

This commit is contained in:
Kovid Goyal
2023-09-24 10:40:21 +05:30
parent 38be3e98a1
commit 59e4c6660e
2 changed files with 11 additions and 2 deletions

View File

@@ -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
}

View File

@@ -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)
}