Compare commits

..

No commits in common. "756ba40fbbb9a605d2612848e246e6ef71e1c31e" and "75bfbf3cffe293be92e7f512e1a64c1659366980" have entirely different histories.

4 changed files with 16 additions and 47 deletions

2
.vscode/launch.json vendored
View File

@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",
"program": "main.go", "program": "main.go",
"args": ["localbuild", "-p"] "args": ["localbuild"]
}, },
{ {
"name": "localrun", "name": "localrun",

View File

@ -37,7 +37,7 @@ to quickly create a Cobra application.`,
} else { } else {
fmt.Println("Repository deleted successfully") fmt.Println("Repository deleted successfully")
} }
err = deleteFolderIfExists(binPath) //err := deleteFolderIfExists(binPath)
if err != nil { if err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
} else { } else {

View File

@ -1,7 +1,7 @@
package cmd package cmd
import ( import (
"bufio" "bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
@ -29,8 +29,9 @@ func walkFolder(folderPath string, ctx context.Context, wg *sync.WaitGroup) {
for _, file := range files { for _, file := range files {
// Check if filename starts with "oc-" // Check if filename starts with "oc-"
if strings.HasPrefix(file.Name(), "oc-") && (!strings.Contains(file.Name(), ".") || strings.Contains(file.Name(), ".exe")) { if strings.HasPrefix(file.Name(), "oc-") {
// Prepare the command to start the process // Prepare the command to start the process
wg.Add(1) wg.Add(1)
go startProcess(ctx, file.Name(), wg) go startProcess(ctx, file.Name(), wg)
} }
@ -46,70 +47,38 @@ 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) { func startProcess(ctx context.Context, program string, wg *sync.WaitGroup, args ...string) {
defer wg.Done() 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...) cmd := exec.CommandContext(ctx, program, args...)
mu.Lock() mu.Lock()
processes = append(processes, cmd) processes = append(processes, cmd)
mu.Unlock() mu.Unlock()
// Create pipes to capture the output in real-time var outBuf, errBuf bytes.Buffer
stdoutPipe, err := cmd.StdoutPipe() cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err := cmd.Start()
if err != nil { 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) log.Printf("Failed to start process: %s", program)
return return
} }
log.Printf("Started process: %s", program) log.Printf("Started process: %s", program)
// Create goroutines to read stdout and stderr concurrently fmt.Print(outBuf.String())
go func() { fmt.Print(errBuf.String())
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)
}
}()
go func() { err = cmd.Wait()
scanner := bufio.NewScanner(stderrPipe) if err != nil {
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) log.Printf("Process finished with error: %v", err)
} else { } else {
log.Printf("Process finished successfully: %s", program) log.Printf("Process finished successfully: %s", program)
} }
fmt.Print(outBuf.String())
fmt.Print(errBuf.String())
} }
// stopAllProcesses stops all the started processes // stopAllProcesses stops all the started processes

BIN
oc-local Executable file

Binary file not shown.