Last active
May 28, 2020 09:26
-
-
Save kjk/2846af817271b7cb727962c8e556650b to your computer and use it in GitHub Desktop.
capture but also show progress example for https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html (made with https://codeeval.dev)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"os/exec" | |
"runtime" | |
) | |
func main() { | |
cmd := exec.Command("ls", "-lah") | |
if runtime.GOOS == "windows" { | |
cmd = exec.Command("tasklist") | |
} | |
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment