-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (97 loc) · 2.53 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const Version = "0.1.0"
const usage = `
███████╗██╗░░░░░██╗░░░░░░█████╗░
██╔════╝██║░░░░░██║░░░░░██╔══██╗
█████╗░░██║░░░░░██║░░░░░███████║
██╔══╝░░██║░░░░░██║░░░░░██╔══██║
███████╗███████╗███████╗██║░░██║
╚══════╝╚══════╝╚══════╝╚═╝░░╚═╝ v` + Version + `
Usage: ella [command]
Commands:
- fmt Format one or many files in place using glob pattern
ella fmt <glob path>
- gen Generate code from a folder to a file and currently
supports .go and .ts extensions
ella gen <pkg> <output path to file> <search glob paths...>
- ver Print the version of ella
example:
ella fmt "./path/to/*.ella"
ella gen rpc ./path/to/output.go "./path/to/*.ella"
ella gen rpc ./path/to/output.ts "./path/to/*.ella" "./path/to/other/*.ella"
`
func main() {
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(0)
}
var err error
switch os.Args[1] {
case "fmt":
if len(os.Args) < 3 {
fmt.Print(usage)
os.Exit(0)
}
err = format(os.Args[2])
case "gen":
if len(os.Args) < 5 {
fmt.Print(usage)
os.Exit(0)
}
err = gen(os.Args[2], os.Args[3], os.Args[4:]...)
case "ver":
fmt.Println(Version)
default:
fmt.Print(usage)
os.Exit(0)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func format(path string) error {
filenames, err := filepath.Glob(path)
if err != nil {
return err
}
for _, filename := range filenames {
doc, err := ParseDocument(NewParserWithFilenames(filename))
if err != nil {
return err
}
var sb strings.Builder
doc.Format(&sb)
err = os.WriteFile(filename, []byte(sb.String()), os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func gen(pkg, out string, searchPaths ...string) (err error) {
var docs []*Document
for _, searchPath := range searchPaths {
filenames, err := filepath.Glob(searchPath)
if err != nil {
return err
}
for _, filename := range filenames {
doc, err := ParseDocument(NewParserWithFilenames(filename))
if err != nil {
return err
}
docs = append(docs, doc)
}
}
if err = Validate(docs...); err != nil {
return err
}
return Generate(pkg, out, docs)
}