package main import ( "bytes" "io" "log" "os/exec" "strings" ) // Starts the task report it's exit status // To Logger func RunJob(task *TaskManifest, deliveryUUID string, logger *log.Logger) { var stderrBuf, stdoutBuf bytes.Buffer execuatable, args := CommandStrtoProgArgs(task.Command) cmd := exec.Command(execuatable, args...) cmd.Stdout = &stdoutBuf cmd.Stderr = &stderrBuf task.Lock.Lock() err := cmd.Run() task.Lock.Unlock() if err != nil { logger.Println("Exec reported error", err.Error()) return } stdout, err := io.ReadAll(&stdoutBuf) if err != nil { logger.Printf("Could not read stdout from command for job %s\n", deliveryUUID) return } stderr, err := io.ReadAll(&stderrBuf) if err != nil { logger.Printf("Could not read stderr from command for job %s\n", deliveryUUID) return } logger.Printf("stdout: %v", string(stdout)) logger.Printf("stderr: %v", string(stderr)) logger.Printf("Job: %s exited with code: %d", deliveryUUID, cmd.ProcessState.ExitCode()) } // Seperate executable from args in command string func CommandStrtoProgArgs(cmd string) (Execuatbale string, Args []string) { tokens := strings.Split(cmd, " ") if len(tokens) == 1 { return tokens[0], nil } return tokens[0], tokens[1:] }