Giter VIP home page Giter VIP logo

go-sh's Introduction

go-sh

Build Status 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
  • test: this is shell build command, very usefull(support -d and -f)

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()

test, the build in command support

session.Test("d", "dir") // test dir
session.Test("f", "file) // test regular file

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'
cd /usr
if test -d "local"
then
	ll local | awk '{print $1, $NF}'
fi

convert to golang, will be

s := sh.NewSession()
s.Env["PATH"] = "/usr/bin:/bin"
s.Set(sh.Dir("/usr"))
s.Alias("ll", "ls", "-l")
if s.Test("d", "local") {
	s.Command("ll", []string{"local"}).Command("awk", []string{"{print $1, $NF}"}).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.

support the author: alipay

thanks

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

go-sh's People

Contributors

codeskyblue avatar

Watchers

James Cloos avatar 狗蛋 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.