Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Last active June 1, 2017 15:36
Show Gist options
  • Save lvidarte/1b7f8d7331b7f12bd0b022fee0b5f210 to your computer and use it in GitHub Desktop.
Save lvidarte/1b7f8d7331b7f12bd0b022fee0b5f210 to your computer and use it in GitHub Desktop.
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()
}
@lvidarte
Copy link
Author

lvidarte commented Jun 1, 2017

$ go run sh.go
main.go
shell.go
sh.go
torrent-client.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment