sh

package module
v0.0.0-...-04434d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2014 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-sh

Go Walker

So what is go-sh. Sometimes we need to write some shell scripts, but shell scripts is not good at cross platform, but golang is good at that. Is there a good way to use golang to write scripts like shell? Use go-sh we can do it now.

go-sh support some shell futures.

  • shell session
  • export: env
  • alias: like alias ll='ls -l'
  • cd: remember current dir
  • pipe

Example is always important. I will show you how to use it.

First give you a full example, I will explain every command below.

session := sh.NewSession()
session.Env["PATH"] = "/usr/bin:/bin"
session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Alias("ll", "ls", "-l")
var err error
err = session.Call("ll", []string{"/"})
if err != nil {
	log.Fatal(err)
}
ret, err := session.Capture("pwd", sh.Dir("/home")) # wraper of session.Call
if err != nil {
	log.Fatal(err)
}
# ret is "/home\n"
fmt.Println(ret)

create a new Session

session := sh.NewSession()

use alias like this

session.Alias("ll", "ls", "-l") # like alias ll='ls -l'

set current env like this

session.Env["BUILD_ID"] = "123" # like export BUILD_ID=123

set current directory

session.Set(sh.Dir("/")) # like cd /

empty args filled in Call will call last command

session.Call() # will call echo hi again

pipe is also supported

session.Command("echo", []string{"hello\tworld"}).Command("cut", []string{"-f2"})
// output should be "world"
session.Run()

with Alias Env Set Call Capture Command a shell scripts can be easily converted into golang program. below is a shell script.

#!/bin/bash -
#
export PATH=/usr/bin:/bin
alias ll='ls -l'
ll | awk '{print $1}' | grep "^-rw"

convert to golang, will be

s := sh.NewSession()
s.Env["PATH"] = "/usr/bin:/bin"
s.Alias("ll", "ls", "-l")
s.Command("ll").Command("awk", []string{"'{print $1}'"}).Command("grep", []string{"^-rw"}).Run()

contribute

If you love this project, star it which will encourage the coder. pull requests are welcomed, if you want to add some new fetures.

thanks

this project is based on http://github.com/codegangsta/inject. thanks for the author.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

type Dir string

type Return

type Return struct {
	Stdout string
	Stderr string
}

func Capture

func Capture(a ...interface{}) (ret *Return, err error)

func (*Return) String

func (r *Return) String() string

func (*Return) Trim

func (r *Return) Trim() string

type Session

type Session struct {
	Env    map[string]string
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

func NewSession

func NewSession(a ...interface{}) *Session

func (*Session) Alias

func (s *Session) Alias(alias, cmd string, args ...string)

func (*Session) Call

func (s *Session) Call(a ...interface{}) error

func (*Session) Capture

func (s *Session) Capture(a ...interface{}) (ret *Return, err error)

func (*Session) Command

func (s *Session) Command(a ...interface{}) *Session

func (*Session) Output

func (s *Session) Output() (out string, err error)

func (*Session) Run

func (s *Session) Run() (err error)

func (*Session) Set

func (s *Session) Set(a ...interface{}) *Session

func (*Session) Start

func (s *Session) Start() (err error)

func (*Session) Wait

func (s *Session) Wait() (err error)

Should be call after Start()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL