-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotsukai.go
97 lines (84 loc) · 1.96 KB
/
otsukai.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"errors"
"github.com/mika-sandbox/otsukai/logger"
"github.com/mika-sandbox/otsukai/parser"
"github.com/mika-sandbox/otsukai/runtime"
"github.com/mika-sandbox/otsukai/runtime/context"
re "github.com/mika-sandbox/otsukai/runtime/errors"
"github.com/urfave/cli/v2"
"os"
)
func run(c *cli.Context) error {
recipe := c.String("recipe")
content, err := os.ReadFile(recipe)
if err != nil {
logger.Fatalf("failed to read recipe: %s", err)
return re.RUNTIME_ERROR
}
ruby, err := parser.Parser.ParseString("", string(content)+"\n")
if err != nil {
logger.Errf("invalid syntax: %s", err)
return re.SYNTAX_ERROR
}
ctx := context.NewContext(ruby)
if err := runtime.Run(&ctx); err != nil {
if errors.Is(err, re.EXECUTION_ERROR) || errors.Is(err, re.SYNTAX_ERROR) || errors.Is(err, re.RUNTIME_ERROR) {
return err
}
logger.Fatalf("%s", err)
return err
}
return nil
}
func test(c *cli.Context) error {
recipe := c.String("recipe")
logger.Infof("check syntax for %s", recipe)
content, err := os.ReadFile(recipe)
if err != nil {
logger.Fatalf("failed to read recipe: %s", err.Error())
return err
}
_, err = parser.Parser.ParseString("", string(content)+"\n")
if err != nil {
logger.Errf("check syntas for %s is failed: %s", recipe, err.Error())
return err
}
logger.Successf("check syntax for %s is success", recipe)
return nil
}
func main() {
app := &cli.App{
Name: "otsukai",
Usage: "otsukai",
Commands: []*cli.Command{
{
Name: "run",
Usage: "run otsukai",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "recipe",
Value: "otsukai.rb",
Usage: "path for recipe",
},
},
Action: run,
},
{
Name: "test",
Usage: "test recipe syntax",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "recipe",
Value: "otsukai.rb",
Usage: "path for recipe",
},
},
Action: test,
},
},
}
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}