forked from Khan/genqlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (81 loc) · 2.15 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
// Package generate provides programmatic access to genqlient's functionality,
// and documentation of its configuration options. For general usage
// documentation, see the project [GitHub].
//
// [GitHub]: https://github.com/suhabe/genqlient
package generate
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/alexflint/go-arg"
)
func readConfigGenerateAndWrite(configFilename string) error {
var config *Config
var err error
if configFilename != "" {
config, err = ReadAndValidateConfig(configFilename)
if err != nil {
return err
}
} else {
config, err = ReadAndValidateConfigFromDefaultLocations()
if err != nil {
return err
}
}
generated, err := Generate(config)
if err != nil {
return err
}
for filename, content := range generated {
err = os.MkdirAll(filepath.Dir(filename), 0o755)
if err != nil {
return errorf(nil,
"could not create parent directory for generated file %v: %v",
filename, err)
}
err = os.WriteFile(filename, content, 0o644)
if err != nil {
return errorf(nil, "could not write generated file %v: %v",
filename, err)
}
}
return nil
}
type cliArgs struct {
ConfigFilename string `arg:"positional" placeholder:"CONFIG" default:"" help:"path to genqlient configuration (default: genqlient.yaml in current or any parent directory)"`
Init bool `arg:"--init" help:"write out and use a default config file"`
}
func (cliArgs) Description() string {
return strings.TrimSpace(`
Generates GraphQL client code for a given schema and queries.
See https://github.com/Khan/genqlient for full documentation.
`)
}
// Main is the command-line entrypoint to genqlient; it's equivalent to calling
//
// go run github.com/suhabe/genqlient
//
// For lower-level control over genqlient's operation, see [Generate].
func Main() {
exitIfError := func(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var args cliArgs
arg.MustParse(&args)
if args.Init {
filename := args.ConfigFilename
if filename == "" {
filename = "genqlient.yaml"
}
err := initConfig(filename)
exitIfError(err)
}
err := readConfigGenerateAndWrite(args.ConfigFilename)
exitIfError(err)
}