From 46d12706489c53113cc091be020c7a4d0f95fde8 Mon Sep 17 00:00:00 2001 From: kalipso Date: Tue, 31 Oct 2023 13:11:31 +0100 Subject: [PATCH] [actions/shell_script] init --- actions/actions.go | 1 + actions/shell_script.go | 119 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 actions/shell_script.go diff --git a/actions/actions.go b/actions/actions.go index 0dd2660..77262a5 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -122,6 +122,7 @@ func GetAllActions() []DocumentedAction { Printer{}, TimeOut{}, Command{}, + ShellScript{}, Shutdown{}, } } diff --git a/actions/shell_script.go b/actions/shell_script.go new file mode 100644 index 0000000..b132e63 --- /dev/null +++ b/actions/shell_script.go @@ -0,0 +1,119 @@ +package actions + +import ( + "encoding/json" + "fmt" + "os/exec" + "os" + + "unknown.com/gokill/internal" +) + +type ShellScript struct { + Path string `json:"path"` + ActionChan ActionResultChan +} + +func isExecutableFile(path string) bool { + fi, err := os.Lstat(path) + + if err != nil { + fmt.Println("Test executing Shellscript Failed.") + return false + } + + mode := fi.Mode() + + //TODO: should check if current user can execute + if mode&01111 == 0 { + return false + } + + return true +} + +func (c ShellScript) DryExecute() { + fmt.Printf("Test Executing ShellScript:\n%s\n", c.Path) + + _, err := os.Open(c.Path) + + if err != nil { + fmt.Println("Test executing Shellscript Failed.") + c.ActionChan <- err + return + } + + if !isExecutableFile(c.Path) { + fmt.Println("Test executing Shellscript Failed.") + c.ActionChan <- fmt.Errorf("File is not executable: %s", c.Path) + return + } + + c.ActionChan <- nil +} + +func (c ShellScript) Execute() { + if !isExecutableFile(c.Path) { + fmt.Println("Test executing Shellscript Failed.") + c.ActionChan <- fmt.Errorf("File is not executable: %s", c.Path) + return + } + + cmd := exec.Command("/bin/sh", c.Path) + + stdout, err := cmd.Output() + + if err != nil { + fmt.Println(err.Error()) + c.ActionChan <- err + } + + fmt.Println(string(stdout[:])) + c.ActionChan <- nil +} + +func CreateShellScript(config internal.ActionConfig, c ActionResultChan) (ShellScript, error) { + result := ShellScript{} + + err := json.Unmarshal(config.Options, &result) + + if err != nil { + return ShellScript{}, err + } + + if result.Path == "" { + return ShellScript{}, internal.OptionMissingError{"path"} + } + + result.ActionChan = c + return result, nil +} + +func (cc ShellScript) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) { + return CreateShellScript(config, c) +} + +func (p ShellScript) GetName() string { + return "ShellScript" +} + +func (p ShellScript) GetDescription() string { + return "Executes the given shell script." +} + +func (p ShellScript) GetExample() string { + return ` + { + "type": "ShellScript", + "options": { + "path": "/path/to/file.sh" + } + } + ` +} + +func (p ShellScript) GetOptions() []internal.ConfigOption { + return []internal.ConfigOption{ + {"path", "string", "path to script to execute", ""}, + } +}