Skip to content

Commit

Permalink
Forward env vars to commands
Browse files Browse the repository at this point in the history
We want to forward env vars to commands just because it's good practice,
and probably enables things that previously weren't possible.
  • Loading branch information
jesseduffield committed Oct 9, 2023
1 parent d2fa5e9 commit 966570c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/commands/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand All @@ -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 ""
Expand Down

0 comments on commit 966570c

Please sign in to comment.