Skip to content

Instantly share code, notes, and snippets.

@reedobrien
Created July 8, 2014 13:19
Show Gist options
  • Save reedobrien/fdaa1db0bc80a00061c7 to your computer and use it in GitHub Desktop.
Save reedobrien/fdaa1db0bc80a00061c7 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
)
func Run(args ...string) {
args = strings.Split(strings.Join(args, " "), " ")
c := exec.Command(args[0], args[1:]...)
stderr, err := c.StderrPipe()
if err != nil {
log.Println(err)
return
}
stdout, err := c.StdoutPipe()
if err != nil {
log.Println(err)
return
}
if err := c.Start(); err != nil {
log.Println(err)
return
}
go Scan(stderr, os.Stderr)
Scan(stdout, os.Stdout)
}
func Scan(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)
for scanner.Scan() {
fmt.Fprintf(out, "%s\n", scanner.Text())
}
}
func main() {
Run("date")
Run("pwd")
Run("ping", "-V")
Run("echo")
Run("ping", "-c 3 www.google.com")
Run("echo")
Run("ping", "-c", "3", "www.yahoo.com")
Run("ping -c 3 www.archlinux.com")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment