diff --git a/cmd/cmd/commands/exec.go b/cmd/cmd/commands/exec.go index 892decd..d1c286d 100644 --- a/cmd/cmd/commands/exec.go +++ b/cmd/cmd/commands/exec.go @@ -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" @@ -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"), diff --git a/command.go b/command.go index 776b56f..aeb0268 100644 --- a/command.go +++ b/command.go @@ -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 diff --git a/config/config.go b/config/config.go index dbf9728..fbf6a60 100644 --- a/config/config.go +++ b/config/config.go @@ -25,7 +25,8 @@ type Config struct { ReadOnly bool // engine = host - IsHistoryDisabled bool + IsHistoryDisabled bool + IsInheritEnvironmentEnabled bool // engine = docker Image string diff --git a/engine/host/config.go b/engine/host/config.go index c834742..8968ef2 100644 --- a/engine/host/config.go +++ b/engine/host/config.go @@ -12,6 +12,8 @@ type Config struct { // IsHistoryDisabled bool + // + IsInheritEnvironmentEnabled bool // Custom Command Runner ID ID string diff --git a/engine/host/create.go b/engine/host/create.go index 283dd7c..550a85e 100644 --- a/engine/host/create.go +++ b/engine/host/create.go @@ -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 } @@ -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))