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

Commit 43eb2ac

Browse files
committed
Add possibility to provide parameters via command
1 parent 31a8882 commit 43eb2ac

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ Usage of ./execonmcode:
1616
Path to socket (default "/var/run/duet.sock")
1717
```
1818

19+
## Parameters
20+
`execonmcode` does provide a simple mechanism for parameter substitution. It is possible to pass string parameters to the
21+
selected `M-Code` and have them inserted in the `-command`. In the command string they have to be single letters prefixed by
22+
the percent-sign (`%`) and they must not be `G`, `M` or `T`.
23+
24+
All parameters that do not have a corresponding value in the `M-Code` will be forwarded as given.
25+
26+
### Example
27+
Run `execonmcode` as
28+
```
29+
$ ./execonmcode -command "mycommand %F %N %D"
30+
```
31+
Then you can use the following `M-Code` syntax to replace these parameters
32+
```
33+
M7722 F"my first parameter" N"my second parameter"
34+
```
35+
this will lead to an execution of
36+
```
37+
mycommand "my first parameter" "my second parameter" %D
38+
```
39+
Note that `%D` was passed as is since it was not given in the `M-Code`.
40+
1941
# Installation
2042
* Download
2143
* Rename to just `execonmcode`

executor.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import (
55
"os/exec"
66
"strings"
77

8+
"github.com/wilriker/goduetapiclient/commands"
89
"github.com/wilriker/goduetapiclient/connection"
910
"github.com/wilriker/goduetapiclient/connection/initmessages"
1011
"github.com/wilriker/goduetapiclient/types"
1112
)
1213

14+
const (
15+
variablePrefix = "%"
16+
)
17+
1318
type Executor struct {
1419
socketPath string
1520
mCode int64
@@ -48,7 +53,7 @@ func (e *Executor) Run() {
4853
continue
4954
}
5055
if c.Type == types.MCode && c.MajorNumber != nil && *c.MajorNumber == e.mCode {
51-
cmd := exec.Command(e.command, e.args...)
56+
cmd := exec.Command(e.command, e.getArgs(c)...)
5257
err := cmd.Run()
5358
if err != nil {
5459
err = ic.ResolveCode(types.Error, err.Error())
@@ -63,3 +68,19 @@ func (e *Executor) Run() {
6368
}
6469
}
6570
}
71+
72+
func (e *Executor) getArgs(c *commands.Code) []string {
73+
args := make([]string, len(e.args))
74+
for _, v := range e.args {
75+
if strings.HasPrefix(v, variablePrefix) {
76+
vl := strings.TrimSpace(strings.ToUpper(strings.TrimLeft(v, variablePrefix)))
77+
if len(vl) == 1 {
78+
if pv := c.Parameter(vl); pv != nil {
79+
v = pv.AsString()
80+
}
81+
}
82+
}
83+
args = append(args, v)
84+
}
85+
return args
86+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/wilriker/execonmcode
22

33
go 1.13
44

5-
require github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c
5+
require github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c h1:kbeSWusNKHJYqPLLUn3UqBsgUw+fqTLGBYqhiVfCvhQ=
22
github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM=
3+
github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a h1:jlWEyWAq7VTVxZo6SxywPjmZkEanBFhnfvzwgmh5KxA=
4+
github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM=

0 commit comments

Comments
 (0)