-
Notifications
You must be signed in to change notification settings - Fork 0
/
basher.go
255 lines (230 loc) · 6.79 KB
/
basher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Package basher provides an API for running and integrating with Bash from Go
package basher
import (
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"github.com/kardianos/osext"
"github.com/mitchellh/go-homedir"
)
func exitStatus(err error) (int, error) {
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// There is no platform independent way to retrieve
// the exit code, but the following will work on Unix
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return int(status.ExitStatus()), nil
}
}
return 0, err
}
return 0, nil
}
// Application sets up a common entrypoint for a Bash application that
// uses exported Go functions. It uses the DEBUG environment variable
// to set debug on the Context, and SHELL for the Bash binary if it
// includes the string "bash". You can pass a loader function to use
// for the sourced files, and a boolean for whether or not the
// environment should be copied into the Context process.
func Application(
funcs map[string]func([]string),
scripts []string,
loader func(string) ([]byte, error),
copyEnv bool) {
bashDir, err := homedir.Expand("~/.basher")
if err != nil {
log.Fatal(err, "1")
}
bashPath := bashDir + "/bash"
if _, err := os.Stat(bashPath); os.IsNotExist(err) {
err = RestoreAsset(bashDir, "bash")
if err != nil {
log.Fatal(err, "1")
}
}
bash, err := NewContext(bashPath, os.Getenv("DEBUG") != "")
if err != nil {
log.Fatal(err)
}
for name, fn := range funcs {
bash.ExportFunc(name, fn)
}
if bash.HandleFuncs(os.Args) {
os.Exit(0)
}
for _, script := range scripts {
bash.Source(script, loader)
}
if copyEnv {
bash.CopyEnv()
}
status, err := bash.Run("main", os.Args[1:])
if err != nil {
log.Fatal(err)
}
os.Exit(status)
}
// A Context is an instance of a Bash interpreter and environment, including
// sourced scripts, environment variables, and embedded Go functions
type Context struct {
sync.Mutex
// Debug simply leaves the generated BASH_ENV file produced
// from each Run call of this Context for debugging.
Debug bool
// BashPath is the path to the Bash executable to be used by Run
BashPath string
// SelfPath is set by NewContext to be the current executable path.
// It's used to call back into the calling Go process to run exported
// functions.
SelfPath string
// The io.Reader given to Bash for STDIN
Stdin io.Reader
// The io.Writer given to Bash for STDOUT
Stdout io.Writer
// The io.Writer given to Bash for STDERR
Stderr io.Writer
vars []string
scripts [][]byte
funcs map[string]func([]string)
}
// Creates and initializes a new Context that will use the given Bash executable.
// The debug mode will leave the produced temporary BASH_ENV file for inspection.
func NewContext(bashpath string, debug bool) (*Context, error) {
executable, err := osext.Executable()
if err != nil {
return nil, err
}
return &Context{
Debug: debug,
BashPath: bashpath,
SelfPath: executable,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
scripts: make([][]byte, 0),
vars: make([]string, 0),
funcs: make(map[string]func([]string)),
}, nil
}
// Copies the current environment variables into the Context
func (c *Context) CopyEnv() {
c.Lock()
defer c.Unlock()
c.vars = append(c.vars, os.Environ()...)
}
// Adds a shell script to the Context environment. The loader argument can be nil
// which means it will use ioutil.Readfile and load from disk, but it exists so you
// can use the Asset function produced by go-bindata when including script files in
// your Go binary. Calls to Source adds files to the environment in order.
func (c *Context) Source(filepath string, loader func(string) ([]byte, error)) error {
if loader == nil {
loader = ioutil.ReadFile
}
data, err := loader(filepath)
if err != nil {
return err
}
c.Lock()
defer c.Unlock()
c.scripts = append(c.scripts, data)
return nil
}
// Adds an environment variable to the Context
func (c *Context) Export(name string, value string) {
c.Lock()
defer c.Unlock()
c.vars = append(c.vars, name+"="+value)
}
// Registers a function with the Context that will produce a Bash function in the environment
// that calls back into your executable triggering the function defined as fn.
func (c *Context) ExportFunc(name string, fn func([]string)) {
c.Lock()
defer c.Unlock()
c.funcs[name] = fn
}
// Expects your os.Args to parse and handle any callbacks to Go functions registered with
// ExportFunc. You normally call this at the beginning of your program. If a registered
// function is found and handled, HandleFuncs will exit with the appropriate exit code for you.
func (c *Context) HandleFuncs(args []string) bool {
for i, arg := range args {
if arg == ":::" && len(args) > i+1 {
c.Lock()
defer c.Unlock()
for cmd := range c.funcs {
if cmd == args[i+1] {
c.funcs[cmd](args[i+2:])
return true
}
}
return false
}
}
return false
}
func (c *Context) buildEnvfile() (string, error) {
file, err := ioutil.TempFile(os.TempDir(), "bashenv.")
if err != nil {
return "", err
}
defer file.Close()
// variables
file.Write([]byte("unset BASH_ENV\n")) // unset for future calls to bash
file.Write([]byte("export SELF=" + os.Args[0] + "\n"))
file.Write([]byte("export SELF_EXECUTABLE='" + c.SelfPath + "'\n"))
for _, kvp := range c.vars {
file.Write([]byte("export " + strings.Replace(
strings.Replace(kvp, "'", "\\'", -1), "=", "=$'", 1) + "'\n"))
}
// functions
for cmd := range c.funcs {
file.Write([]byte(cmd + "() { $SELF_EXECUTABLE ::: " + cmd + " \"$@\"; }\n"))
}
// scripts
for _, data := range c.scripts {
file.Write(append(data, '\n'))
}
return file.Name(), nil
}
// Runs a command in Bash from this Context. With each call, a temporary file
// is generated used as BASH_ENV when calling Bash that includes all variables,
// sourced scripts, and exported functions from the Context. Standard I/O by
// default is attached to the calling process I/O. You can change this by setting
// the Stdout, Stderr, Stdin variables of the Context.
func (c *Context) Run(command string, args []string) (int, error) {
c.Lock()
defer c.Unlock()
envfile, err := c.buildEnvfile()
if err != nil {
return 0, err
}
if !c.Debug {
defer os.Remove(envfile)
}
argstring := ""
for _, arg := range args {
argstring = argstring + " '" + strings.Replace(arg, "'", "'\\''", -1) + "'"
}
signals := make(chan os.Signal, 1)
signal.Notify(signals)
cmd := exec.Command(c.BashPath, "-c", command+argstring)
cmd.Env = []string{"BASH_ENV=" + envfile}
cmd.Stdin = c.Stdin
cmd.Stdout = c.Stdout
cmd.Stderr = c.Stderr
cmd.Start()
go func() {
for sig := range signals {
cmd.Process.Signal(sig)
if cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
cmd.Process.Signal(sig)
}
}
}()
return exitStatus(cmd.Wait())
}