From 43eb2ac6720cb0a9d0b09d19f0cf240dac301716 Mon Sep 17 00:00:00 2001 From: Manuel Coenen Date: Fri, 13 Dec 2019 12:55:05 +0100 Subject: [PATCH] Add possibility to provide parameters via command --- README.md | 22 ++++++++++++++++++++++ executor.go | 23 ++++++++++++++++++++++- go.mod | 2 +- go.sum | 2 ++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b38259e..ca48bae 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/executor.go b/executor.go index 4c9a081..c113f4b 100644 --- a/executor.go +++ b/executor.go @@ -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 @@ -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()) @@ -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 +} diff --git a/go.mod b/go.mod index 75360ed..e784417 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a71131f..07a6b04 100644 --- a/go.sum +++ b/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=