-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (107 loc) · 2.63 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"gopkg.in/yaml.v3"
)
func main() {
sources, errRead := readSchemas(schemasFlag)
if errRead != nil {
panic(errRead)
}
schema, errLoad := loadSchema(sources)
if errLoad != nil {
panic(errLoad)
}
marshal := saveToYAML
if asJSON {
marshal = saveToJSON
}
if err := marshal(resultFile, schema); err != nil {
panic(err)
}
}
func readSchemas(schemas []string) ([]*ast.Source, error) {
sources := make([]*ast.Source, 0, len(schemas))
for _, filename := range schemas {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("cannot open file %q: %w", filename, err)
}
defer f.Close()
schemaRaw, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("cannot read file %q: %w", filename, err)
}
sources = append(sources, &ast.Source{
Name: filename,
Input: string(schemaRaw),
})
}
return sources, nil
}
func loadSchema(sources []*ast.Source) (*ast.Schema, error) {
schema, err := gqlparser.LoadSchema(sources...)
if err != nil {
return nil, fmt.Errorf("cannot load schema: %w", err)
}
if schema.Query == nil {
schema.Query = &ast.Definition{
Kind: ast.Object,
Name: "Query",
}
schema.Types["Query"] = schema.Query
}
return schema, nil
}
var schemasFlag stringSliceFlag
var resultFile string
var asJSON bool
func init() {
flag.Var(&schemasFlag, "schema", "file with a GraphQL schema")
flag.StringVar(&resultFile, "result", "schema.yaml", "filename with a schema in YAML")
flag.BoolVar(&asJSON, "json", false, "should save schema in JSON")
flag.Parse()
if len(schemasFlag) == 0 {
panic("no schema files provided")
}
if asJSON && resultFile == "schema.yaml" {
resultFile = "schema.json"
}
}
func saveToYAML(filaname string, v interface{}) error {
result, err := os.Create(filaname)
if err != nil {
return fmt.Errorf("cannot open result file: %w", err)
}
defer result.Close()
if err := yaml.NewEncoder(result).Encode(v); err != nil {
return fmt.Errorf("cannot marshal to YAML: %w", err)
}
return nil
}
func saveToJSON(filaname string, v interface{}) error {
result, err := os.Create(filaname)
if err != nil {
return fmt.Errorf("cannot open result file: %w", err)
}
defer result.Close()
if err := json.NewEncoder(result).Encode(v); err != nil {
return fmt.Errorf("cannot marshal to JSON: %w", err)
}
return nil
}
type stringSliceFlag []string
func (ss *stringSliceFlag) String() string {
return strings.Join(*ss, ",")
}
func (ss *stringSliceFlag) Set(value string) error {
*ss = append(*ss, value)
return nil
}