MCPcopy Index your code
hub / github.com/codeskyblue/go-sh

github.com/codeskyblue/go-sh @0.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.2 ↗ · + Follow
63 symbols 259 edges 11 files 9 documented · 14% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

go-sh

wercker status Go Walker

If you depend on the old api, see tag: v.0.1

install: go get github.com/codeskyblue/go-sh

Pipe Example:

package main

import "github.com/codeskyblue/go-sh"

func main() {
    sh.Command("echo", "hello\tworld").Command("cut", "-f2").Run()
}

Because I like os/exec, go-sh is very much modelled after it. However, go-sh provides a better experience.

These are some of its features:

  • keep the variable environment (e.g. export)
  • alias support (e.g. alias in shell)
  • remember current dir
  • pipe command
  • shell build-in commands echo & test
  • timeout support

Examples are important:

sh: echo hello
go: sh.Command("echo", "hello").Run()

sh: export BUILD_ID=123
go: s = sh.NewSession().SetEnv("BUILD_ID", "123")

sh: alias ll='ls -l'
go: s = sh.NewSession().Alias('ll', 'ls', '-l')

sh: (cd /; pwd)
go: sh.Command("pwd", sh.Dir("/")).Run()

sh: test -d data || mkdir data
go: if ! sh.Test("dir", "data") { sh.Command("mkdir", "data").Run() }

sh: cat first second | awk '{print $1}'
go: sh.Command("cat", "first", "second").Command("awk", "{print $1}").Run()

sh: count=$(echo "one two three" | wc -w)
go: count, err := sh.Echo("one two three").Command("wc", "-w").Output()

sh(in ubuntu): timeout 1s sleep 3
go: c := sh.Command("sleep", "3"); c.Start(); c.WaitTimeout(time.Second) # default SIGKILL
go: out, err := sh.Command("sleep", "3").SetTimeout(time.Second).Output() # set session timeout and get output)

sh: echo hello | cat
go: out, err := sh.Command("cat").SetInput("hello").Output()

sh: cat # read from stdin
go: out, err := sh.Command("cat").SetStdin(os.Stdin).Output()

sh: ls -l > /tmp/listing.txt # write stdout to file
go: err := sh.Command("ls", "-l").WriteStdout("/tmp/listing.txt")

If you need to keep env and dir, it is better to create a session

session := sh.NewSession()
session.SetEnv("BUILD_ID", "123")
session.SetDir("/")
# then call cmd
session.Command("echo", "hello").Run()
# set ShowCMD to true for easily debug
session.ShowCMD = true

for more information, it better to see docs. Go Walker

contribute

If you love this project, starring it will encourage the coder. Pull requests are welcome.

support the author: alipay

thanks

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

the reason to use Go shell

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.

Core symbols most depended-on inside this repo

Command
called by 47
sh.go
Run
called by 19
pipe.go
NewSession
called by 17
sh.go
Output
called by 14
pipe.go
Test
called by 13
test.go
Start
called by 6
pipe.go
Wait
called by 4
pipe.go
Alias
called by 4
sh.go

Shape

Function 35
Method 24
Struct 3
TypeAlias 1

Languages

Go100%

Modules by API surface

sh.go17 symbols
pipe.go11 symbols
pipe_test.go9 symbols
sh_test.go7 symbols
test.go6 symbols
test_test.go5 symbols
example_test.go4 symbols
example/timeout/timeout.go1 symbols
example/tail/tailf.go1 symbols
example/less/less.go1 symbols
example/example1.go1 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add go-sh \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact