diff options
Diffstat (limited to 'pkg/commands/oscommands')
-rw-r--r-- | pkg/commands/oscommands/cmd_obj.go | 33 | ||||
-rw-r--r-- | pkg/commands/oscommands/exec_live_default.go | 9 | ||||
-rw-r--r-- | pkg/commands/oscommands/exec_live_win.go | 4 | ||||
-rw-r--r-- | pkg/commands/oscommands/os.go | 35 |
4 files changed, 70 insertions, 11 deletions
diff --git a/pkg/commands/oscommands/cmd_obj.go b/pkg/commands/oscommands/cmd_obj.go new file mode 100644 index 000000000..592a4a61d --- /dev/null +++ b/pkg/commands/oscommands/cmd_obj.go @@ -0,0 +1,33 @@ +package oscommands + +import ( + "os/exec" +) + +// A command object is a general way to represent a command to be run on the +// command line. If you want to log the command you'll use .ToString() and +// if you want to run it you'll use .GetCmd() +type ICmdObj interface { + GetCmd() *exec.Cmd + ToString() string + AddEnvVars(...string) ICmdObj +} + +type CmdObj struct { + cmdStr string + cmd *exec.Cmd +} + +func (self *CmdObj) GetCmd() *exec.Cmd { + return self.cmd +} + +func (self *CmdObj) ToString() string { + return self.cmdStr +} + +func (self *CmdObj) AddEnvVars(vars ...string) ICmdObj { + self.cmd.Env = append(self.cmd.Env, vars...) + + return self +} diff --git a/pkg/commands/oscommands/exec_live_default.go b/pkg/commands/oscommands/exec_live_default.go index f0c000c1e..95d5d09fb 100644 --- a/pkg/commands/oscommands/exec_live_default.go +++ b/pkg/commands/oscommands/exec_live_default.go @@ -19,11 +19,10 @@ import ( // Output is a function that executes by every word that gets read by bufio // As return of output you need to give a string that will be written to stdin // NOTE: If the return data is empty it won't written anything to stdin -func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error { - c.Log.WithField("command", command).Info("RunCommand") - c.LogCommand(command, true) - cmd := c.ExecutableFromString(command) - cmd.Env = append(cmd.Env, "LANG=en_US.UTF-8", "LC_ALL=en_US.UTF-8") +func RunCommandWithOutputLiveWrapper(c *OSCommand, cmdObj ICmdObj, output func(string) string) error { + c.Log.WithField("command", cmdObj.ToString()).Info("RunCommand") + c.LogCommand(cmdObj.ToString(), true) + cmd := cmdObj.AddEnvVars("LANG=en_US.UTF-8", "LC_ALL=en_US.UTF-8").GetCmd() var stderr bytes.Buffer cmd.Stderr = &stderr diff --git a/pkg/commands/oscommands/exec_live_win.go b/pkg/commands/oscommands/exec_live_win.go index 83a40fe74..aa17242e3 100644 --- a/pkg/commands/oscommands/exec_live_win.go +++ b/pkg/commands/oscommands/exec_live_win.go @@ -5,6 +5,6 @@ package oscommands // RunCommandWithOutputLiveWrapper runs a command live but because of windows compatibility this command can't be ran there // TODO: Remove this hack and replace it with a proper way to run commands live on windows -func RunCommandWithOutputLiveWrapper(c *OSCommand, command string, output func(string) string) error { - return c.RunCommand(command) +func RunCommandWithOutputLiveWrapper(c *OSCommand, cmdObj ICmdObj, output func(string) string) error { + return c.RunCommand(cmdObj.ToString()) } diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 4babbe8a3..f0c9525a5 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -218,16 +218,16 @@ func (c *OSCommand) ShellCommandFromString(commandStr string) *exec.Cmd { } // RunCommandWithOutputLive runs RunCommandWithOutputLiveWrapper -func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string) string) error { - return RunCommandWithOutputLiveWrapper(c, command, output) +func (c *OSCommand) RunCommandWithOutputLive(cmdObj ICmdObj, output func(string) string) error { + return RunCommandWithOutputLiveWrapper(c, cmdObj, output) } // DetectUnamePass detect a username / password / passphrase question in a command // promptUserForCredential is a function that gets executed when this function detect you need to fillin a password or passphrase // The promptUserForCredential argument will be "username", "password" or "passphrase" and expects the user's password/passphrase or username back -func (c *OSCommand) DetectUnamePass(command string, promptUserForCredential func(string) string) error { +func (c *OSCommand) DetectUnamePass(cmdObj ICmdObj, promptUserForCredential func(string) string) error { ttyText := "" - errMessage := c.RunCommandWithOutputLive(command, func(word string) string { + errMessage := c.RunCommandWithOutputLive(cmdObj, func(word string) string { ttyText = ttyText + " " + word prompts := map[string]string{ @@ -563,3 +563,30 @@ func (c *OSCommand) RemoveFile(path string) error { return c.removeFile(path) } + +func (c *OSCommand) NewCmdObjFromStr(cmdStr string) ICmdObj { + args := str.ToArgv(cmdStr) + cmd := c.Command(args[0], args[1:]...) + cmd.Env = os.Environ() + + return &CmdObj{ + cmdStr: cmdStr, + cmd: cmd, + } +} + +func (c *OSCommand) NewCmdObjFromArgs(args []string) ICmdObj { + cmd := c.Command(args[0], args[1:]...) + + return &CmdObj{ + cmdStr: strings.Join(args, " "), + cmd: cmd, + } +} + +func (c *OSCommand) NewCmdObj(cmd *exec.Cmd) ICmdObj { + return &CmdObj{ + cmdStr: strings.Join(cmd.Args, " "), + cmd: cmd, + } +} |