diff --git a/README.md b/README.md index 3f8137bbc..113173a48 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/root.go b/cmd/root.go index d59f1475b..911a2acb1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" @@ -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)