Something went wrong on our end
Select Git revision
-
Volker Schukai authoredVolker Schukai authored
runnable-shell.go 2.52 KiB
package jobqueue
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
)
func NewShellRunnableFromMap(data map[string]interface{}) (*ShellRunnable, error) {
scriptPath, _ := data["scriptpath"].(string)
script, _ := data["script"].(string)
if scriptPath != "" && script != "" {
return nil, fmt.Errorf("%w: ScriptPath and Script are mutually exclusive", ErrInvalidData)
}
if scriptPath == "" && script == "" {
return nil, fmt.Errorf("%w: ScriptPath or Script is required", ErrInvalidData)
}
return &ShellRunnable{
ScriptPath: scriptPath,
Script: script,
}, nil
}
// ShellResult is a result of a shell script
type ShellResult struct {
Output string
Error string
ExitCode int
}
type ShellRunnable struct {
ScriptPath string
Script string
}
func (s *ShellRunnable) Run(ctx context.Context) (RunResult[ShellResult], error) {
scriptPath := s.ScriptPath
if s.Script != "" {
// write to temp
tmp, err := os.CreateTemp("", "script-*.sh")
if err != nil {
return RunResult[ShellResult]{
Status: ResultStatusFailed,
Data: ShellResult{
Output: "",
ExitCode: DefaultErrorExitCode,
Error: fmt.Errorf("%w: %v", ErrFailedToCreateTempFile, err).Error(),
},
}, err
}
scriptPath = tmp.Name()
defer os.Remove(scriptPath)
_, err = tmp.WriteString(s.Script)
defer tmp.Close()
if err != nil {
return RunResult[ShellResult]{
Status: ResultStatusFailed,
Data: ShellResult{
Output: "",
ExitCode: DefaultErrorExitCode,