This repository was archived by the owner on Sep 5, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 `
Original file line number Diff line number Diff 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+
1318type 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+ }
Original file line number Diff line number Diff line change @@ -2,4 +2,4 @@ module github.com/wilriker/execonmcode
22
33go 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
Original file line number Diff line number Diff line change 11github.com/wilriker/goduetapiclient v0.0.0-20191014112126-94a17c5b2e4c h1:kbeSWusNKHJYqPLLUn3UqBsgUw+fqTLGBYqhiVfCvhQ =
22github.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 =
You can’t perform that action at this time.
0 commit comments