Skip to content
Snippets Groups Projects
Select Git revision
  • 910c9388788b8f5ccb1633db9e466b70d2698e10
  • master default protected
  • 1.31
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
23 results

transformer.mjs

Blame
  • runnable-shell.go 2.59 KiB
    // Copyright 2023 schukai GmbH
    // SPDX-License-Identifier: AGPL-3.0
    
    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,
    					Error:    fmt.Errorf("%w: %v", ErrFailedToWriteTempFile, err).Error(),
    				},
    			}, err
    		}
    
    	}
    
    	// #nosec
    	cmd := exec.CommandContext(ctx, "sh", scriptPath)
    	output, err := cmd.Output()
    
    	var stderr []byte
    	if err != nil {
    		stderr = err.(*exec.ExitError).Stderr
    	}
    
    	exitCode := SuccessExitCode
    
    	if err != nil {
    		if exitError, ok := err.(*exec.ExitError); ok {
    			exitCode = exitError.ExitCode()
    		}
    		return RunResult[ShellResult]{
    			Status: ResultStatusFailed,
    			Data: ShellResult{
    				Output:   string(output),
    				ExitCode: exitCode,
    				Error:    string(stderr),
    			},
    		}, err
    	}
    
    	return RunResult[ShellResult]{
    		Status: ResultStatusSuccess,
    		Data: ShellResult{
    			Output:   strings.TrimSpace(string(output)),
    			ExitCode: exitCode,
    			Error:    string(stderr),
    		},
    	}, nil
    }
    
    func (s *ShellRunnable) GetType() string {
    	return "shell"
    }
    
    func (c *ShellRunnable) GetPersistence() RunnableImport {
    
    	data := JSONMap{
    		"scriptPath": c.ScriptPath,
    		"script":      c.Script,
    	}
    
    	return RunnableImport{
    		Type: c.GetType(),
    		Data: data,
    	}
    }