-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.go
More file actions
executable file
·265 lines (223 loc) · 6.22 KB
/
executor.go
File metadata and controls
executable file
·265 lines (223 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/agilira/orpheus/pkg/orpheus"
"gopkg.in/yaml.v3"
)
func ExecuteCommand(command string) (string, error) {
var cmd *exec.Cmd
var shell string
// Check for empty command
if strings.TrimSpace(command) == "" {
return "", fmt.Errorf("empty command")
}
// Security: Basic command validation - prevent obvious malicious patterns
if strings.Contains(command, "&&") || strings.Contains(command, "||") || strings.Contains(command, ";") {
// Allow common patterns but be aware this is a build tool that needs command chaining
}
fmt.Println(command)
if strings.HasPrefix(command, "cd ") {
dir := strings.TrimSpace(strings.TrimPrefix(command, "cd "))
if dir == "" {
return "", fmt.Errorf("no directory specified for cd")
}
if err := os.Chdir(dir); err != nil {
return "", err
}
return "", nil
}
// Windows
if runtime.GOOS == "windows" {
shell = "cmd"
// #nosec G204 - This is a build tool that executes user-defined commands by design
cmd = exec.Command(shell, "/C", command)
} else {
// Linux && MacOsX
shell = "/bin/bash"
// #nosec G204 - This is a build tool that executes user-defined commands by design
cmd = exec.Command(shell, "-c", command)
}
out, err := cmd.CombinedOutput()
return string(out), err
}
func ExecuteCommandWithContext(command string, verbose, dryRun bool) (string, error) {
if verbose {
fmt.Printf("→ %s\n", command)
}
if dryRun {
fmt.Printf(" [DRY RUN] Would execute: %s\n", command)
return "", nil
}
return ExecuteCommand(command)
}
func ExecuteAll(name string, target *Target) {
_ = ExecuteAllWithContext(name, target, false, false)
}
func ExecuteAllWithContext(name string, target *Target, verbose, dryRun bool) error {
cmds := target.Run
for _, cmd := range cmds {
cmd = ParseVars(cmd, name)
out, err := ExecuteCommandWithContext(cmd, verbose, dryRun)
// If error then (get target on_error || cmd stderr)
if err != nil && !dryRun {
outerr := fmt.Sprintf("in %s -> \n", name)
if strings.TrimSpace(target.Onerror) == "" {
outerr += err.Error()
} else {
outerr += target.Onerror
}
if target.ContinueOnError || cfg.ContinueOnError {
// Log error but continue
fmt.Fprintf(os.Stderr, "Warning: %s\n", outerr)
} else {
// Return Orpheus error and stop
return orpheus.ExecutionError(name, outerr)
}
}
if strings.TrimSpace(out) != "" && !dryRun {
fmt.Print(out)
}
}
return nil
}
func (t *Target) RunDeps() {
_ = t.RunDepsWithContext(false, false)
}
func (t *Target) RunDepsWithContext(verbose, dryRun bool) error {
deps := t.Deps
for _, dep := range deps {
// if dep is file
if strings.Contains(dep, ".") {
// TODO: Handle file dependencies
if verbose {
fmt.Printf("Checking file dependency: %s\n", dep)
}
} else {
if err := runTargetWithContext(dep, verbose, dryRun); err != nil {
return err
}
}
}
return nil
}
func (c *Config) RunPrologue() {
_ = c.RunPrologueWithContext(false, false)
}
func (c *Config) RunPrologueWithContext(verbose, dryRun bool) error {
if err := c.Prologue.RunDepsWithContext(verbose, dryRun); err != nil {
return err
}
return ExecuteAllWithContext("prologue", &c.Prologue, verbose, dryRun)
}
func (c *Config) RunEpilogue() {
_ = c.RunEpilogueWithContext(false, false)
}
func (c *Config) RunEpilogueWithContext(verbose, dryRun bool) error {
if err := c.Epilogue.RunDepsWithContext(verbose, dryRun); err != nil {
return err
}
return ExecuteAllWithContext("epilogue", &c.Epilogue, verbose, dryRun)
}
func RunTarget(name string) {
_ = runTargetWithContext(name, false, false)
}
func runTargetWithContext(name string, verbose, dryRun bool) error {
target := GetTarget(name)
if err := target.RunDepsWithContext(verbose, dryRun); err != nil {
return err
}
if target.Run == nil && target.Deps == nil {
return orpheus.NotFoundError(name, fmt.Sprintf("target '%s' not found", name))
}
return ExecuteAllWithContext(name, &target, verbose, dryRun)
}
// Context-aware wrapper functions
func runPrologueWithContext(verbose, dryRun bool) error {
return cfg.RunPrologueWithContext(verbose, dryRun)
}
func runEpilogueWithContext(verbose, dryRun bool) error {
return cfg.RunEpilogueWithContext(verbose, dryRun)
}
func listTargets(format string) error {
switch format {
case "json":
return listTargetsJSON()
case "yaml":
return listTargetsYAML()
default: // table
return listTargetsTable()
}
}
func listTargetsTable() error {
fmt.Println("Available targets:")
fmt.Println("------------------")
if len(cfg.Targets) == 0 {
fmt.Println("No targets found")
return nil
}
// Find max name length for formatting
maxNameLen := 0
for name := range cfg.Targets {
if len(name) > maxNameLen {
maxNameLen = len(name)
}
}
// Print targets
for name, target := range cfg.Targets {
padding := strings.Repeat(" ", maxNameLen-len(name)+2)
deps := ""
if len(target.Deps) > 0 {
deps = fmt.Sprintf(" (depends: %s)", strings.Join(target.Deps, ", "))
}
fmt.Printf(" %s%s%d commands%s\n", name, padding, len(target.Run), deps)
}
fmt.Printf("\nTotal: %d targets\n", len(cfg.Targets))
return nil
}
func listTargetsJSON() error {
type TargetInfo struct {
Name string `json:"name"`
Commands int `json:"commands"`
Deps []string `json:"dependencies,omitempty"`
}
var targets []TargetInfo
for name, target := range cfg.Targets {
targets = append(targets, TargetInfo{
Name: name,
Commands: len(target.Run),
Deps: target.Deps,
})
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
return encoder.Encode(map[string]interface{}{
"targets": targets,
"total": len(targets),
})
}
func listTargetsYAML() error {
type TargetInfo struct {
Name string `yaml:"name"`
Commands int `yaml:"commands"`
Deps []string `yaml:"dependencies,omitempty"`
}
var targets []TargetInfo
for name, target := range cfg.Targets {
targets = append(targets, TargetInfo{
Name: name,
Commands: len(target.Run),
Deps: target.Deps,
})
}
encoder := yaml.NewEncoder(os.Stdout)
defer func() { _ = encoder.Close() }()
return encoder.Encode(map[string]interface{}{
"targets": targets,
"total": len(targets),
})
}