Skip to content

Commit

Permalink
feat: add env variable handling for subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
AzraelSec committed Sep 11, 2023
1 parent 8d710f1 commit c0d8a4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/shell/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package shell
import "strings"

func IgnoreInterrupt(e error) error {
if e == nil {
return e
}
if strings.Contains(e.Error(), "signal: interrupt") {
return nil
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/shell/sync_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package shell

import (
"context"
"fmt"
"io"
"os"
"os/exec"
)

Expand All @@ -17,7 +19,7 @@ type syncShell struct {
type ShellOps struct {
ShellPath string
ExecPath string
Env []string
Env map[string]string
Cmd string
Ctx context.Context
}
Expand All @@ -41,13 +43,17 @@ func NewSyncShell(ops ShellOps) *syncShell {
process.Dir = ops.ExecPath
}
if len(ops.Env) != 0 {
process.Env = ops.Env
vars := os.Environ()
for key, val := range ops.Env {
vars = append(vars, fmt.Sprintf("%s=%s", key, val))
}
process.Env = vars
}

return &syncShell{
ctx: &ops.Ctx,
process: process,
Pid: -1,
Pid: -1,
}
}

Expand All @@ -62,5 +68,5 @@ func (ss *syncShell) Start(w io.Writer) (int, error) {
}
ss.Pid = ss.process.Process.Pid
error = ss.process.Wait()
return ss.process.ProcessState.ExitCode(), error
return ss.process.ProcessState.ExitCode(), IgnoreInterrupt(error)
}

0 comments on commit c0d8a4f

Please # to comment.