Compare commits

...

2 Commits

Author SHA1 Message Date
8d632b0691 [actions/unix_command] rm args param to simplify usage 2023-10-30 15:07:43 +01:00
6c8399dbff [gitignore] update 2023-10-30 15:07:26 +01:00
2 changed files with 26 additions and 9 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
*.gcow2 *.qcow2
result result
example.json example.json
go.sum go.sum

View File

@@ -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", ""},
} }
} }