Last active
June 1, 2017 15:36
-
-
Save lvidarte/1b7f8d7331b7f12bd0b022fee0b5f210 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 ( | |
"bytes" | |
"fmt" | |
"log" | |
"os/exec" | |
"strings" | |
) | |
type Session struct { | |
Stdout string | |
} | |
func (s *Session) Command(name string, args ...string) *Session { | |
cmd := exec.Command(name, args...) | |
cmd.Stdin = strings.NewReader(s.Stdout) | |
var out bytes.Buffer | |
cmd.Stdout = &out | |
err := cmd.Run() | |
if err != nil { | |
log.Fatal(err) | |
} | |
s.Stdin = out.String() | |
return s | |
} | |
func (s *Session) Exec() { | |
fmt.Printf(s.Stdout) | |
} | |
func main() { | |
s := Session{} | |
s.Command("ls", "-1").Command("grep", ".go").Exec() | |
} |
Author
lvidarte
commented
Jun 1, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment