Skip to content
Snippets Groups Projects
Select Git revision
  • 97c970370d43d875c3c4615888735298129172a8
  • master default protected
  • v1.23.2
  • v1.23.1
  • v1.23.0
  • v1.22.0
  • v1.21.1
  • v1.21.0
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.4
  • v1.19.3
  • v1.19.2
  • v1.19.1
  • v1.19.0
  • v1.18.2
  • v1.18.1
  • v1.18.0
  • v1.17.0
  • v1.16.1
22 results

runnable-shell.go

Blame
  • runnable-shell.go 3.05 KiB
    // Copyright 2023 schukai GmbH
    // SPDX-License-Identifier: AGPL-3.0
    
    package jobqueue
    
    import (
    	"context"
    	"fmt"
    	"os"
    	"os/exec"
    	"strings"
    	"sync"
    )
    
    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
    }
    
    func (s *ShellResult) GetResult() string {
    	return s.Output
    }
    
    func (s *ShellResult) GetError() (string, int) {
    	if s.ExitCode != 0 {
    		return s.Error, s.ExitCode
    	}
    	return "", 0
    }
    
    type ShellRunnable struct {
    	ScriptPath string
    	Script     string
    	mu         sync.Mutex
    }
    
    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 {
    			Error("Failed to create temp file: %v", err)
    			return RunResult[ShellResult]{
    				Status: ResultStatusFailed,
    				Data: ShellResult{
    					Output:   "",