Package sh
Package go-sh is intended to make shell call with golang more easily.
Some usage is more similar to os/exec, eg: Run(), Output(), Command(name, args...)
But with these similar function, pipe is added in and this package also got shell-session support.
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.
// just like os/exec
sh.Command("echo", "hello").Run()
// support pipe
sh.Command("echo", "hello").Command("wc", "-c").Run()
// create a session to store dir and env
sh.NewSession().SetDir("/").Command("pwd")
// shell buildin command - "test"
sh.Test("dir", "mydir")
// like shell call: (cd /; pwd)
sh.Command("pwd", sh.Dir("/")) same with sh.Command(sh.Dir("/"), "pwd")
// output to json and xml easily
v := map[string] int {}
err = sh.Command("echo", `{"number": 1}`).UnmarshalJSON(&v)
- Variables
- func Go(f func() error) chan error
- func Test(exp string, arg string) bool
- type Dir
- type Session
- func Command(name string, a ...interface{}) *Session
- func Echo(in string) *Session
- func InteractiveSession() *Session
- func NewSession() *Session
- func (s *Session) Alias(alias, cmd string, args ...string)
- func (s *Session) Call(name string, a ...interface{}) error
- func (s *Session) CombinedOutput() (out []byte, err error)
- func (s *Session) Command(name string, a ...interface{}) *Session
- func (s *Session) Getwd() string
- func (s *Session) Kill(sig os.Signal)
- func (s *Session) Output() (out []byte, err error)
- func (s *Session) Run() (err error)
- func (s *Session) SetDir(dir string) *Session
- func (s *Session) SetEnv(key, value string) *Session
- func (s *Session) SetInput(in string) *Session
- func (s *Session) SetStdin(r io.Reader) *Session
- func (s *Session) SetTimeout(d time.Duration) *Session
- func (s *Session) Start() (err error)
- func (s *Session) Test(expression string, argument string) bool
- func (s *Session) UnmarshalJSON(data interface{}) (err error)
- func (s *Session) UnmarshalXML(data interface{}) (err error)
- func (s *Session) Wait() error
- func (s *Session) WaitTimeout(timeout time.Duration) (err error)
- func (s *Session) WriteStdout(f string) error
Package files
pipe.go
sh.go
test.go
Variables
var ErrExecTimeout = errors.New("execute timeout")
func Go(f func() error) chan error
func Test(exp string, arg string) bool
expression can be d,dir, f,file, link
▾ Example
Code:
if sh.Test("dir", "mydir") {
fmt.Println("mydir exists")
}
type Dir string
type Session struct {
Env map[string]string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
ShowCMD bool
PipeFail bool
PipeStdErrors bool
}
func Command(name string, a ...interface{}) *Session
▾ Example
Code:
out, err := sh.Command("echo", "hello").Output()
fmt.Println(string(out), err)
func Echo(in string) *Session
func InteractiveSession() *Session
func NewSession() *Session
func (*Session) Alias
¶
func (s *Session) Alias(alias, cmd string, args ...string)
▾ Example
Code:
s := NewSession()
s.Alias("alias_echo_hello", "echo", "hello")
out, err := s.Command("alias_echo_hello", "world").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
Output:
hello world
func (*Session) Call
¶
func (s *Session) Call(name string, a ...interface{}) error
combine Command and Run
func (s *Session) CombinedOutput() (out []byte, err error)
func (*Session) Command
¶
func (s *Session) Command(name string, a ...interface{}) *Session
▾ Example
Code:
s := NewSession()
out, err := s.Command("echo", "hello").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
Output:
hello
▾ Example (Pipe)
Code:
s := NewSession()
out, err := s.Command("echo", "hello", "world").Command("awk", "{print $2}").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
Output:
world
func (*Session) Getwd
¶
func (s *Session) Getwd() string
func (*Session) Kill
¶
func (s *Session) Kill(sig os.Signal)
func (*Session) Output
¶
func (s *Session) Output() (out []byte, err error)
func (*Session) Run
¶
func (s *Session) Run() (err error)
func (*Session) SetDir
¶
func (s *Session) SetDir(dir string) *Session
func (*Session) SetEnv
¶
func (s *Session) SetEnv(key, value string) *Session
func (s *Session) SetInput(in string) *Session
func (s *Session) SetStdin(r io.Reader) *Session
func (s *Session) SetTimeout(d time.Duration) *Session
func (*Session) Start
¶
func (s *Session) Start() (err error)
start command
func (*Session) Test
¶
func (s *Session) Test(expression string, argument string) bool
expression can be dir, file, link
func (s *Session) UnmarshalJSON(data interface{}) (err error)
unmarshal shell output to decode json
func (s *Session) UnmarshalXML(data interface{}) (err error)
unmarshal command output into xml
func (*Session) Wait
¶
func (s *Session) Wait() error
Should be call after Start()
only catch the last command error
func (s *Session) WaitTimeout(timeout time.Duration) (err error)
func (s *Session) WriteStdout(f string) error
Subdirectories