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

Commit ad42d63

Browse files
committed
Add -returnOutput flag to return output of application back to DCS in case of successful execution
1 parent b59e066 commit ad42d63

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Usage of ./execonmcode:
2020
Code that will initiate execution of the command. This can be specified multiple times.
2121
-noFlush
2222
Do not flush the code channel before executing the associated command
23+
-returnOutput
24+
Return the output of the command back to DCS. Use with care. Cannot be combined with -execAsync.
2325
-socketPath string
2426
Path to socket (default "/var/run/dsf/dcs.sock")
2527
-trace

cmd/eom/execonmcode.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"log"
67
"os"
78
"strings"
@@ -12,7 +13,7 @@ import (
1213
)
1314

1415
const (
15-
version = "5.1.1"
16+
version = "5.1.2"
1617
)
1718

1819
func main() {
@@ -25,13 +26,14 @@ func main() {
2526
flag.Var(&s.Commands, "command", "Command to execute. This can be specified multiple times.")
2627
flag.BoolVar(&s.NoFlush, "noFlush", false, "Do not flush the code channel before executing the associated command")
2728
flag.BoolVar(&s.ExecAsync, "execAsync", false, "Run command to execute async and return success to DCS immediately")
29+
flag.BoolVar(&s.ReturnOutput, "returnOutput", false, "Return the output of the command back to DCS. Use with care. Cannot be combined with -execAsync.")
2830
flag.BoolVar(&s.Debug, "debug", false, "Print debug output")
2931
flag.BoolVar(&s.Trace, "trace", false, "Print underlying requests/responses")
30-
version := flag.Bool("version", false, "Show version and exit")
32+
printVersion := flag.Bool("version", false, "Show version and exit")
3133
flag.Parse()
3234

33-
if *version {
34-
log.Println(version)
35+
if *printVersion {
36+
fmt.Println(version)
3537
os.Exit(0)
3638
}
3739

@@ -50,6 +52,10 @@ func main() {
5052
log.Fatal("Unsupported InterceptionMode", s.InterceptionMode)
5153
}
5254

55+
if s.ExecAsync && s.ReturnOutput {
56+
log.Fatal("-execAsync and -returnOutput cannot be used together.")
57+
}
58+
5359
e := execonmcode.NewExecutor(s)
5460
err := e.Run()
5561
if err != nil {

executor.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ const (
1818
)
1919

2020
type Executor struct {
21-
socketPath string
22-
mode initmessages.InterceptionMode
23-
mCodes map[int64]int
24-
commands Commands
25-
execAsync bool
26-
flush bool
27-
debug bool
28-
trace bool
21+
socketPath string
22+
mode initmessages.InterceptionMode
23+
mCodes map[int64]int
24+
commands Commands
25+
execAsync bool
26+
returnOutput bool
27+
flush bool
28+
debug bool
29+
trace bool
2930
}
3031

3132
func NewExecutor(s Settings) *Executor {
@@ -41,14 +42,15 @@ func NewExecutor(s Settings) *Executor {
4142
}
4243
}
4344
return &Executor{
44-
socketPath: s.SocketPath,
45-
mode: initmessages.InterceptionMode(s.InterceptionMode),
46-
mCodes: mc,
47-
commands: s.Commands,
48-
execAsync: s.ExecAsync,
49-
flush: !s.NoFlush,
50-
debug: s.Debug,
51-
trace: s.Trace,
45+
socketPath: s.SocketPath,
46+
mode: initmessages.InterceptionMode(s.InterceptionMode),
47+
mCodes: mc,
48+
commands: s.Commands,
49+
execAsync: s.ExecAsync,
50+
returnOutput: s.ReturnOutput,
51+
flush: !s.NoFlush,
52+
debug: s.Debug,
53+
trace: s.Trace,
5254
}
5355
}
5456

@@ -109,7 +111,11 @@ func (e *Executor) Run() error {
109111
if err != nil {
110112
err = ic.ResolveCode(messages.Error, fmt.Sprintf("%s: %s", err.Error(), string(output)))
111113
} else {
112-
err = ic.ResolveCode(messages.Success, "")
114+
msg := ""
115+
if e.returnOutput {
116+
msg = string(output)
117+
}
118+
err = ic.ResolveCode(messages.Success, msg)
113119
}
114120
}
115121
if err != nil {

settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Settings struct {
77
Commands Commands
88
NoFlush bool
99
ExecAsync bool
10+
ReturnOutput bool
1011
Debug bool
1112
Trace bool
1213
}

0 commit comments

Comments
 (0)