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

Commit

Permalink
Add ability to handle arbitrary amount of mcode+command pairs
Browse files Browse the repository at this point in the history
Update to goduetapiclient 1.2.0
  • Loading branch information
wilriker committed Jan 6, 2020
1 parent c871427 commit 967be88
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 41 deletions.
22 changes: 10 additions & 12 deletions cmd/eom/execonmcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
40 changes: 40 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 41 additions & 28 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -63,26 +63,39 @@ 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()
}
}
}

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 {
Expand All @@ -91,7 +104,7 @@ func (e *Executor) getArgs(c *commands.Code) []string {
}
}
}
args = append(args, v)
a = append(a, v)
}
return args
return a
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
29 changes: 29 additions & 0 deletions mcodes.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 967be88

Please sign in to comment.