forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gengraphql.go
executable file
·123 lines (102 loc) · 2.5 KB
/
gengraphql.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/sensu/sensu-go/graphql/generator"
)
const (
packageNameDefault = "schema"
debuggingDefault = false
)
var logger = logrus.WithField("component", "scripts/gengraphql")
func main() { // nolint
// Parse
config := parseArgs(os.Args)
// Configure logger
if config.debug {
logger.Logger.Level = logrus.DebugLevel
logger.
WithFields(logrus.Fields{
"path": config.path,
"debug": config.debug,
"package-name": config.pkgName,
}).
Debug("configured")
}
// Find GraphQL files
files, err := ioutil.ReadDir(config.path)
if err != nil {
logger.Fatal(err)
}
for i := 0; i < len(files); {
if present := strings.HasSuffix(files[i].Name(), "graphql"); present {
logger.WithField("file", files[i].Name()).Debug("graphql file found")
i++
} else {
files = append(files[:i], files[i+1:]...)
}
}
// Parse
logger.Info("parsing .graphql files")
graphqlFiles := make(generator.GraphQLFiles, len(files))
for i, f := range files {
log := logger.WithField("file", f.Name())
filePath := filepath.Join(config.path, f.Name())
file, err := generator.ParseFile(filePath)
if err != nil {
log.WithError(err).Fatal("unable to parse file; check syntax")
}
if err := file.Validate(); err != nil {
log.Fatal(err)
}
log.Debug("file parsed successfully")
graphqlFiles[i] = file
}
// Generate
generator := generator.New(graphqlFiles)
generator.PackageName = config.pkgName
generator.Invoker = "scripts/gengraphql.go"
if err := generator.Run(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
logger.Fatal("unable to generate file")
}
logger.Info("files written successfully")
}
type config struct {
path string
pkgName string
debug bool
}
func parseArgs(args []string) config {
c := config{}
cmd := flag.NewFlagSet(args[0], flag.ExitOnError)
cmd.StringVar(&c.pkgName, "P", packageNameDefault, "name of the package")
cmd.BoolVar(&c.debug, "debug", debuggingDefault, "print debug messages")
cmd.Usage = func() {
_, err := fmt.Fprint(os.Stderr, "Usage: gengraphql [OPTIONS] DIRECTORY\n\n")
if err != nil {
logger.Fatal("unable to write usage")
}
cmd.PrintDefaults()
}
if len(args) < 2 {
cmd.Usage()
os.Exit(1)
}
if err := cmd.Parse(args[1:]); err != nil {
cmd.Usage()
os.Exit(1)
}
// Ensure directory argument was given
if cmd.NArg() < 1 {
cmd.Usage()
os.Exit(1)
}
c.path = cmd.Arg(0)
return c
}