executeScriptAtPath executes a single script with the given data Returns any error that occurred during execution
(scriptPath string, data *ScriptData)
| 55 | // executeScriptAtPath executes a single script with the given data |
| 56 | // Returns any error that occurred during execution |
| 57 | func (d *Downloader) executeScriptAtPath(scriptPath string, data *ScriptData) error { |
| 58 | if scriptPath == "" { |
| 59 | return fmt.Errorf("script path is empty") |
| 60 | } |
| 61 | |
| 62 | // Check if script file exists |
| 63 | if _, err := os.Stat(scriptPath); os.IsNotExist(err) { |
| 64 | return fmt.Errorf("script file does not exist: %s", scriptPath) |
| 65 | } |
| 66 | |
| 67 | // Determine the script interpreter based on file extension |
| 68 | var cmd *exec.Cmd |
| 69 | ext := filepath.Ext(scriptPath) |
| 70 | |
| 71 | switch ext { |
| 72 | case ".sh", ".bash": |
| 73 | cmd = exec.Command("bash", scriptPath) |
| 74 | case ".py": |
| 75 | cmd = exec.Command("python3", scriptPath) |
| 76 | case ".js": |
| 77 | cmd = exec.Command("node", scriptPath) |
| 78 | case ".bat", ".cmd": |
| 79 | // Windows batch files |
| 80 | if runtime.GOOS == "windows" { |
| 81 | cmd = exec.Command("cmd", "/c", scriptPath) |
| 82 | } else { |
| 83 | // Batch files are Windows-specific |
| 84 | return fmt.Errorf("batch files (.bat/.cmd) are only supported on Windows") |
| 85 | } |
| 86 | case ".ps1": |
| 87 | // PowerShell scripts |
| 88 | if runtime.GOOS == "windows" { |
| 89 | cmd = exec.Command("powershell", "-ExecutionPolicy", "Bypass", "-File", scriptPath) |
| 90 | } else { |
| 91 | // Try pwsh (PowerShell Core) on non-Windows systems |
| 92 | cmd = exec.Command("pwsh", "-File", scriptPath) |
| 93 | } |
| 94 | case "": |
| 95 | // No extension, try to execute directly (assumes shebang or executable) |
| 96 | cmd = exec.Command(scriptPath) |
| 97 | default: |
| 98 | // Unknown extension, try to execute directly |
| 99 | cmd = exec.Command(scriptPath) |
| 100 | } |
| 101 | |
| 102 | // Set environment variables with task information |
| 103 | cmd.Env = append(os.Environ(), |
| 104 | fmt.Sprintf("GOPEED_EVENT=%s", data.Event), |
| 105 | fmt.Sprintf("GOPEED_TASK_ID=%s", data.Payload.Task.ID), |
| 106 | fmt.Sprintf("GOPEED_TASK_NAME=%s", data.Payload.Task.Name()), |
| 107 | fmt.Sprintf("GOPEED_TASK_STATUS=%s", data.Payload.Task.Status), |
| 108 | ) |
| 109 | |
| 110 | // Add task path using the same logic as task deletion |
| 111 | if data.Payload.Task.Meta != nil && data.Payload.Task.Meta.Res != nil { |
| 112 | var taskPath string |
| 113 | if data.Payload.Task.Meta.Res.Name != "" { |
| 114 | // Multi-file task (folder) |