I have a function like below to execute a command and show output to stdout (terminal):
package main
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
import (
"bytes"
"io"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("./test.sh")
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
}
And this is the bash script to test:
#!/bin/bash
declare -a arr=("frontend" "backend" "middleware" "apigateway")
for service in "${arr[@]}"
do
sleep 1
echo $service-prod-secret.yaml
done
For now, I only have the output when the command run print out to the os.Stdout (terminal), but I want to get this output line by line and push to some where (EX: socket) to show progress of executed command, because I have a command take long time to finish, it 's hard to only see the white screen and have no idea about the progress of the command.
Output:
frontend-prod-secret.yaml
backend-prod-secret.yaml
middleware-prod-secret.yaml
apigateway-prod-secret.yaml
out:
frontend-prod-secret.yaml
backend-prod-secret.yaml
middleware-prod-secret.yaml
apigateway-prod-secret.yaml
I need to wait the command to finish to get the output, but this take long time.
How can I reference the output line by line (in real time) to push to some where I want?