|
|
|
|
@@ -1,7 +1,7 @@
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"bufio"
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
@@ -29,9 +29,8 @@ func walkFolder(folderPath string, ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
// Check if filename starts with "oc-"
|
|
|
|
|
if strings.HasPrefix(file.Name(), "oc-") {
|
|
|
|
|
if strings.HasPrefix(file.Name(), "oc-") && (!strings.Contains(file.Name(), ".") || strings.Contains(file.Name(), ".exe")) {
|
|
|
|
|
// Prepare the command to start the process
|
|
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go startProcess(ctx, file.Name(), wg)
|
|
|
|
|
}
|
|
|
|
|
@@ -47,38 +46,70 @@ func walkFolder(folderPath string, ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// startProcess runs the given program and stores a reference to it for later control
|
|
|
|
|
func startProcess(ctx context.Context, program string, wg *sync.WaitGroup, args ...string) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
// Create log file
|
|
|
|
|
logFile, err := os.Create(program + ".log")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Failed to create log file: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, program, args...)
|
|
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
|
processes = append(processes, cmd)
|
|
|
|
|
mu.Unlock()
|
|
|
|
|
|
|
|
|
|
var outBuf, errBuf bytes.Buffer
|
|
|
|
|
cmd.Stdout = &outBuf
|
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
|
err := cmd.Start()
|
|
|
|
|
// Create pipes to capture the output in real-time
|
|
|
|
|
stdoutPipe, err := cmd.StdoutPipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Failed to get stdout pipe: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stderrPipe, err := cmd.StderrPipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Failed to get stderr pipe: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start the command
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
|
log.Printf("Failed to start process: %s", program)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("Started process: %s", program)
|
|
|
|
|
|
|
|
|
|
fmt.Print(outBuf.String())
|
|
|
|
|
fmt.Print(errBuf.String())
|
|
|
|
|
// Create goroutines to read stdout and stderr concurrently
|
|
|
|
|
go func() {
|
|
|
|
|
scanner := bufio.NewScanner(stdoutPipe)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
fmt.Fprintln(logFile, scanner.Text()) // Print each line from stdout as it arrives
|
|
|
|
|
}
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
|
log.Printf("Error reading stdout: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
err = cmd.Wait()
|
|
|
|
|
if err != nil {
|
|
|
|
|
go func() {
|
|
|
|
|
scanner := bufio.NewScanner(stderrPipe)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
fmt.Fprintln(logFile, scanner.Text()) // Print each line from stderr as it arrives
|
|
|
|
|
}
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
|
log.Printf("Error reading stderr: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Wait for the process to finish
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
|
log.Printf("Process finished with error: %v", err)
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Process finished successfully: %s", program)
|
|
|
|
|
}
|
|
|
|
|
fmt.Print(outBuf.String())
|
|
|
|
|
fmt.Print(errBuf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stopAllProcesses stops all the started processes
|
|
|
|
|
|