diff --git a/cmd/eom/execonmcode.go b/cmd/eom/execonmcode.go index 5ff276e..04fcef3 100644 --- a/cmd/eom/execonmcode.go +++ b/cmd/eom/execonmcode.go @@ -10,26 +10,24 @@ import ( type settings struct { socketPath string - mCode int64 - command string + mCodes execonmcode.MCodes + commands execonmcode.Commands + debug bool } func main() { s := settings{} - flag.StringVar(&s.socketPath, "socketPath", connection.DefaultSocketPath, "Path to socket") - flag.Int64Var(&s.mCode, "mCode", 7722, "Code that will initiate execution of the command") - flag.StringVar(&s.command, "command", "", "Command to execute") + flag.StringVar(&s.socketPath, "socketPath", connection.FullSocketPath, "Path to socket") + flag.Var(&s.mCodes, "mCode", "Code that will initiate execution of the command") + flag.Var(&s.commands, "command", "Command to execute") + flag.BoolVar(&s.debug, "debug", false, "Print debug output") flag.Parse() - if s.mCode < 0 { - log.Fatal("--mCode must be >= 0") + if s.mCodes.Len() != s.commands.Len() { + log.Fatal("Unequal amount of M-codes and commands given") } - if s.command == "" { - log.Fatal("--command must not be empty") - } - - e := execonmcode.NewExecutor(s.socketPath, s.command, s.mCode) + e := execonmcode.NewExecutor(s.socketPath, s.commands, s.mCodes, s.debug) e.Run() } diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..804a0a0 --- /dev/null +++ b/commands.go @@ -0,0 +1,40 @@ +package execonmcode + +import ( + "errors" + "fmt" + "strings" +) + +type Commands []string + +func (c *Commands) String() string { + return fmt.Sprintf("%v", *c) +} + +func (c *Commands) Set(v string) error { + *c = append(*c, v) + return nil +} + +func (c *Commands) Len() int { + return len(*c) +} + +func (c *Commands) Get(i int) (string, []string, error) { + if i >= c.Len() { + return "", nil, errors.New("Index out of range") + } + cmd := (*c)[i] + s := strings.Split(cmd, " ") + a := make([]string, 0) + if len(s) > 1 { + for _, p := range s[1:] { + pp := strings.TrimSpace(p) + if pp != "" { + a = append(a, p) + } + } + } + return s[0], a, nil +} diff --git a/executor.go b/executor.go index 8f55d5b..fa21160 100644 --- a/executor.go +++ b/executor.go @@ -19,28 +19,28 @@ const ( type Executor struct { socketPath string - mCode int64 - command string - args []string + mCodes map[int64]int + commands Commands + debug bool } -func NewExecutor(socketPath, command string, mCode int64) *Executor { - s := strings.Split(command, " ") - a := make([]string, 0) - if len(s) > 1 { - for _, p := range s[1:] { - pp := strings.TrimSpace(p) - if pp != "" { - a = append(a, p) +func NewExecutor(socketPath string, commands Commands, mCodes MCodes, debug bool) *Executor { + mc := make(map[int64]int) + for i, m := range mCodes { + mc[m] = i + if debug { + cmd, args, err := commands.Get(i) + if err != nil { + log.Println(m, err) } + log.Printf("%d: %s %s", m, cmd, strings.Join(args, " ")) } } - c := s[0] return &Executor{ socketPath: socketPath, - command: c, - args: a, - mCode: mCode, + mCodes: mc, + commands: commands, + debug: debug, } } @@ -63,16 +63,29 @@ func (e *Executor) Run() { log.Printf("Error receiving code: %s", err) continue } - if c.Type == types.MCode && c.MajorNumber != nil && *c.MajorNumber == e.mCode { - cmd := exec.Command(e.command, e.getArgs(c)...) - output, err := cmd.CombinedOutput() - if err != nil { - err = ic.ResolveCode(types.Error, fmt.Sprintf("%s: %s", err.Error(), string(output))) - } else { - err = ic.ResolveCode(types.Success, "") + if c.Type == types.MCode && c.MajorNumber != nil { + i, ok := e.mCodes[*c.MajorNumber] + if !ok { + ic.IgnoreCode() + continue } + comd, a, err := e.commands.Get(i) if err != nil { - log.Println("Error executing command:", err) + ic.ResolveCode(types.Error, err.Error()) + } else { + cmd := exec.Command(comd, e.getArgs(c, a)...) + if e.debug { + log.Println("Executing:", cmd) + } + output, err := cmd.CombinedOutput() + if err != nil { + err = ic.ResolveCode(types.Error, fmt.Sprintf("%s: %s", err.Error(), string(output))) + } else { + err = ic.ResolveCode(types.Success, "") + } + if err != nil { + log.Println("Error executing command:", err) + } } } else { ic.IgnoreCode() @@ -80,9 +93,9 @@ func (e *Executor) Run() { } } -func (e *Executor) getArgs(c *commands.Code) []string { - args := make([]string, 0) - for _, v := range e.args { +func (e *Executor) getArgs(c *commands.Code, args []string) []string { + a := make([]string, 0) + for _, v := range args { if strings.HasPrefix(v, variablePrefix) { vl := strings.TrimSpace(strings.ToUpper(strings.TrimLeft(v, variablePrefix))) if len(vl) == 1 { @@ -91,7 +104,7 @@ func (e *Executor) getArgs(c *commands.Code) []string { } } } - args = append(args, v) + a = append(a, v) } - return args + return a } diff --git a/go.mod b/go.mod index a949b5d..20b8245 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/wilriker/execonmcode go 1.13 -require github.com/wilriker/goduetapiclient v1.1.0 +require github.com/wilriker/goduetapiclient v1.2.0 diff --git a/go.sum b/go.sum index 81790e2..3fa1965 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,7 @@ github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a h1:jlWEyW github.com/wilriker/goduetapiclient v0.0.0-20191213105344-41ea5d36085a/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM= github.com/wilriker/goduetapiclient v1.1.0 h1:s2ufoNPyEZ8QxP7lY0COMfDGRf1A6x/xPxEdsoT9cGA= github.com/wilriker/goduetapiclient v1.1.0/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM= +github.com/wilriker/goduetapiclient v1.1.1-0.20191226195316-8e83bd8e4124 h1:YApThH1QQk1QShCc82Crj7aSiDXtWJdxS39kOFiU61c= +github.com/wilriker/goduetapiclient v1.1.1-0.20191226195316-8e83bd8e4124/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM= +github.com/wilriker/goduetapiclient v1.2.0 h1:lB5owgJVYpTeMjWcT6slQwKXQgPgrzCdHdeNIv80JCo= +github.com/wilriker/goduetapiclient v1.2.0/go.mod h1:KSadGbt2Z/wbyhZoh3I+Zp0me9YqrLhAPrAjH9J8OLM= diff --git a/mcodes.go b/mcodes.go new file mode 100644 index 0000000..d3be7f1 --- /dev/null +++ b/mcodes.go @@ -0,0 +1,29 @@ +package execonmcode + +import ( + "errors" + "fmt" + "strconv" +) + +type MCodes []int64 + +func (m *MCodes) String() string { + return fmt.Sprintf("%v", *m) +} + +func (m *MCodes) Len() int { + return len(*m) +} + +func (m *MCodes) Set(v string) error { + mc, err := strconv.ParseInt(v, 10, 64) + if err != nil { + return err + } + if mc < 0 { + return errors.New("-mCode must be >= 0") + } + *m = append(*m, mc) + return nil +}