From 0576d879d6890230a58e1b375d9070c583834c66 Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Wed, 7 Oct 2020 18:49:21 -0400 Subject: [PATCH] Let pty be optional --- modules/cmdrunner/settings.go | 2 ++ modules/cmdrunner/widget.go | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/modules/cmdrunner/settings.go b/modules/cmdrunner/settings.go index 7f88f89f8d..faa7dda913 100644 --- a/modules/cmdrunner/settings.go +++ b/modules/cmdrunner/settings.go @@ -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 @@ -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), } diff --git a/modules/cmdrunner/widget.go b/modules/cmdrunner/widget.go index 77ea058ac3..ecb4f2f02a 100644 --- a/modules/cmdrunner/widget.go +++ b/modules/cmdrunner/widget.go @@ -124,13 +124,12 @@ 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) } @@ -138,6 +137,22 @@ func runCommandLoop(widget *Widget) { } } +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())