-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
240 lines (203 loc) · 5.92 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bytes"
"fmt"
"github.com/Jumpaku/cyamli/docs"
"github.com/Jumpaku/cyamli/name"
"go/format"
"io"
"os"
"slices"
_ "embed"
"github.com/Jumpaku/cyamli/golang"
"github.com/Jumpaku/cyamli/python3"
"github.com/Jumpaku/cyamli/schema"
)
//go:embed cli.yaml
var schemaData []byte
func LoadSchema() *schema.Schema {
s, _ := schema.Load(bytes.NewBuffer(schemaData))
return s
}
var cli = NewCLI()
func main() {
cli.FUNC = func(subcommand []string, input CLI_Input, inputErr error) (err error) {
switch {
case inputErr != nil:
fmt.Println(cli.DESC_Simple())
case input.Opt_Version:
fmt.Printf("version: %s\n", LoadSchema().Program.Version)
case input.Opt_Help:
fmt.Println(cli.DESC_Detail())
}
return nil
}
cli.List.FUNC = funcList
cli.Generate.Golang.FUNC = funcGenerateGolang
cli.Generate.Python3.FUNC = funcGeneratePython3
cli.Generate.Docs.FUNC = funcGenerateDocs
if err := Run(cli, os.Args); err != nil {
panic(err)
}
}
func funcList(_ []string, input CLI_List_Input, inputErr error) (err error) {
if inputErr != nil {
fmt.Println(cli.DESC_Simple())
return fmt.Errorf("fail to resolve command line arguments: %w", inputErr)
}
var reader io.Reader = os.Stdin
if input.Opt_SchemaPath != "" {
f, err := os.Open(input.Opt_SchemaPath)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
defer f.Close()
reader = f
}
s, err := schema.Load(reader)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
subcommands := []name.Path{}
_ = s.Walk(func(path name.Path, cmd *schema.Command) error {
subcommands = append(subcommands, path)
return nil
})
slices.SortFunc(subcommands, func(a, b name.Path) int { return slices.Compare(a, b) })
for _, path := range subcommands {
program := s.Program.Name
if program == "" {
program = "<program>"
}
_, err := fmt.Fprintln(os.Stdout, append(name.Path{program}, path...).Join(" ", "", ""))
if err != nil {
return fmt.Errorf("fail to print %q: %w", input.Opt_SchemaPath, err)
}
}
return nil
}
func funcGenerateGolang(_ []string, input CLI_GenerateGolang_Input, inputErr error) (err error) {
if inputErr != nil {
fmt.Println(cli.DESC_Simple())
return fmt.Errorf("fail to resolve command line arguments: %w", inputErr)
}
if input.Opt_Help {
fmt.Println(cli.Generate.Golang.DESC_Detail())
return nil
}
var reader io.Reader = os.Stdin
if input.Opt_SchemaPath != "" {
f, err := os.Open(input.Opt_SchemaPath)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
defer f.Close()
reader = f
}
schema, err := schema.Load(reader)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
var writer io.Writer = os.Stdout
if input.Opt_OutPath != "" {
f, err := os.Create(input.Opt_OutPath)
if err != nil {
return fmt.Errorf("fail to open output file %q: %w", input.Opt_OutPath, err)
}
defer f.Close()
writer = f
}
buf := bytes.NewBuffer(nil)
if err := golang.Generate(input.Opt_Package, schema, buf); err != nil {
return fmt.Errorf("fail to generate cli %q: %w", input.Opt_SchemaPath, err)
}
fmtBytes, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("fail to format cli code: %w", err)
}
if _, err := writer.Write(fmtBytes); err != nil {
return fmt.Errorf("fail to generate cli %q: %w", input.Opt_SchemaPath, err)
}
return nil
}
func funcGeneratePython3(_ []string, input CLI_GeneratePython3_Input, inputErr error) (err error) {
if inputErr != nil {
fmt.Println(cli.DESC_Simple())
return fmt.Errorf("fail to resolve command line arguments: %w", inputErr)
}
if input.Opt_Help {
fmt.Println(cli.Generate.Golang.DESC_Detail())
return nil
}
var reader io.Reader = os.Stdin
if input.Opt_SchemaPath != "" {
f, err := os.Open(input.Opt_SchemaPath)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
defer f.Close()
reader = f
}
schema, err := schema.Load(reader)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
var writer io.Writer = os.Stdout
if input.Opt_OutPath != "" {
f, err := os.Create(input.Opt_OutPath)
if err != nil {
return fmt.Errorf("fail to open output file %q: %w", input.Opt_OutPath, err)
}
defer f.Close()
writer = f
}
buf := bytes.NewBuffer(nil)
if err := python3.Generate(schema, buf); err != nil {
return fmt.Errorf("fail to generate cli %q: %w", input.Opt_SchemaPath, err)
}
if _, err := writer.Write(buf.Bytes()); err != nil {
return fmt.Errorf("fail to generate cli %q: %w", input.Opt_SchemaPath, err)
}
return nil
}
func funcGenerateDocs(_ []string, input CLI_GenerateDocs_Input, inputErr error) (err error) {
if inputErr != nil {
fmt.Println(cli.DESC_Simple())
return fmt.Errorf("fail to resolve command line arguments: %w", inputErr)
}
if input.Opt_Help {
fmt.Println(cli.Generate.Golang.DESC_Detail())
return nil
}
var reader io.Reader = os.Stdin
if input.Opt_SchemaPath != "" {
f, err := os.Open(input.Opt_SchemaPath)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
defer f.Close()
reader = f
}
schema, err := schema.Load(reader)
if err != nil {
return fmt.Errorf("fail to open schema file %q: %w", input.Opt_SchemaPath, err)
}
var writer io.Writer = os.Stdout
if input.Opt_OutPath != "" {
f, err := os.Create(input.Opt_OutPath)
if err != nil {
return fmt.Errorf("fail to open output file %q: %w", input.Opt_OutPath, err)
}
defer f.Close()
writer = f
}
args := docs.GenerateArgs{
Format: docs.DocsFormat(input.Opt_Format),
All: input.Opt_All,
Subcommand: input.Arg_Subcommands,
}
if err := docs.Generate(schema, args, writer); err != nil {
return fmt.Errorf("fail to generate cli %q: %w", input.Opt_SchemaPath, err)
}
return nil
}