This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework into arbitrary command exectutor
- Loading branch information
Showing
6 changed files
with
115 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
# Description | ||
This is a small extension to [DuetSoftwareFramework](https://github.com/christhamm/DuetSoftwareFramework) | ||
to execute arbitrary system commands when a user-defined `M-Code` is encountered. | ||
|
||
An example usage would be to execute system shutdown on the SBC when a e.g. `M7722` is run. | ||
|
||
# Usage | ||
``` | ||
$ ./shutdownsbc --help | ||
Usage of ./shutdownsbc: | ||
-shutdownMCode int | ||
Code that will initiate shutdown of the SBC (default 7722) | ||
$ ./execonmcode --help | ||
Usage of ./execonmcode: | ||
-command string | ||
Command to execute | ||
-mCode int | ||
Code that will initiate execution of the command (default 7722) | ||
-socketPath string | ||
Path to socket (default "/var/run/duet.sock") | ||
Path to socket (default "/var/run/duet.sock") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
|
||
"github.com/wilriker/execonmcode" | ||
"github.com/wilriker/goduetapiclient/connection" | ||
) | ||
|
||
type settings struct { | ||
socketPath string | ||
mCode int64 | ||
command string | ||
} | ||
|
||
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.Parse() | ||
|
||
if s.mCode < 0 { | ||
log.Fatal("--mCode must be >= 0") | ||
} | ||
|
||
if s.command == "" { | ||
log.Fatal("--command must not be empty") | ||
} | ||
|
||
e := execonmcode.NewExecutor(s.socketPath, s.command, s.mCode) | ||
e.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package execonmcode | ||
|
||
import ( | ||
"log" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/wilriker/goduetapiclient/connection" | ||
"github.com/wilriker/goduetapiclient/connection/initmessages" | ||
"github.com/wilriker/goduetapiclient/types" | ||
) | ||
|
||
type Executor struct { | ||
socketPath string | ||
mCode int64 | ||
command string | ||
args []string | ||
} | ||
|
||
func NewExecutor(socketPath, command string, mCode int64) *Executor { | ||
s := strings.Split(command, " ") | ||
a := []string{} | ||
if len(s) > 1 { | ||
a = s[1:] | ||
} | ||
c := s[0] | ||
return &Executor{ | ||
socketPath: socketPath, | ||
command: c, | ||
args: a, | ||
mCode: mCode, | ||
} | ||
} | ||
|
||
func (e *Executor) Run() { | ||
|
||
ic := connection.InterceptConnection{} | ||
err := ic.Connect(initmessages.InterceptionModePre, e.socketPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer ic.Close() | ||
|
||
for { | ||
c, err := ic.ReceiveCode() | ||
if err != nil { | ||
log.Println("Error:", err) | ||
continue | ||
} | ||
if c.Type == types.MCode && c.MajorNumber != nil && *c.MajorNumber == e.mCode { | ||
cmd := exec.Command(e.command, e.args...) | ||
err := cmd.Run() | ||
if err != nil { | ||
err = ic.ResolveCode(types.Error, err.Error()) | ||
} else { | ||
err = ic.ResolveCode(types.Success, "") | ||
} | ||
if err != nil { | ||
log.Println("Error:", err) | ||
} | ||
} else { | ||
ic.IgnoreCode() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/wilriker/dsfext-shutdownsbc | ||
module github.com/wilriker/execonmcode | ||
|
||
go 1.13 | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters