Skip to content

Commit 5f59b2e

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents e5a44f0 + e8b8fb8 commit 5f59b2e

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

api/http.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package api
33
import (
44
"context"
55
_ "embed"
6+
"encoding/base64"
7+
"encoding/json"
8+
"fmt"
9+
"log"
610
"strings"
711

812
"github.com/gin-contrib/cors"
@@ -118,7 +122,18 @@ func NewHttpAPI(lc fx.Lifecycle, resolver *ledger.Resolver) *HttpAPI {
118122
}
119123

120124
if err != nil {
121-
res["err"] = err.Error()
125+
err_str := err.Error()
126+
err_str = strings.ReplaceAll(err_str, "\n", "\r\n")
127+
payload, err := json.Marshal(gin.H{
128+
"error": err_str,
129+
})
130+
if err != nil {
131+
log.Fatal(err)
132+
}
133+
payload_b64 := base64.StdEncoding.EncodeToString([]byte(payload))
134+
link := fmt.Sprintf("https://play.numscript.org/?payload=%v", payload_b64)
135+
res["err"] = err_str
136+
res["details"] = link
122137
}
123138

124139
c.JSON(200, res)

cmd/root.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/numary/ledger/config"
1616
"github.com/numary/ledger/ledger"
1717
"github.com/numary/ledger/storage"
18+
"github.com/numary/machine/script/compiler"
1819
"github.com/spf13/cobra"
1920
"github.com/spf13/viper"
2021
"go.uber.org/fx"
@@ -91,7 +92,7 @@ func Execute() {
9192
},
9293
})
9394

94-
script := &cobra.Command{
95+
script_exec := &cobra.Command{
9596
Use: "exec [ledger] [script]",
9697
Args: cobra.ExactArgs(2),
9798
Run: func(cmd *cobra.Command, args []string) {
@@ -135,15 +136,49 @@ func Execute() {
135136
log.Fatal(err)
136137
}
137138

138-
fmt.Println(res.StatusCode, string(b))
139+
var result struct {
140+
Err string `json:"err,omitempty"`
141+
Ok bool `json:"ok"`
142+
}
143+
err = json.Unmarshal(b, &result)
144+
if err != nil {
145+
log.Fatal(err)
146+
}
147+
if result.Ok {
148+
fmt.Println("Script ran successfully ✅")
149+
} else {
150+
log.Fatal(result.Err)
151+
}
152+
},
153+
}
154+
155+
script_check := &cobra.Command{
156+
Use: "check [script]",
157+
Args: cobra.ExactArgs(1),
158+
Run: func(cmd *cobra.Command, args []string) {
159+
config.Init()
160+
161+
b, err := ioutil.ReadFile(args[0])
162+
163+
if err != nil {
164+
log.Fatal(err)
165+
}
166+
167+
_, err = compiler.Compile(string(b))
168+
if err != nil {
169+
log.Fatal(err)
170+
} else {
171+
fmt.Println("Script is correct ✅")
172+
}
139173
},
140174
}
141175

142176
root.AddCommand(server)
143177
root.AddCommand(conf)
144178
root.AddCommand(UICmd)
145179
root.AddCommand(store)
146-
root.AddCommand(script)
180+
root.AddCommand(script_exec)
181+
root.AddCommand(script_check)
147182

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/huandu/go-sqlbuilder v1.12.1
99
github.com/jackc/pgx/v4 v4.11.0
1010
github.com/mattn/go-sqlite3 v1.14.7
11-
github.com/numary/machine v0.0.0-20210721174107-2596ca4e5b60
11+
github.com/numary/machine v0.0.0-20210726090747-5853ebec28f0
1212
github.com/pkg/errors v0.8.1
1313
github.com/spf13/cobra v1.1.3
1414
github.com/spf13/viper v1.8.1

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
365365
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
366366
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
367367
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
368+
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
369+
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
368370
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
369371
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
370372
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
@@ -416,6 +418,10 @@ github.com/numary/machine v0.0.0-20210719124351-aad80bd74032 h1:/0PgX+JYoyHa776k
416418
github.com/numary/machine v0.0.0-20210719124351-aad80bd74032/go.mod h1:dvm8ZK8ywVDLDCKX8RyVT5bnD5/9A2RRmK8ZdJ9OvSM=
417419
github.com/numary/machine v0.0.0-20210721174107-2596ca4e5b60 h1:qUYxAdLEOSbBR9l5Vse9kde3dwZgkHdwfuAfGHmriJ0=
418420
github.com/numary/machine v0.0.0-20210721174107-2596ca4e5b60/go.mod h1:dvm8ZK8ywVDLDCKX8RyVT5bnD5/9A2RRmK8ZdJ9OvSM=
421+
github.com/numary/machine v0.0.0-20210723112905-42b29a92fe8d h1:YfQ7urinEfBDyDGijNer3aYaNhHV7xhy5mE/XdYyHms=
422+
github.com/numary/machine v0.0.0-20210723112905-42b29a92fe8d/go.mod h1:azMZte4ywWM0ISNMgMII4IS9XJ0N3ZAB7TfQ2Ay3dL0=
423+
github.com/numary/machine v0.0.0-20210726090747-5853ebec28f0 h1:TM2r/u2KwZBipmd6s5q/mzlzWfNCFwEadH9xyYbvZAw=
424+
github.com/numary/machine v0.0.0-20210726090747-5853ebec28f0/go.mod h1:azMZte4ywWM0ISNMgMII4IS9XJ0N3ZAB7TfQ2Ay3dL0=
419425
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
420426
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
421427
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=

0 commit comments

Comments
 (0)