Skip to content

Commit

Permalink
Merge pull request #20 from numary/dev/exec
Browse files Browse the repository at this point in the history
Exec script from CLI
  • Loading branch information
altitude authored Jul 20, 2021
2 parents cfbb99a + 926b2a9 commit 5096cb8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ curl -X POST \
"postings": [
{
"source": "world",
"destination": "central-bank",
"destination": "central_bank",
"asset": "GEM",
"amount": 100
},
{
"source": "central-bank",
"source": "central_bank",
"destination": "users:001",
"asset": "GEM",
"amount": 100
Expand Down
55 changes: 55 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"

"github.com/gin-gonic/gin"
"github.com/numary/ledger/api"
"github.com/numary/ledger/config"
"github.com/numary/ledger/ledger"
Expand Down Expand Up @@ -85,10 +91,59 @@ func Execute() {
},
})

script := &cobra.Command{
Use: "exec [ledger] [script]",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
config.Init()

b, err := ioutil.ReadFile(args[1])

if err != nil {
log.Fatal(err)
}

r := regexp.MustCompile(`^\n`)
s := string(b)
s = r.ReplaceAllString(s, "")

b, err = json.Marshal(gin.H{
"plain": string(s),
})

if err != nil {
log.Fatal(err)
}

res, err := http.Post(
fmt.Sprintf(
"http://%s/%s/script",
viper.Get("server.http.bind_address"),
args[0],
),
"application/json",
bytes.NewReader([]byte(b)),
)

if err != nil {
log.Fatal(err)
}

b, err = ioutil.ReadAll(res.Body)

if err != nil {
log.Fatal(err)
}

fmt.Println(res.StatusCode, string(b))
},
}

root.AddCommand(server)
root.AddCommand(conf)
root.AddCommand(UICmd)
root.AddCommand(store)
root.AddCommand(script)

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

0 comments on commit 5096cb8

Please sign in to comment.