Compare commits
2 Commits
0da097168d
...
8d632b0691
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d632b0691 | |||
| 6c8399dbff |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
*.gcow2
|
*.qcow2
|
||||||
result
|
result
|
||||||
example.json
|
example.json
|
||||||
go.sum
|
go.sum
|
||||||
|
|||||||
@@ -4,28 +4,46 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"unknown.com/gokill/internal"
|
"unknown.com/gokill/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Command string `json:"command"`
|
Command string `json:"command"`
|
||||||
Args []string `json:"args"`
|
|
||||||
ActionChan chan bool
|
ActionChan chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Command) DryExecute() {
|
func (c Command) DryExecute() {
|
||||||
fmt.Printf("Test Executing Command:\n%s ", c.Command)
|
fmt.Printf("Test Executing Command:\n%s ", c.Command)
|
||||||
for _, arg := range c.Args {
|
|
||||||
fmt.Printf("%s ", arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("")
|
|
||||||
c.ActionChan <- true
|
c.ActionChan <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Command) splitCommandString() (string, []string, error) {
|
||||||
|
splitted := strings.Fields(c.Command)
|
||||||
|
|
||||||
|
if len(splitted) == 0 {
|
||||||
|
return "", nil, fmt.Errorf("Command is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(splitted) == 1 {
|
||||||
|
return splitted[0], []string(nil), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return splitted[0], splitted[1:], nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c Command) Execute() {
|
func (c Command) Execute() {
|
||||||
cmd := exec.Command(c.Command, c.Args...)
|
command, args, err := c.splitCommandString()
|
||||||
|
fmt.Println("Executing command: ", c.Command)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
c.ActionChan <- false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(command, args...)
|
||||||
|
|
||||||
stdout, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
|
|
||||||
@@ -71,6 +89,5 @@ func (p Command) GetDescription() string {
|
|||||||
func (p Command) GetOptions() []internal.ConfigOption {
|
func (p Command) GetOptions() []internal.ConfigOption {
|
||||||
return []internal.ConfigOption{
|
return []internal.ConfigOption{
|
||||||
{"command", "string", "command to execute", ""},
|
{"command", "string", "command to execute", ""},
|
||||||
{"args", "string[]", "args", ""},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user