diff --git a/README.md b/README.md index 5abe63e..043d491 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ Usage of ./execonmcode: Code that will initiate execution of the command. This can be specified multiple times. -noFlush Do not flush the code channel before executing the associated command + -returnOutput + Return the output of the command back to DCS. Use with care. Cannot be combined with -execAsync. -socketPath string Path to socket (default "/var/run/dsf/dcs.sock") -trace diff --git a/cmd/eom/execonmcode.go b/cmd/eom/execonmcode.go index 82a8e85..d43d2ee 100644 --- a/cmd/eom/execonmcode.go +++ b/cmd/eom/execonmcode.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "log" "os" "strings" @@ -12,7 +13,7 @@ import ( ) const ( - version = "5.1.1" + version = "5.1.2" ) func main() { @@ -25,13 +26,14 @@ func main() { flag.Var(&s.Commands, "command", "Command to execute. This can be specified multiple times.") flag.BoolVar(&s.NoFlush, "noFlush", false, "Do not flush the code channel before executing the associated command") flag.BoolVar(&s.ExecAsync, "execAsync", false, "Run command to execute async and return success to DCS immediately") + flag.BoolVar(&s.ReturnOutput, "returnOutput", false, "Return the output of the command back to DCS. Use with care. Cannot be combined with -execAsync.") flag.BoolVar(&s.Debug, "debug", false, "Print debug output") flag.BoolVar(&s.Trace, "trace", false, "Print underlying requests/responses") - version := flag.Bool("version", false, "Show version and exit") + printVersion := flag.Bool("version", false, "Show version and exit") flag.Parse() - if *version { - log.Println(version) + if *printVersion { + fmt.Println(version) os.Exit(0) } @@ -50,6 +52,10 @@ func main() { log.Fatal("Unsupported InterceptionMode", s.InterceptionMode) } + if s.ExecAsync && s.ReturnOutput { + log.Fatal("-execAsync and -returnOutput cannot be used together.") + } + e := execonmcode.NewExecutor(s) err := e.Run() if err != nil { diff --git a/executor.go b/executor.go index 552200d..9858a9f 100644 --- a/executor.go +++ b/executor.go @@ -18,14 +18,15 @@ const ( ) type Executor struct { - socketPath string - mode initmessages.InterceptionMode - mCodes map[int64]int - commands Commands - execAsync bool - flush bool - debug bool - trace bool + socketPath string + mode initmessages.InterceptionMode + mCodes map[int64]int + commands Commands + execAsync bool + returnOutput bool + flush bool + debug bool + trace bool } func NewExecutor(s Settings) *Executor { @@ -41,14 +42,15 @@ func NewExecutor(s Settings) *Executor { } } return &Executor{ - socketPath: s.SocketPath, - mode: initmessages.InterceptionMode(s.InterceptionMode), - mCodes: mc, - commands: s.Commands, - execAsync: s.ExecAsync, - flush: !s.NoFlush, - debug: s.Debug, - trace: s.Trace, + socketPath: s.SocketPath, + mode: initmessages.InterceptionMode(s.InterceptionMode), + mCodes: mc, + commands: s.Commands, + execAsync: s.ExecAsync, + returnOutput: s.ReturnOutput, + flush: !s.NoFlush, + debug: s.Debug, + trace: s.Trace, } } @@ -109,7 +111,11 @@ func (e *Executor) Run() error { if err != nil { err = ic.ResolveCode(messages.Error, fmt.Sprintf("%s: %s", err.Error(), string(output))) } else { - err = ic.ResolveCode(messages.Success, "") + msg := "" + if e.returnOutput { + msg = string(output) + } + err = ic.ResolveCode(messages.Success, msg) } } if err != nil { diff --git a/settings.go b/settings.go index 7e0dca7..49fb67b 100644 --- a/settings.go +++ b/settings.go @@ -7,6 +7,7 @@ type Settings struct { Commands Commands NoFlush bool ExecAsync bool + ReturnOutput bool Debug bool Trace bool }