Skip to content

Commit 5096cb8

Browse files
authored
Merge pull request #20 from numary/dev/exec
Exec script from CLI
2 parents cfbb99a + 926b2a9 commit 5096cb8

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ curl -X POST \
2727
"postings": [
2828
{
2929
"source": "world",
30-
"destination": "central-bank",
30+
"destination": "central_bank",
3131
"asset": "GEM",
3232
"amount": 100
3333
},
3434
{
35-
"source": "central-bank",
35+
"source": "central_bank",
3636
"destination": "users:001",
3737
"asset": "GEM",
3838
"amount": 100

cmd/root.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package cmd
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
7+
"io/ioutil"
58
"log"
9+
"net/http"
610
"os"
11+
"regexp"
712

13+
"github.com/gin-gonic/gin"
814
"github.com/numary/ledger/api"
915
"github.com/numary/ledger/config"
1016
"github.com/numary/ledger/ledger"
@@ -85,10 +91,59 @@ func Execute() {
8591
},
8692
})
8793

94+
script := &cobra.Command{
95+
Use: "exec [ledger] [script]",
96+
Args: cobra.ExactArgs(2),
97+
Run: func(cmd *cobra.Command, args []string) {
98+
config.Init()
99+
100+
b, err := ioutil.ReadFile(args[1])
101+
102+
if err != nil {
103+
log.Fatal(err)
104+
}
105+
106+
r := regexp.MustCompile(`^\n`)
107+
s := string(b)
108+
s = r.ReplaceAllString(s, "")
109+
110+
b, err = json.Marshal(gin.H{
111+
"plain": string(s),
112+
})
113+
114+
if err != nil {
115+
log.Fatal(err)
116+
}
117+
118+
res, err := http.Post(
119+
fmt.Sprintf(
120+
"http://%s/%s/script",
121+
viper.Get("server.http.bind_address"),
122+
args[0],
123+
),
124+
"application/json",
125+
bytes.NewReader([]byte(b)),
126+
)
127+
128+
if err != nil {
129+
log.Fatal(err)
130+
}
131+
132+
b, err = ioutil.ReadAll(res.Body)
133+
134+
if err != nil {
135+
log.Fatal(err)
136+
}
137+
138+
fmt.Println(res.StatusCode, string(b))
139+
},
140+
}
141+
88142
root.AddCommand(server)
89143
root.AddCommand(conf)
90144
root.AddCommand(UICmd)
91145
root.AddCommand(store)
146+
root.AddCommand(script)
92147

93148
if err := root.Execute(); err != nil {
94149
fmt.Fprintln(os.Stderr, err)

0 commit comments

Comments
 (0)