Created
July 8, 2014 13:19
-
-
Save reedobrien/fdaa1db0bc80a00061c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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