aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/cmd/gen.go
blob: 3842f02b37da3a7d7a7280159d42a1c5bc78ddeb (plain) (blame)
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
package cmd

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/99designs/gqlgen/codegen"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var genCmd = cli.Command{
	Name:  "generate",
	Usage: "generate a graphql server based on schema",
	Flags: []cli.Flag{
		cli.BoolFlag{Name: "verbose, v", Usage: "show logs"},
		cli.StringFlag{Name: "config, c", Usage: "the config filename"},
	},
	Action: func(ctx *cli.Context) {
		var config *codegen.Config
		var err error
		if configFilename := ctx.String("config"); configFilename != "" {
			config, err = codegen.LoadConfig(configFilename)
			if err != nil {
				fmt.Fprintln(os.Stderr, err.Error())
				os.Exit(1)
			}
		} else {
			config, err = codegen.LoadConfigFromDefaultLocations()
			if os.IsNotExist(errors.Cause(err)) {
				config = codegen.DefaultConfig()
			} else if err != nil {
				fmt.Fprintln(os.Stderr, err.Error())
				os.Exit(1)
			}
		}

		for _, filename := range config.SchemaFilename {
			var schemaRaw []byte
			schemaRaw, err = ioutil.ReadFile(filename)
			if err != nil {
				fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error())
				os.Exit(1)
			}
			config.SchemaStr[filename] = string(schemaRaw)
		}

		if err = config.Check(); err != nil {
			fmt.Fprintln(os.Stderr, "invalid config format: "+err.Error())
			os.Exit(1)
		}

		err = codegen.Generate(*config)
		if err != nil {
			fmt.Fprintln(os.Stderr, err.Error())
			os.Exit(2)
		}
	},
}