forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
53 lines (46 loc) · 1.01 KB
/
example.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"github.com/dustin/go-jsonpointer"
"github.com/progrium/go-basher"
)
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func jsonPointer(args []string) int {
if len(args) == 0 {
return 3
}
bytes, err := ioutil.ReadAll(os.Stdin)
assert(err)
var o map[string]interface{}
assert(json.Unmarshal(bytes, &o))
println(jsonpointer.Get(o, args[0]).(string))
return 0
}
func reverse(args []string) int {
bytes, err := ioutil.ReadAll(os.Stdin)
assert(err)
runes := []rune(strings.Trim(string(bytes), "\n"))
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
println(string(runes))
return 0
}
func main() {
bash, _ := basher.NewContext("/bin/bash", false)
bash.ExportFunc("json-pointer", jsonPointer)
bash.ExportFunc("reverse", reverse)
bash.HandleFuncs(os.Args)
bash.Source("bash/example.bash", Asset)
status, err := bash.Run("main", os.Args[1:])
assert(err)
os.Exit(status)
}