├── testdata ├── hello.txt ├── executable └── linkfile ├── go.mod ├── example ├── less │ └── less.go ├── tail │ └── tailf.go ├── timeout │ └── timeout.go └── example1.go ├── go.sum ├── example_test.go ├── wercker.yml ├── test.go ├── test_test.go ├── OLD_README.md ├── sh_test.go ├── pipe_test.go ├── README.md ├── pipe.go ├── sh.go └── LICENSE /testdata/hello.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /testdata/executable: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /testdata/linkfile: -------------------------------------------------------------------------------- 1 | hello.txt -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/codeskyblue/go-sh 2 | 3 | go 1.12 4 | 5 | require github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 6 | -------------------------------------------------------------------------------- /example/less/less.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/codeskyblue/go-sh" 4 | 5 | func main() { 6 | sh.Command("less", "less.go").Run() 7 | } 8 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q= 2 | github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= 3 | -------------------------------------------------------------------------------- /example/tail/tailf.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | 7 | "github.com/codeskyblue/go-sh" 8 | ) 9 | 10 | func main() { 11 | flag.Parse() 12 | if flag.NArg() != 1 { 13 | fmt.Println("Usage: PROGRAM ") 14 | return 15 | } 16 | sh.Command("tail", "-f", flag.Arg(0)).Run() 17 | } 18 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | package sh_test 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/codeskyblue/go-sh" 7 | ) 8 | 9 | func ExampleCommand() { 10 | out, err := sh.Command("echo", "hello").Output() 11 | fmt.Println(string(out), err) 12 | } 13 | 14 | func ExampleCommandPipe() { 15 | out, err := sh.Command("echo", "-n", "hi").Command("wc", "-c").Output() 16 | fmt.Println(string(out), err) 17 | } 18 | 19 | func ExampleCommandSetDir() { 20 | out, err := sh.Command("pwd", sh.Dir("/")).Output() 21 | fmt.Println(string(out), err) 22 | } 23 | 24 | func ExampleTest() { 25 | if sh.Test("dir", "mydir") { 26 | fmt.Println("mydir exists") 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /example/timeout/timeout.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | sh "github.com/codeskyblue/go-sh" 8 | ) 9 | 10 | func main() { 11 | c := sh.Command("sleep", "3") 12 | c.Start() 13 | err := c.WaitTimeout(time.Second * 1) 14 | if err != nil { 15 | fmt.Printf("timeout should happend: %v\n", err) 16 | } 17 | // timeout should be a session 18 | out, err := sh.Command("sleep", "2").SetTimeout(time.Second).Output() 19 | fmt.Printf("output:(%s), err(%v)\n", string(out), err) 20 | 21 | out, err = sh.Command("echo", "hello").SetTimeout(time.Second).Output() 22 | fmt.Printf("output:(%s), err(%v)\n", string(out), err) 23 | } 24 | -------------------------------------------------------------------------------- /wercker.yml: -------------------------------------------------------------------------------- 1 | box: wercker/golang 2 | # Build definition 3 | build: 4 | # The steps that will be executed on build 5 | steps: 6 | # Sets the go workspace and places you package 7 | # at the right place in the workspace tree 8 | - setup-go-workspace 9 | 10 | # Gets the dependencies 11 | - script: 12 | name: go get 13 | code: | 14 | cd $WERCKER_SOURCE_DIR 15 | go version 16 | go get -t . 17 | 18 | # Build the project 19 | - script: 20 | name: go build 21 | code: | 22 | go build . 23 | 24 | # Test the project 25 | - script: 26 | name: go test 27 | code: | 28 | go test -v ./... 29 | -------------------------------------------------------------------------------- /example/example1.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | 7 | "github.com/codeskyblue/go-sh" 8 | ) 9 | 10 | func main() { 11 | sh.Command("echo", "hello").Run() 12 | out, err := sh.Command("echo", "hello").Output() 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Println("output is", string(out)) 17 | 18 | var a int 19 | sh.Command("echo", "2").UnmarshalJSON(&a) 20 | fmt.Println("a =", a) 21 | 22 | s := sh.NewSession() 23 | s.Alias("hi", "echo", "hi") 24 | s.Command("hi", "boy").Run() 25 | 26 | fmt.Print("pwd = ") 27 | s.Command("pwd", sh.Dir("/")).Run() 28 | 29 | if !sh.Test("dir", "data") { 30 | sh.Command("echo", "mkdir", "data").Run() 31 | } 32 | 33 | sh.Command("echo", "hello", "world"). 34 | Command("awk", `{print "second arg is "$2}`).Run() 35 | s.ShowCMD = true 36 | s.Command("echo", "hello", "world"). 37 | Command("awk", `{print "second arg is "$2}`).Run() 38 | 39 | s.SetEnv("BUILD_ID", "123").Command("bash", "-c", "echo $BUILD_ID").Run() 40 | s.Command("bash", "-c", "echo current shell is $SHELL").Run() 41 | } 42 | -------------------------------------------------------------------------------- /test.go: -------------------------------------------------------------------------------- 1 | package sh 2 | 3 | import ( 4 | "os" 5 | "path/filepath" 6 | ) 7 | 8 | func filetest(name string, modemask os.FileMode) (match bool, err error) { 9 | fi, err := os.Stat(name) 10 | if err != nil { 11 | return 12 | } 13 | match = (fi.Mode() & modemask) == modemask 14 | return 15 | } 16 | 17 | func (s *Session) Getwd() string { 18 | dir := string(s.dir) 19 | if dir == "" { 20 | dir, _ = os.Getwd() 21 | } 22 | return dir 23 | } 24 | 25 | func (s *Session) abspath(name string) string { 26 | if filepath.IsAbs(name) { 27 | return name 28 | } 29 | return filepath.Join(s.Getwd(), name) 30 | } 31 | 32 | func init() { 33 | //log.SetFlags(log.Lshortfile | log.LstdFlags) 34 | } 35 | 36 | // expression can be dir, file, link 37 | func (s *Session) Test(expression string, argument string) bool { 38 | var err error 39 | var fi os.FileInfo 40 | fi, err = os.Lstat(s.abspath(argument)) 41 | switch expression { 42 | case "d", "dir": 43 | return err == nil && fi.IsDir() 44 | case "f", "file": 45 | return err == nil && fi.Mode().IsRegular() 46 | case "x", "executable": 47 | /* 48 | fmt.Println(expression, argument) 49 | if err == nil { 50 | fmt.Println(fi.Mode()) 51 | } 52 | */ 53 | return err == nil && fi.Mode()&os.FileMode(0100) != 0 54 | case "L", "link": 55 | return err == nil && fi.Mode()&os.ModeSymlink != 0 56 | } 57 | return false 58 | } 59 | 60 | // expression can be d,dir, f,file, link 61 | func Test(exp string, arg string) bool { 62 | s := NewSession() 63 | return s.Test(exp, arg) 64 | } 65 | -------------------------------------------------------------------------------- /test_test.go: -------------------------------------------------------------------------------- 1 | package sh_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/codeskyblue/go-sh" 7 | ) 8 | 9 | var s = sh.NewSession() 10 | 11 | type T struct{ *testing.T } 12 | 13 | func NewT(t *testing.T) *T { 14 | return &T{t} 15 | } 16 | 17 | func (t *T) checkTest(exp string, arg string, result bool) { 18 | r := s.Test(exp, arg) 19 | if r != result { 20 | t.Errorf("test -%s %s, %v != %v", exp, arg, r, result) 21 | } 22 | } 23 | 24 | func TestTest(i *testing.T) { 25 | t := NewT(i) 26 | t.checkTest("d", "../go-sh", true) 27 | t.checkTest("d", "./yymm", false) 28 | 29 | // file test 30 | t.checkTest("f", "testdata/hello.txt", true) 31 | t.checkTest("f", "testdata/xxxxx", false) 32 | t.checkTest("f", "testdata/yymm", false) 33 | 34 | // link test 35 | t.checkTest("link", "testdata/linkfile", true) 36 | t.checkTest("link", "testdata/xxxxxlinkfile", false) 37 | t.checkTest("link", "testdata/hello.txt", false) 38 | 39 | // executable test 40 | t.checkTest("x", "testdata/executable", true) 41 | t.checkTest("x", "testdata/xxxxx", false) 42 | t.checkTest("x", "testdata/hello.txt", false) 43 | } 44 | 45 | func ExampleShellTest(t *testing.T) { 46 | // test -L 47 | sh.Test("link", "testdata/linkfile") 48 | sh.Test("L", "testdata/linkfile") 49 | // test -f 50 | sh.Test("file", "testdata/file") 51 | sh.Test("f", "testdata/file") 52 | // test -x 53 | sh.Test("executable", "testdata/binfile") 54 | sh.Test("x", "testdata/binfile") 55 | // test -d 56 | sh.Test("dir", "testdata/dir") 57 | sh.Test("d", "testdata/dir") 58 | } 59 | -------------------------------------------------------------------------------- /OLD_README.md: -------------------------------------------------------------------------------- 1 | ## OLD README 2 | First give you a full example, I will explain every command below. 3 | 4 | session := sh.NewSession() 5 | session.Env["PATH"] = "/usr/bin:/bin" 6 | session.Stdout = os.Stdout 7 | session.Stderr = os.Stderr 8 | session.Alias("ll", "ls", "-l") 9 | session.ShowCMD = true // enable for debug 10 | var err error 11 | err = session.Call("ll", "/") 12 | if err != nil { 13 | log.Fatal(err) 14 | } 15 | ret, err := session.Capture("pwd", sh.Dir("/home")) # wraper of session.Call 16 | if err != nil { 17 | log.Fatal(err) 18 | } 19 | # ret is "/home\n" 20 | fmt.Println(ret) 21 | 22 | create a new Session 23 | 24 | session := sh.NewSession() 25 | 26 | use alias like this 27 | 28 | session.Alias("ll", "ls", "-l") # like alias ll='ls -l' 29 | 30 | set current env like this 31 | 32 | session.Env["BUILD_ID"] = "123" # like export BUILD_ID=123 33 | 34 | set current directory 35 | 36 | session.Set(sh.Dir("/")) # like cd / 37 | 38 | pipe is also supported 39 | 40 | session.Command("echo", "hello\tworld").Command("cut", "-f2") 41 | // output should be "world" 42 | session.Run() 43 | 44 | test, the build in command support 45 | 46 | session.Test("d", "dir") // test dir 47 | session.Test("f", "file) // test regular file 48 | 49 | with `Alias Env Set Call Capture Command` a shell scripts can be easily converted into golang program. below is a shell script. 50 | 51 | #!/bin/bash - 52 | # 53 | export PATH=/usr/bin:/bin 54 | alias ll='ls -l' 55 | cd /usr 56 | if test -d "local" 57 | then 58 | ll local | awk '{print $1, $NF}' 59 | fi 60 | 61 | convert to golang, will be 62 | 63 | s := sh.NewSession() 64 | s.Env["PATH"] = "/usr/bin:/bin" 65 | s.Set(sh.Dir("/usr")) 66 | s.Alias("ll", "ls", "-l") 67 | if s.Test("d", "local") { 68 | s.Command("ll", "local").Command("awk", "{print $1, $NF}").Run() 69 | } 70 | -------------------------------------------------------------------------------- /sh_test.go: -------------------------------------------------------------------------------- 1 | package sh 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "runtime" 7 | "strings" 8 | "testing" 9 | ) 10 | 11 | func TestAlias(t *testing.T) { 12 | s := NewSession() 13 | s.Alias("gr", "echo", "hi") 14 | out, err := s.Command("gr", "sky").Output() 15 | if err != nil { 16 | t.Error(err) 17 | } 18 | if string(out) != "hi sky\n" { 19 | t.Errorf("expect 'hi sky' but got:%s", string(out)) 20 | } 21 | } 22 | 23 | func ExampleSession_Command() { 24 | s := NewSession() 25 | out, err := s.Command("echo", "hello").Output() 26 | if err != nil { 27 | log.Fatal(err) 28 | } 29 | fmt.Println(string(out)) 30 | // Output: hello 31 | } 32 | 33 | func ExampleSession_Command_pipe() { 34 | s := NewSession() 35 | out, err := s.Command("echo", "hello", "world").Command("awk", "{print $2}").Output() 36 | if err != nil { 37 | log.Fatal(err) 38 | } 39 | fmt.Println(string(out)) 40 | // Output: world 41 | } 42 | 43 | func ExampleSession_Alias() { 44 | s := NewSession() 45 | s.Alias("alias_echo_hello", "echo", "hello") 46 | out, err := s.Command("alias_echo_hello", "world").Output() 47 | if err != nil { 48 | log.Fatal(err) 49 | } 50 | fmt.Println(string(out)) 51 | // Output: hello world 52 | } 53 | 54 | func TestEcho(t *testing.T) { 55 | out, err := Echo("one two three").Command("wc", "-w").Output() 56 | if err != nil { 57 | t.Error(err) 58 | } 59 | if strings.TrimSpace(string(out)) != "3" { 60 | t.Errorf("expect '3' but got:%s", string(out)) 61 | } 62 | } 63 | 64 | func TestSession(t *testing.T) { 65 | if runtime.GOOS == "windows" { 66 | t.Log("ignore test on windows") 67 | return 68 | } 69 | session := NewSession() 70 | session.ShowCMD = true 71 | err := session.Call("pwd") 72 | if err != nil { 73 | t.Error(err) 74 | } 75 | out, err := session.SetDir("/").Command("pwd").Output() 76 | if err != nil { 77 | t.Error(err) 78 | } 79 | if string(out) != "/\n" { 80 | t.Errorf("expect /, but got %s", string(out)) 81 | } 82 | } 83 | 84 | /* 85 | #!/bin/bash - 86 | # 87 | export PATH=/usr/bin:/bin 88 | alias ll='ls -l' 89 | cd /usr 90 | if test -d "local" 91 | then 92 | ll local | awk '{print $1, $NF}' | grep bin 93 | fi 94 | */ 95 | func Example(t *testing.T) { 96 | s := NewSession() 97 | //s.ShowCMD = true 98 | s.Env["PATH"] = "/usr/bin:/bin" 99 | s.SetDir("/bin") 100 | s.Alias("ll", "ls", "-l") 101 | 102 | if s.Test("d", "local") { 103 | //s.Command("ll", []string{"local"}).Command("awk", []string{"{print $1, $NF}"}).Command("grep", []string{"bin"}).Run() 104 | s.Command("ll", "local").Command("awk", "{print $1, $NF}").Command("grep", "bin").Run() 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /pipe_test.go: -------------------------------------------------------------------------------- 1 | package sh 2 | 3 | import ( 4 | "encoding/xml" 5 | "io" 6 | "os" 7 | "os/exec" 8 | "strings" 9 | "testing" 10 | "time" 11 | ) 12 | 13 | func TestUnmarshalJSON(t *testing.T) { 14 | var a int 15 | s := NewSession() 16 | s.ShowCMD = true 17 | err := s.Command("echo", []string{"1"}).UnmarshalJSON(&a) 18 | if err != nil { 19 | t.Error(err) 20 | } 21 | if a != 1 { 22 | t.Errorf("expect a tobe 1, but got %d", a) 23 | } 24 | } 25 | 26 | func TestUnmarshalXML(t *testing.T) { 27 | s := NewSession() 28 | xmlSample := ` 29 | ` 30 | type server struct { 31 | XMLName xml.Name `xml:"server"` 32 | Version string `xml:"version,attr"` 33 | } 34 | data := &server{} 35 | s.Command("echo", xmlSample).UnmarshalXML(data) 36 | if data.Version != "1" { 37 | t.Error(data) 38 | } 39 | } 40 | 41 | func TestPipe(t *testing.T) { 42 | s := NewSession() 43 | s.ShowCMD = true 44 | s.Call("echo", "hello") 45 | err := s.Command("echo", "hi").Command("cat", "-n").Start() 46 | if err != nil { 47 | t.Error(err) 48 | } 49 | err = s.Wait() 50 | if err != nil { 51 | t.Error(err) 52 | } 53 | out, err := s.Command("echo", []string{"hello"}).Output() 54 | if err != nil { 55 | t.Error(err) 56 | } 57 | if string(out) != "hello\n" { 58 | t.Error("capture wrong output:", out) 59 | } 60 | s.Command("echo", []string{"hello\tworld"}).Command("cut", []string{"-f2"}).Run() 61 | } 62 | 63 | func TestPipeCommand(t *testing.T) { 64 | c1 := exec.Command("echo", "good") 65 | rd, wr := io.Pipe() 66 | c1.Stdout = wr 67 | c2 := exec.Command("cat", "-n") 68 | c2.Stdout = os.Stdout 69 | c2.Stdin = rd 70 | c1.Start() 71 | c2.Start() 72 | 73 | c1.Wait() 74 | wc, ok := c1.Stdout.(io.WriteCloser) 75 | if ok { 76 | wc.Close() 77 | } 78 | c2.Wait() 79 | } 80 | 81 | func TestPipeInput(t *testing.T) { 82 | s := NewSession() 83 | s.ShowCMD = true 84 | s.SetInput("first line\nsecond line\n") 85 | out, err := s.Command("grep", "second").Output() 86 | if err != nil { 87 | t.Error(err) 88 | } 89 | if string(out) != "second line\n" { 90 | t.Error("capture wrong output:", out) 91 | } 92 | } 93 | 94 | func TestTimeout(t *testing.T) { 95 | s := NewSession() 96 | err := s.Command("sleep", "2").Start() 97 | if err != nil { 98 | t.Fatal(err) 99 | } 100 | err = s.WaitTimeout(time.Second) 101 | if err != ErrExecTimeout { 102 | t.Fatal(err) 103 | } 104 | } 105 | 106 | func TestSetTimeout(t *testing.T) { 107 | s := NewSession() 108 | s.SetTimeout(time.Second) 109 | defer s.SetTimeout(0) 110 | err := s.Command("sleep", "2").Run() 111 | if err != ErrExecTimeout { 112 | t.Fatal(err) 113 | } 114 | } 115 | 116 | func TestCombinedOutput(t *testing.T) { 117 | s := NewSession() 118 | bytes, err := s.Command("sh", "-c", "echo stderr >&2 ; echo stdout").CombinedOutput() 119 | if err != nil { 120 | t.Error(err) 121 | } 122 | stringOutput := string(bytes) 123 | if !(strings.Contains(stringOutput, "stdout") && strings.Contains(stringOutput, "stderr")) { 124 | t.Errorf("expect output from both output streams, got '%s'", strings.TrimSpace(stringOutput)) 125 | } 126 | } 127 | 128 | func TestPipeFail(t *testing.T) { 129 | sh := NewSession() 130 | sh.PipeFail = true 131 | sh.PipeStdErrors = true 132 | sh.Command("cat", "unknown-file") 133 | sh.Command("echo") 134 | if _, err := sh.Output(); err == nil { 135 | t.Error("expected error") 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## go-sh 2 | [![wercker status](https://app.wercker.com/status/009acbd4f00ccc6de7e2554e12a50d84/s "wercker status")](https://app.wercker.com/project/bykey/009acbd4f00ccc6de7e2554e12a50d84) 3 | [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/codeskyblue/go-sh) 4 | 5 | *If you depend on the old api, see tag: v.0.1* 6 | 7 | install: `go get github.com/codeskyblue/go-sh` 8 | 9 | Pipe Example: 10 | 11 | package main 12 | 13 | import "github.com/codeskyblue/go-sh" 14 | 15 | func main() { 16 | sh.Command("echo", "hello\tworld").Command("cut", "-f2").Run() 17 | } 18 | 19 | Because I like os/exec, `go-sh` is very much modelled after it. However, `go-sh` provides a better experience. 20 | 21 | These are some of its features: 22 | 23 | * keep the variable environment (e.g. export) 24 | * alias support (e.g. alias in shell) 25 | * remember current dir 26 | * pipe command 27 | * shell build-in commands echo & test 28 | * timeout support 29 | 30 | Examples are important: 31 | 32 | sh: echo hello 33 | go: sh.Command("echo", "hello").Run() 34 | 35 | sh: export BUILD_ID=123 36 | go: s = sh.NewSession().SetEnv("BUILD_ID", "123") 37 | 38 | sh: alias ll='ls -l' 39 | go: s = sh.NewSession().Alias('ll', 'ls', '-l') 40 | 41 | sh: (cd /; pwd) 42 | go: sh.Command("pwd", sh.Dir("/")).Run() 43 | 44 | sh: test -d data || mkdir data 45 | go: if ! sh.Test("dir", "data") { sh.Command("mkdir", "data").Run() } 46 | 47 | sh: cat first second | awk '{print $1}' 48 | go: sh.Command("cat", "first", "second").Command("awk", "{print $1}").Run() 49 | 50 | sh: count=$(echo "one two three" | wc -w) 51 | go: count, err := sh.Echo("one two three").Command("wc", "-w").Output() 52 | 53 | sh(in ubuntu): timeout 1s sleep 3 54 | go: c := sh.Command("sleep", "3"); c.Start(); c.WaitTimeout(time.Second) # default SIGKILL 55 | go: out, err := sh.Command("sleep", "3").SetTimeout(time.Second).Output() # set session timeout and get output) 56 | 57 | sh: echo hello | cat 58 | go: out, err := sh.Command("cat").SetInput("hello").Output() 59 | 60 | sh: cat # read from stdin 61 | go: out, err := sh.Command("cat").SetStdin(os.Stdin).Output() 62 | 63 | sh: ls -l > /tmp/listing.txt # write stdout to file 64 | go: err := sh.Command("ls", "-l").WriteStdout("/tmp/listing.txt") 65 | 66 | If you need to keep env and dir, it is better to create a session 67 | 68 | session := sh.NewSession() 69 | session.SetEnv("BUILD_ID", "123") 70 | session.SetDir("/") 71 | # then call cmd 72 | session.Command("echo", "hello").Run() 73 | # set ShowCMD to true for easily debug 74 | session.ShowCMD = true 75 | 76 | By default, pipeline returns error only if the last command exit with a non-zero status. However, you can also enable `pipefail` option like `bash`. In that case, pipeline returns error if any of the commands fail and for multiple failed commands, it returns the error of rightmost failed command. 77 | 78 | session := sh.NewSession() 79 | session.PipeFail = true 80 | session.Command("cat", "unknown-file").Command("echo").Run() 81 | 82 | By default, pipelines's std-error is set to last command's std-error. However, you can also combine std-errors of all commands into pipeline's std-error using `session.PipeStdErrors = true`. 83 | 84 | for more information, it better to see docs. 85 | [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/codeskyblue/go-sh) 86 | 87 | ### contribute 88 | If you love this project, starring it will encourage the coder. Pull requests are welcome. 89 | 90 | support the author: [alipay](https://me.alipay.com/goskyblue) 91 | 92 | ### thanks 93 | this project is based on . thanks for the author. 94 | 95 | # the reason to use Go shell 96 | Sometimes we need to write shell scripts, but shell scripts are not good at working cross platform, Go, on the other hand, is good at that. Is there a good way to use Go to write shell like scripts? Using go-sh we can do this now. 97 | -------------------------------------------------------------------------------- /pipe.go: -------------------------------------------------------------------------------- 1 | package sh 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "encoding/xml" 7 | "errors" 8 | "io" 9 | "os" 10 | "strings" 11 | "syscall" 12 | "time" 13 | ) 14 | 15 | var ErrExecTimeout = errors.New("execute timeout") 16 | 17 | // unmarshal shell output to decode json 18 | func (s *Session) UnmarshalJSON(data interface{}) (err error) { 19 | bufrw := bytes.NewBuffer(nil) 20 | s.Stdout = bufrw 21 | if err = s.Run(); err != nil { 22 | return 23 | } 24 | return json.NewDecoder(bufrw).Decode(data) 25 | } 26 | 27 | // unmarshal command output into xml 28 | func (s *Session) UnmarshalXML(data interface{}) (err error) { 29 | bufrw := bytes.NewBuffer(nil) 30 | s.Stdout = bufrw 31 | if err = s.Run(); err != nil { 32 | return 33 | } 34 | return xml.NewDecoder(bufrw).Decode(data) 35 | } 36 | 37 | // start command 38 | func (s *Session) Start() (err error) { 39 | s.started = true 40 | var rd *io.PipeReader 41 | var wr *io.PipeWriter 42 | var length = len(s.cmds) 43 | if s.ShowCMD { 44 | var cmds = make([]string, 0, 4) 45 | for _, cmd := range s.cmds { 46 | cmds = append(cmds, strings.Join(cmd.Args, " ")) 47 | } 48 | s.writePrompt(strings.Join(cmds, " | ")) 49 | } 50 | for index, cmd := range s.cmds { 51 | if index == 0 { 52 | cmd.Stdin = s.Stdin 53 | } else { 54 | cmd.Stdin = rd 55 | } 56 | if index != length { 57 | rd, wr = io.Pipe() // create pipe 58 | cmd.Stdout = wr 59 | if s.PipeStdErrors { 60 | cmd.Stderr = s.Stderr 61 | } else { 62 | cmd.Stderr = os.Stderr 63 | } 64 | } 65 | if index == length-1 { 66 | cmd.Stdout = s.Stdout 67 | cmd.Stderr = s.Stderr 68 | } 69 | err = cmd.Start() 70 | if err != nil { 71 | return 72 | } 73 | } 74 | return 75 | } 76 | 77 | // Should be call after Start() 78 | // only catch the last command error 79 | func (s *Session) Wait() error { 80 | var pipeErr, lastErr error 81 | for _, cmd := range s.cmds { 82 | if lastErr = cmd.Wait(); lastErr != nil { 83 | pipeErr = lastErr 84 | } 85 | wr, ok := cmd.Stdout.(*io.PipeWriter) 86 | if ok { 87 | wr.Close() 88 | } 89 | } 90 | if s.PipeFail { 91 | return pipeErr 92 | } 93 | return lastErr 94 | } 95 | 96 | func (s *Session) Kill(sig os.Signal) { 97 | for _, cmd := range s.cmds { 98 | if cmd.Process != nil { 99 | cmd.Process.Signal(sig) 100 | } 101 | } 102 | } 103 | 104 | func (s *Session) WaitTimeout(timeout time.Duration) (err error) { 105 | select { 106 | case <-time.After(timeout): 107 | s.Kill(syscall.SIGKILL) 108 | return ErrExecTimeout 109 | case err = <-Go(s.Wait): 110 | return err 111 | } 112 | } 113 | 114 | func Go(f func() error) chan error { 115 | ch := make(chan error, 1) 116 | go func() { 117 | ch <- f() 118 | }() 119 | return ch 120 | } 121 | 122 | func (s *Session) Run() (err error) { 123 | if err = s.Start(); err != nil { 124 | return 125 | } 126 | if s.timeout != time.Duration(0) { 127 | return s.WaitTimeout(s.timeout) 128 | } 129 | return s.Wait() 130 | } 131 | 132 | func (s *Session) Output() (out []byte, err error) { 133 | oldout := s.Stdout 134 | defer func() { 135 | s.Stdout = oldout 136 | }() 137 | stdout := bytes.NewBuffer(nil) 138 | s.Stdout = stdout 139 | err = s.Run() 140 | out = stdout.Bytes() 141 | return 142 | } 143 | 144 | func (s *Session) WriteStdout(f string) error { 145 | oldout := s.Stdout 146 | defer func() { 147 | s.Stdout = oldout 148 | }() 149 | 150 | out, err := os.Create(f) 151 | if err != nil { 152 | return err 153 | } 154 | defer out.Close() 155 | s.Stdout = out 156 | return s.Run() 157 | } 158 | 159 | func (s *Session) CombinedOutput() (out []byte, err error) { 160 | oldout := s.Stdout 161 | olderr := s.Stderr 162 | defer func() { 163 | s.Stdout = oldout 164 | s.Stderr = olderr 165 | }() 166 | stdout := bytes.NewBuffer(nil) 167 | s.Stdout = stdout 168 | s.Stderr = stdout 169 | 170 | err = s.Run() 171 | out = stdout.Bytes() 172 | return 173 | } 174 | -------------------------------------------------------------------------------- /sh.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package go-sh is intended to make shell call with golang more easily. 3 | Some usage is more similar to os/exec, eg: Run(), Output(), Command(name, args...) 4 | 5 | But with these similar function, pipe is added in and this package also got shell-session support. 6 | 7 | Why I love golang so much, because the usage of golang is simple, but the power is unlimited. I want to make this pakcage got the sample style like golang. 8 | 9 | // just like os/exec 10 | sh.Command("echo", "hello").Run() 11 | 12 | // support pipe 13 | sh.Command("echo", "hello").Command("wc", "-c").Run() 14 | 15 | // create a session to store dir and env 16 | sh.NewSession().SetDir("/").Command("pwd") 17 | 18 | // shell buildin command - "test" 19 | sh.Test("dir", "mydir") 20 | 21 | // like shell call: (cd /; pwd) 22 | sh.Command("pwd", sh.Dir("/")) same with sh.Command(sh.Dir("/"), "pwd") 23 | 24 | // output to json and xml easily 25 | v := map[string] int {} 26 | err = sh.Command("echo", `{"number": 1}`).UnmarshalJSON(&v) 27 | */ 28 | package sh 29 | 30 | import ( 31 | "fmt" 32 | "io" 33 | "os" 34 | "os/exec" 35 | "reflect" 36 | "strings" 37 | "time" 38 | 39 | "github.com/codegangsta/inject" 40 | ) 41 | 42 | type Dir string 43 | 44 | type Session struct { 45 | inj inject.Injector 46 | alias map[string][]string 47 | cmds []*exec.Cmd 48 | dir Dir 49 | started bool 50 | Env map[string]string 51 | Stdin io.Reader 52 | Stdout io.Writer 53 | Stderr io.Writer 54 | ShowCMD bool // enable for debug 55 | timeout time.Duration 56 | 57 | // additional pipe options 58 | PipeFail bool // returns error of rightmost no-zero command 59 | PipeStdErrors bool // combine std errors of all pipe commands 60 | } 61 | 62 | func (s *Session) writePrompt(args ...interface{}) { 63 | var ps1 = fmt.Sprintf("[golang-sh]$") 64 | args = append([]interface{}{ps1}, args...) 65 | fmt.Fprintln(s.Stderr, args...) 66 | } 67 | 68 | func NewSession() *Session { 69 | env := make(map[string]string) 70 | for _, key := range []string{"PATH"} { 71 | env[key] = os.Getenv(key) 72 | } 73 | s := &Session{ 74 | inj: inject.New(), 75 | alias: make(map[string][]string), 76 | dir: Dir(""), 77 | Stdin: strings.NewReader(""), 78 | Stdout: os.Stdout, 79 | Stderr: os.Stderr, 80 | Env: env, 81 | } 82 | return s 83 | } 84 | 85 | func InteractiveSession() *Session { 86 | s := NewSession() 87 | s.SetStdin(os.Stdin) 88 | return s 89 | } 90 | 91 | func Command(name string, a ...interface{}) *Session { 92 | s := NewSession() 93 | return s.Command(name, a...) 94 | } 95 | 96 | func Echo(in string) *Session { 97 | s := NewSession() 98 | return s.SetInput(in) 99 | } 100 | 101 | func (s *Session) Alias(alias, cmd string, args ...string) { 102 | v := []string{cmd} 103 | v = append(v, args...) 104 | s.alias[alias] = v 105 | } 106 | 107 | func (s *Session) Command(name string, a ...interface{}) *Session { 108 | var args = make([]string, 0) 109 | var sType = reflect.TypeOf("") 110 | 111 | // init cmd, args, dir, envs 112 | // if not init, program may panic 113 | s.inj.Map(name).Map(args).Map(s.dir).Map(map[string]string{}) 114 | for _, v := range a { 115 | switch reflect.TypeOf(v) { 116 | case sType: 117 | args = append(args, v.(string)) 118 | default: 119 | s.inj.Map(v) 120 | } 121 | } 122 | if len(args) != 0 { 123 | s.inj.Map(args) 124 | } 125 | s.inj.Invoke(s.appendCmd) 126 | return s 127 | } 128 | 129 | // combine Command and Run 130 | func (s *Session) Call(name string, a ...interface{}) error { 131 | return s.Command(name, a...).Run() 132 | } 133 | 134 | /* 135 | func (s *Session) Exec(cmd string, args ...string) error { 136 | return s.Call(cmd, args) 137 | } 138 | */ 139 | 140 | func (s *Session) SetEnv(key, value string) *Session { 141 | s.Env[key] = value 142 | return s 143 | } 144 | 145 | func (s *Session) SetDir(dir string) *Session { 146 | s.dir = Dir(dir) 147 | return s 148 | } 149 | 150 | func (s *Session) SetInput(in string) *Session { 151 | s.Stdin = strings.NewReader(in) 152 | return s 153 | } 154 | 155 | func (s *Session) SetStdin(r io.Reader) *Session { 156 | s.Stdin = r 157 | return s 158 | } 159 | 160 | func (s *Session) SetTimeout(d time.Duration) *Session { 161 | s.timeout = d 162 | return s 163 | } 164 | 165 | func newEnviron(env map[string]string, inherit bool) []string { //map[string]string { 166 | environ := make([]string, 0, len(env)) 167 | if inherit { 168 | for _, line := range os.Environ() { 169 | for k, _ := range env { 170 | if strings.HasPrefix(line, k+"=") { 171 | goto CONTINUE 172 | } 173 | } 174 | environ = append(environ, line) 175 | CONTINUE: 176 | } 177 | } 178 | for k, v := range env { 179 | environ = append(environ, k+"="+v) 180 | } 181 | return environ 182 | } 183 | 184 | func (s *Session) GetPIDs() []int { 185 | var pids = make([]int, 0) 186 | 187 | for _, cmd := range s.cmds { 188 | if cmd.Process != nil { 189 | pids = append(pids, cmd.Process.Pid) 190 | } 191 | } 192 | return pids 193 | } 194 | 195 | func (s *Session) appendCmd(cmd string, args []string, cwd Dir, env map[string]string) { 196 | if s.started { 197 | s.started = false 198 | s.cmds = make([]*exec.Cmd, 0) 199 | } 200 | for k, v := range s.Env { 201 | if _, ok := env[k]; !ok { 202 | env[k] = v 203 | } 204 | } 205 | environ := newEnviron(s.Env, true) // true: inherit sys-env 206 | v, ok := s.alias[cmd] 207 | if ok { 208 | cmd = v[0] 209 | args = append(v[1:], args...) 210 | } 211 | c := exec.Command(cmd, args...) 212 | c.Env = environ 213 | c.Dir = string(cwd) 214 | s.cmds = append(s.cmds, c) 215 | } 216 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------