This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (113 loc) · 2.57 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
package main
import (
"github.com/openland/spacex-cli/codegen"
"github.com/openland/spacex-cli/il"
"github.com/urfave/cli"
"log"
"os"
"path/filepath"
"strings"
)
func collectFiles(path string) ([]string, error) {
var res []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(strings.ToLower(info.Name()), ".graphql") {
return nil
}
res = append(res, path)
return nil
})
if err != nil {
return nil, err
}
return res, nil
}
func main() {
app := cli.NewApp()
app.Name = "spacex-cli"
app.HelpName = "spacex-cli"
app.Version = "1.0.0"
var output string
var source string
var schema string
var target string
var pkg string
var name string
app.Commands = []cli.Command{
{
Name: "generate",
Aliases: []string{"g"},
Usage: "Generate SpaceX client",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output, o",
Usage: "Output file name",
Destination: &output,
},
cli.StringFlag{
Name: "queries, q",
Usage: "Query directory",
Destination: &source,
},
cli.StringFlag{
Name: "schema, s",
Usage: "Schema path",
Destination: &schema,
},
cli.StringFlag{
Name: "target, t",
Usage: "Generator target",
Destination: &target,
},
cli.StringFlag{
Name: "package, p",
Usage: "Package name",
Destination: &pkg,
},
cli.StringFlag{
Name: "name, n",
Usage: "Name of client class",
Destination: &name,
},
},
Action: func(c *cli.Context) error {
if target != "kotlin" && target != "swift" && target != "typescript" && target != "client" {
log.Panic("Only kotlin, swift, typescript or client target is supported")
}
sourcePath, err := filepath.Abs(source)
if err != nil {
panic(err)
}
schemaPath, err := filepath.Abs(schema)
if err != nil {
panic(err)
}
files, err := collectFiles(sourcePath)
if err != nil {
panic(err)
}
model := il.LoadModel(schemaPath, files)
if target == "kotlin" {
codegen.GenerateKotlin(model, output, pkg)
} else if target == "swift" {
codegen.GenerateSwift(model, output)
} else if target == "typescript" {
codegen.GenerateTypescript(model, output)
} else if target == "client" {
codegen.GenerateClient2(model, name, output)
}
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}