Skip to content

Commit

Permalink
feat(executor): add support for logical operators in monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 4, 2024
1 parent 7f11f72 commit 7a8d45f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
51 changes: 51 additions & 0 deletions server/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,58 @@ func (wr *StderrMonitor) Write(p []byte) (n int, err error) {
return
}

type executionLogicalOp int

const (
ExecutionLogicalOpNone executionLogicalOp = 0
ExecutionLogicalOpOr executionLogicalOp = iota
ExecutionLogicalOpAnd executionLogicalOp = iota
)

var logicalOps = map[string]executionLogicalOp{
"||": ExecutionLogicalOpOr,
"&&": ExecutionLogicalOpAnd,
}

func hasLogicalOpInCommand(cmd string) string {
for op := range logicalOps {
if strings.Contains(cmd, op) {
return op
}
}
return ""
}

func Execute(workingDir string, c Collector, prog string, args ...string) (int, int, error) {
sep := hasLogicalOpInCommand(prog)
if sep != "" {
commands := strings.Split(prog, sep)
var err error
var numErrors int
var exitCode int
for i, cmd := range commands {
if i > 0 {
switch logicalOps[sep] {
case ExecutionLogicalOpOr:
if exitCode == 0 {
continue
}
case ExecutionLogicalOpAnd:
if exitCode != 0 {
continue
}
}
}

argv := strings.Split(strings.TrimSpace(cmd), " ")
numErrors, exitCode, err = Execute(workingDir, c, argv[0], argv[1:]...)
if err != nil {
return numErrors, exitCode, err
}
}
return numErrors, exitCode, nil
}

errProcessor := &StderrMonitor{
workingDir: workingDir,
collector: c,
Expand Down
5 changes: 5 additions & 0 deletions server/helpers/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,10 @@ func GetRunCommand(languageId string, filePath string) (string, error) {
)

runCommand = r.Replace(runCommand)
if strings.Count(runCommand, "||") > 0 || strings.Count(runCommand, "&&") > 0 {
// wrap the command in double quotes if it contains logical operators
runCommand = fmt.Sprintf("\"%s\"", runCommand)
}

return fmt.Sprintf("%s -- %s", executablePath, runCommand), nil
}

0 comments on commit 7a8d45f

Please sign in to comment.