From 2b33525ea123d8638c70e85929f322df314e6e12 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sat, 28 Oct 2023 12:37:18 +0200 Subject: [PATCH] [actions/unix_command] init --- actions/unix_command.go | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 actions/unix_command.go diff --git a/actions/unix_command.go b/actions/unix_command.go new file mode 100644 index 0000000..a58a6bb --- /dev/null +++ b/actions/unix_command.go @@ -0,0 +1,76 @@ +package actions + +import ( + "encoding/json" + "fmt" + "os/exec" + + "unknown.com/gokill/internal" +) + +type Command struct { + Command string `json:"command"` + Args []string `json:"args"` + ActionChan chan bool +} + +func (c Command) DryExecute() { + fmt.Printf("Test Executing Command:\n%s ", c.Command) + for _, arg := range c.Args { + fmt.Printf("%s ", arg) + } + + fmt.Println("") + c.ActionChan <- true +} + +func (c Command) Execute() { + cmd := exec.Command(c.Command, c.Args...) + + stdout, err := cmd.Output() + + if err != nil { + fmt.Println(err.Error()) + } + + fmt.Println(string(stdout[:])) + + c.ActionChan <- true +} + +func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) { + result := Command{} + + err := json.Unmarshal(config.Options, &result) + + if err != nil { + return Command{}, err + } + + if result.Command == "" { + return Command{}, internal.OptionMissingError{"command"} + } + + result.ActionChan = c + + return result, nil +} + +func (cc Command) Create(config internal.ActionConfig, c chan bool) (Action, error) { + return CreateCommand(config, c) +} + +func (p Command) GetName() string { + return "Command" +} + +func (p Command) GetDescription() string { + return "When triggered executes given command" +} + +func (p Command) GetOptions() []internal.ConfigOption { + return []internal.ConfigOption{ + {"command", "string", "command to execute", ""}, + {"args", "string[]", "args", ""}, + } +}