Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Add -returnOutput flag to return output of application back to DCS in…
Browse files Browse the repository at this point in the history
… case of successful execution
  • Loading branch information
wilriker committed Jan 21, 2021
1 parent b59e066 commit ad42d63
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions cmd/eom/execonmcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"strings"
Expand All @@ -12,7 +13,7 @@ import (
)

const (
version = "5.1.1"
version = "5.1.2"
)

func main() {
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down
40 changes: 23 additions & 17 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Settings struct {
Commands Commands
NoFlush bool
ExecAsync bool
ReturnOutput bool
Debug bool
Trace bool
}

0 comments on commit ad42d63

Please sign in to comment.