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

Commit

Permalink
Add possibility to provide parameters via command
Browse files Browse the repository at this point in the history
  • Loading branch information
wilriker committed Dec 13, 2019
1 parent 31a8882 commit 43eb2ac
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -16,6 +16,28 @@ Usage of ./execonmcode:
Path to socket (default "/var/run/duet.sock")
```

## Parameters
`execonmcode` does provide a simple mechanism for parameter substitution. It is possible to pass string parameters to the
selected `M-Code` and have them inserted in the `-command`. In the command string they have to be single letters prefixed by
the percent-sign (`%`) and they must not be `G`, `M` or `T`.

All parameters that do not have a corresponding value in the `M-Code` will be forwarded as given.

### Example
Run `execonmcode` as
```
$ ./execonmcode -command "mycommand %F %N %D"
```
Then you can use the following `M-Code` syntax to replace these parameters
```
M7722 F"my first parameter" N"my second parameter"
```
this will lead to an execution of
```
mycommand "my first parameter" "my second parameter" %D
```
Note that `%D` was passed as is since it was not given in the `M-Code`.

# Installation
* Download
* Rename to just `execonmcode`
Expand Down
23 changes: 22 additions & 1 deletion executor.go
Expand Up @@ -5,11 +5,16 @@ import (
"os/exec"
"strings"

"github.com/wilriker/goduetapiclient/commands"
"github.com/wilriker/goduetapiclient/connection"
"github.com/wilriker/goduetapiclient/connection/initmessages"
"github.com/wilriker/goduetapiclient/types"
)

const (
variablePrefix = "%"
)

type Executor struct {
socketPath string
mCode int64
Expand Down Expand Up @@ -48,7 +53,7 @@ func (e *Executor) Run() {
continue
}
if c.Type == types.MCode && c.MajorNumber != nil && *c.MajorNumber == e.mCode {
cmd := exec.Command(e.command, e.args...)
cmd := exec.Command(e.command, e.getArgs(c)...)
err := cmd.Run()
if err != nil {
err = ic.ResolveCode(types.Error, err.Error())
Expand All @@ -63,3 +68,19 @@ func (e *Executor) Run() {
}
}
}

func (e *Executor) getArgs(c *commands.Code) []string {
args := make([]string, len(e.args))
for _, v := range e.args {
if strings.HasPrefix(v, variablePrefix) {
vl := strings.TrimSpace(strings.ToUpper(strings.TrimLeft(v, variablePrefix)))
if len(vl) == 1 {
if pv := c.Parameter(vl); pv != nil {
v = pv.AsString()
}
}
}
args = append(args, v)
}
return args
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -2,4 +2,4 @@ module github.com/wilriker/execonmcode

go 1.13

require github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c
require github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a
2 changes: 2 additions & 0 deletions go.sum
@@ -1,2 +1,4 @@
github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c h1:kbeSWusNKHJYqPLLUn3UqBsgUw+fqTLGBYqhiVfCvhQ=
github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM=
github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a h1:jlWEyWAq7VTVxZo6SxywPjmZkEanBFhnfvzwgmh5KxA=
github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM=

0 comments on commit 43eb2ac

Please sign in to comment.