Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmd/cmd/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"syscall"
"time"

"github.com/go-zoox/core-utils/strings"

"github.com/eiannone/keyboard"
"github.com/go-zoox/cli"
"github.com/go-zoox/command"
Expand Down Expand Up @@ -156,13 +158,24 @@ func Exec(app *cli.MultipleProgram) {
Usage: "SSH know hosts file path",
EnvVars: []string{"SSH_KNOW_HOSTS_FILE_PATH"},
},
&cli.StringSliceFlag{
Name: "env",
Usage: `Set environment variables for the command`,
},
},
Action: func(ctx *cli.Context) (err error) {
environment := map[string]string{}
for _, e := range ctx.StringSlice("env") {
kv := strings.SplitN(e, "=", 2)
environment[kv[0]] = kv[1]
}

cmd, err := command.New(&command.Config{
Agent: ctx.String("agent"),
//
Engine: ctx.String("engine"),
Command: ctx.String("command"),
Environment: environment,
WorkDir: ctx.String("workdir"),
User: ctx.String("user"),
Shell: ctx.String("shell"),
Expand Down
2 changes: 2 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ func New(cfg *Config) (cmd Command, err error) {
ReadOnly: cfg.ReadOnly,
//
IsHistoryDisabled: cfg.IsHistoryDisabled,
//
IsInheritEnvironmentEnabled: cfg.IsInheritEnvironmentEnabled,
})
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type Config struct {
ReadOnly bool

// engine = host
IsHistoryDisabled bool
IsHistoryDisabled bool
IsInheritEnvironmentEnabled bool

// engine = docker
Image string
Expand Down
2 changes: 2 additions & 0 deletions engine/host/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Config struct {

//
IsHistoryDisabled bool
//
IsInheritEnvironmentEnabled bool

// Custom Command Runner ID
ID string
Expand Down
9 changes: 6 additions & 3 deletions engine/host/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (h *host) create() error {
logger.Debugf("create command: %s %v", h.cfg.Shell, args)
h.cmd = exec.Command(h.cfg.Shell, args...)

if err := applyEnv(h.cmd, h.cfg.Environment); err != nil {
if err := applyEnv(h.cmd, h.cfg.Environment, h.cfg.IsInheritEnvironmentEnabled); err != nil {
return err
}

Expand All @@ -45,8 +45,11 @@ func (h *host) create() error {
return nil
}

func applyEnv(cmd *exec.Cmd, environment map[string]string) error {
cmd.Env = append(os.Environ(), "TERM=xterm")
func applyEnv(cmd *exec.Cmd, environment map[string]string, IsInheritEnvironmentEnabled bool) error {
cmd.Env = append([]string{}, "TERM=xterm")
if IsInheritEnvironmentEnabled {
cmd.Env = append(cmd.Env, os.Environ()...)
}

for k, v := range environment {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
Expand Down