Git Product home page Git Product logo

go-sh's Introduction

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.

go-sh's People

Contributors

codeskyblue avatar

Watchers

 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.