Running all commands in the same context

This commit is contained in:
Lilleman auf Larv 2024-02-03 10:40:54 +01:00
parent a4a2e15541
commit 0fa444ae74

36
main.go
View File

@ -156,6 +156,7 @@ func runCmd(log go_log.Log, workDir string, cmdStr string, uid int, gid int) (in
} }
// Create the command // Create the command
response += "Running command: " + cmdStr + "\n"
cmd := exec.Command("/bin/sh", "-c", cmdStr) cmd := exec.Command("/bin/sh", "-c", cmdStr)
cmd.Dir = workDir cmd.Dir = workDir
cmd.SysProcAttr = &syscall.SysProcAttr{} cmd.SysProcAttr = &syscall.SysProcAttr{}
@ -344,22 +345,25 @@ func main() {
res.Header().Set("Cache-Control", "no-cache") res.Header().Set("Cache-Control", "no-cache")
res.Header().Set("Connection", "keep-alive") res.Header().Set("Connection", "keep-alive")
for idx, command := range payload.Commands { allCommands := "true"
response += "Running command #" + fmt.Sprint(idx) + ": " + command + "\n"
exitCode, cmdRes, err := runCmd(log, workDir, command, uid, gid) for _, command := range payload.Commands {
response += cmdRes allCommands += " && " + command
if err != nil { }
response += "Command failed, err: " + err.Error() + "\n"
res.WriteHeader(400) exitCode, cmdRes, err := runCmd(log, workDir, allCommands, uid, gid)
res.Write([]byte(response)) response += cmdRes
return if err != nil {
} response += "Command failed, err: " + err.Error() + "\n"
if exitCode != 0 { res.WriteHeader(400)
response += "Command failed, non-zero exit code: " + fmt.Sprint(exitCode) + "\n" res.Write([]byte(response))
res.WriteHeader(400) return
res.Write([]byte(response)) }
return if exitCode != 0 {
} response += "Command failed, non-zero exit code: " + fmt.Sprint(exitCode) + "\n"
res.WriteHeader(400)
res.Write([]byte(response))
return
} }
// If we get here, nothing have broken down, so 200 OK and send the full response // If we get here, nothing have broken down, so 200 OK and send the full response