forked from danielvladco/go-proto-gql
/
main.go
94 lines (81 loc) · 2.18 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
package main
import (
"flag"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/vektah/gqlparser/v2/formatter"
"github.com/danielvladco/go-proto-gql/pkg/generator"
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return "str list"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var (
importPath = arrayFlags{}
fileNames = arrayFlags{}
svc = flag.Bool("svc", false, "")
merge = flag.Bool("merge", false, "")
)
func main() {
flag.Var(&importPath, "I", "path")
flag.Var(&fileNames, "f", "path")
flag.Parse()
newFileNames, err := protoparse.ResolveFilenames(importPath, fileNames...)
if err != nil {
log.Fatal(err)
}
descs, err := protoparse.Parser{ImportPaths: importPath}.ParseFiles(newFileNames...)
if err != nil {
log.Fatal(err)
}
gqlDesc, err := generator.NewSchemas(descs, *merge, *svc)
if err != nil {
log.Fatal(err)
}
for _, schema := range gqlDesc {
if len(schema.FileDescriptors) < 1 {
log.Fatalf("unexpected number of proto descriptors: %d for gql schema", len(schema.FileDescriptors))
}
if len(schema.FileDescriptors) > 1 {
if err := generateFile(schema, true); err != nil {
log.Fatal(err)
}
break
}
if err := generateFile(schema, *merge); err != nil {
log.Fatal(err)
}
}
}
func generateFile(schema *generator.SchemaDescriptor, merge bool) error {
sc, err := os.Create(resolveGraphqlFilename(schema.FileDescriptors[0].GetName(), merge))
if err != nil {
return err
}
defer sc.Close()
formatter.NewFormatter(sc).FormatSchema(schema.AsGraphql())
return nil
}
func resolveGraphqlFilename(protoFileName string, merge bool) string {
if merge {
gqlFileName := "schema.graphqls"
absProtoFileName, err := filepath.Abs(protoFileName)
if err == nil {
protoDirSlice := strings.Split(filepath.Dir(absProtoFileName), string(filepath.Separator))
if len(protoDirSlice) > 0 {
gqlFileName = protoDirSlice[len(protoDirSlice)-1] + ".graphqls"
}
}
protoDir, _ := path.Split(protoFileName)
return path.Join(protoDir, gqlFileName)
}
return strings.TrimSuffix(protoFileName, path.Ext(protoFileName)) + ".graphqls"
}