Skip to content

Commit

Permalink
Let pty be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Seanstoppable committed Oct 7, 2020
1 parent 050d2a7 commit 0576d87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions modules/cmdrunner/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Settings struct {
args []string `help:"The arguments to the command, with each item as an element in an array. Example: for curl -I cisco.com, the arguments array would be ['-I', 'cisco.com']."`
cmd string `help:"The terminal command to be run, withouth the arguments. Ie: ping, whoami, curl."`
tail bool `help:"Automatically scroll to the end of the command output."`
pty bool `help:"Run the command in a pseudo-terminal. Some apps will behave differently if they feel in a terminal. For example, some apps will produce colorized output in a terminal, and non-colorized output otherwise. Default false" optional:"true"`
maxLines int `help:"Maximum number of lines kept in the buffer."`

// The dimensions of the module
Expand All @@ -32,6 +33,7 @@ func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig

args: utils.ToStrs(moduleConfig.UList("args")),
cmd: moduleConfig.UString("cmd"),
pty: moduleConfig.UBool("pty", false),
tail: moduleConfig.UBool("tail", false),
maxLines: moduleConfig.UInt("maxLines", 256),
}
Expand Down
27 changes: 21 additions & 6 deletions modules/cmdrunner/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,35 @@ func runCommandLoop(widget *Widget) {
widget.resetBuffer()
cmd := exec.Command(widget.settings.cmd, widget.settings.args...)
cmd.Env = widget.environment()
f, err := pty.Start(cmd)
// The command has exited, print any error messages
if err != nil {
widget.handleError(err)
var err error
if widget.settings.pty {
err = runCommandPty(widget, cmd)
} else {
err = runCommand(widget, cmd)
}

_, err = io.Copy(widget.buffer, f)
if err != nil {
widget.handleError(err)
}
widget.redrawChan <- true
}
}

func runCommand(widget *Widget, cmd *exec.Cmd) error {
cmd.Stdout = widget
return cmd.Run()
}

func runCommandPty(widget *Widget, cmd *exec.Cmd) error {
f, err := pty.Start(cmd)
// The command has exited, print any error messages
if err != nil {
return err
}

_, err = io.Copy(widget.buffer, f)
return err
}

func (widget *Widget) handleError(err error) {
widget.m.Lock()
_, writeErr := widget.buffer.WriteString(err.Error())
Expand Down

0 comments on commit 0576d87

Please sign in to comment.