You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45-42Lines changed: 45 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
A Go library for creating Bash environments, exporting Go functions in them as Bash functions, and running commands in that Bash environment. Combined with a tool like [go-bindata](https://github.com/jteeuwen/go-bindata), you can write programs that are part written in Go and part written in Bash that can be distributed as standalone binaries.
@@ -12,63 +12,64 @@ Here we have a simple Go program that defines a `reverse` function, creates a Ba
12
12
package main
13
13
14
14
import (
15
-
"os"
16
-
"io/ioutil"
17
-
"log"
18
-
"strings"
15
+
"os"
16
+
"io/ioutil"
17
+
"log"
18
+
"strings"
19
19
20
-
"github.com/progrium/go-basher"
20
+
"github.com/progrium/go-basher"
21
21
)
22
22
23
23
funcreverse(args []string) {
24
-
bytes, err:= ioutil.ReadAll(os.Stdin)
25
-
if err != nil {
26
-
log.Fatal(err)
27
-
}
28
-
runes:= []rune(strings.Trim(string(bytes), "\n"))
29
-
fori, j:=0, len(runes)-1; i < j; i, j = i+1, j-1 {
30
-
runes[i], runes[j] = runes[j], runes[i]
31
-
}
32
-
println(string(runes))
24
+
bytes, err:= ioutil.ReadAll(os.Stdin)
25
+
if err != nil {
26
+
log.Fatal(err)
27
+
}
28
+
29
+
runes:= []rune(strings.Trim(string(bytes), "\n"))
30
+
fori, j:=0, len(runes)-1; i < j; i, j = i+1, j-1 {
31
+
runes[i], runes[j] = runes[j], runes[i]
32
+
}
33
+
println(string(runes))
33
34
}
34
35
35
36
funcmain() {
36
-
bash, _:= basher.NewContext("/bin/bash", false)
37
-
bash.ExportFunc("reverse", reverse)
38
-
if bash.HandleFuncs(os.Args) {
39
-
os.Exit(0)
40
-
}
41
-
42
-
bash.Source("main.bash", nil)
43
-
status, err:= bash.Run("main", os.Args[1:])
44
-
if err != nil {
45
-
log.Fatal(err)
46
-
}
47
-
os.Exit(status)
37
+
bash, _:= basher.NewContext("/bin/bash", false)
38
+
bash.ExportFunc("reverse", reverse)
39
+
if bash.HandleFuncs(os.Args) {
40
+
os.Exit(0)
41
+
}
42
+
43
+
bash.Source("main.bash", nil)
44
+
status, err:= bash.Run("main", os.Args[1:])
45
+
if err != nil {
46
+
log.Fatal(err)
47
+
}
48
+
os.Exit(status)
48
49
}
49
50
```
50
51
51
52
Here is our `main.bash` file, the actual heart of the program:
52
53
53
54
```bash
54
55
main() {
55
-
echo"Hello world"| reverse
56
+
echo"Hello world"| reverse
56
57
}
57
58
```
58
59
59
60
## Using go-basher with go-bindata
60
61
61
62
You can bundle your Bash scripts into your Go binary using [go-bindata](https://github.com/jteeuwen/go-bindata). First install go-bindata:
62
63
63
-
$ go get github.com/jteeuwen/go-bindata/...
64
+
go get github.com/jteeuwen/go-bindata/...
64
65
65
66
Now put all your Bash scripts in a directory called `bash`. The above example program would mean you'd have a `bash/main.bash` file. Run `go-bindata` on the directory:
66
67
67
-
$ go-bindata bash
68
+
go-bindata bash
68
69
69
70
This will produce a `bindata.go` file that includes all of your Bash scripts.
70
71
71
-
> `bindata.go` includes a function called `Asset` that behaves like `ioutil.ReadFile` for files in your `bindata.go`.
72
+
> `bindata.go` includes a function called `Asset` that behaves like `ioutil.ReadFile` for files in your `bindata.go`.
72
73
73
74
Here's how you embed it into the above example program:
74
75
@@ -77,22 +78,22 @@ Here's how you embed it into the above example program:
77
78
* method B: replace all code in the `main()`-function with the `Application()`-helper function (see below)
78
79
79
80
```Go
80
-
basher.Application(
81
-
map[string]func([]string){
82
-
"reverse": reverse,
83
-
}, []string{
84
-
"bash/main.bash",
85
-
},
86
-
Asset,
87
-
true,
88
-
)
81
+
basher.Application(
82
+
map[string]func([]string){
83
+
"reverse": reverse,
84
+
}, []string{
85
+
"bash/main.bash",
86
+
},
87
+
Asset,
88
+
true,
89
+
)
89
90
```
90
91
91
92
## Batteries included, but replaceable
92
93
93
94
Did you already hear that term? Sometimes Bash binary is missing, for example when using alpine linux or busybox. Or sometimes its not the correct version. Like OSX ships with Bash 3.x which misses a lot of usefull features. Or you want to make sure to avoid shellshock attack.
94
95
95
-
For those reasons static versions of Bash binaries are included for linux and darwin. Statically linked bash binaries are released at: https://github.com/robxu9/bash-static. These are then turned into go code, with go-bindata: bindata_linux.go and bindata_darwin.go.
96
+
For those reasons static versions of Bash binaries are included for linux and darwin. Statically linked bash binaries are released at: <https://github.com/robxu9/bash-static>. These are then turned into go code, with go-bindata: bindata_linux.go and bindata_darwin.go.
96
97
97
98
When you use the `basher.Application()` function, the built in Bash binary will be extracted into the `~/.basher/` dir.
98
99
@@ -106,7 +107,9 @@ Go is a great compiled systems language, but it can still be faster to write and
106
107
107
108
Take a common task like making an HTTP request for JSON data. Parsing JSON is easy in Go, but without depending on a tool like `jq` it is not even worth trying in Bash. And some formats like YAML don't even have a good `jq` equivalent. Whereas making an HTTP request in Go in the *simplest* case is going to be 6+ lines, as opposed to Bash where you can use `curl` in one line. If we write our JSON parser in Go and fetch the HTTP doc with `curl`, we can express printing a field from a remote JSON object in one line:
0 commit comments