Forward env vars to commands

We want to forward env vars to commands just because it's good practice,
and probably enables things that previously weren't possible.
This commit is contained in:
Jesse Duffield 2023-10-09 21:47:23 +11:00
parent d2fa5e9e9c
commit 966570ce6c
3 changed files with 13 additions and 12 deletions

View file

@ -100,7 +100,7 @@ func (c *Container) Attach() (*exec.Cmd, error) {
c.Log.Warn(fmt.Sprintf("attaching to container %s", c.Name))
// TODO: use SDK
cmd := c.OSCommand.PrepareSubProcess("docker", "attach", "--sig-proxy=false", c.ID)
cmd := c.OSCommand.NewCmd("docker", "attach", "--sig-proxy=false", c.ID)
return cmd, nil
}

View file

@ -87,7 +87,7 @@ func (c *OSCommand) RunExecutable(cmd *exec.Cmd) error {
// ExecutableFromString takes a string like `docker ps -a` and returns an executable command for it
func (c *OSCommand) ExecutableFromString(commandStr string) *exec.Cmd {
splitCmd := str.ToArgv(commandStr)
return c.command(splitCmd[0], splitCmd[1:]...)
return c.NewCmd(splitCmd[0], splitCmd[1:]...)
}
// Same as ExecutableFromString but cancellable via a context
@ -96,6 +96,12 @@ func (c *OSCommand) ExecutableFromStringContext(ctx context.Context, commandStr
return exec.CommandContext(ctx, splitCmd[0], splitCmd[1:]...)
}
func (c *OSCommand) NewCmd(cmdName string, commandArgs ...string) *exec.Cmd {
cmd := c.command(cmdName, commandArgs...)
cmd.Env = os.Environ()
return cmd
}
func (c *OSCommand) NewCommandStringWithShell(commandStr string) string {
var quotedCommand string
// Windows does not seem to like quotes around the command
@ -187,12 +193,7 @@ func (c *OSCommand) EditFile(filename string) (*exec.Cmd, error) {
return nil, errors.New("No editor defined in $VISUAL or $EDITOR")
}
return c.PrepareSubProcess(editor, filename), nil
}
// PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it
func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *exec.Cmd {
return c.command(cmdName, commandArgs...)
return c.NewCmd(editor, filename), nil
}
// Quote wraps a message in platform-specific quotation marks
@ -301,7 +302,7 @@ func (c *OSCommand) GetLazydockerPath() string {
// RunCustomCommand returns the pointer to a custom command
func (c *OSCommand) RunCustomCommand(command string) *exec.Cmd {
return c.PrepareSubProcess(c.Platform.shell, c.Platform.shellArg, command)
return c.NewCmd(c.Platform.shell, c.Platform.shellArg, command)
}
// PipeCommands runs a heap of commands and pipes their inputs/outputs together like A | B | C

View file

@ -90,7 +90,7 @@ func TestOSCommandEditFile(t *testing.T) {
assert.EqualValues(t, "nano", name)
return nil
return exec.Command("exit", "0")
},
func(env string) string {
if env == "VISUAL" {
@ -112,7 +112,7 @@ func TestOSCommandEditFile(t *testing.T) {
assert.EqualValues(t, "emacs", name)
return nil
return exec.Command("exit", "0")
},
func(env string) string {
if env == "EDITOR" {
@ -134,7 +134,7 @@ func TestOSCommandEditFile(t *testing.T) {
assert.EqualValues(t, "vi", name)
return nil
return exec.Command("exit", "0")
},
func(env string) string {
return ""