root.go

 1package cmd
 2
 3import (
 4	"fmt"
 5	"io/ioutil"
 6	"log"
 7	"os"
 8
 9	"github.com/99designs/gqlgen/graphql"
10	"github.com/99designs/gqlgen/internal/gopath"
11	"github.com/urfave/cli"
12
13	// Required since otherwise dep will prune away these unused packages before codegen has a chance to run
14	_ "github.com/99designs/gqlgen/handler"
15)
16
17func Execute() {
18	app := cli.NewApp()
19	app.Name = "gqlgen"
20	app.Usage = genCmd.Usage
21	app.Description = "This is a library for quickly creating strictly typed graphql servers in golang. See https://gqlgen.com/ for a getting started guide."
22	app.HideVersion = true
23	app.Flags = genCmd.Flags
24	app.Version = graphql.Version
25	app.Before = func(context *cli.Context) error {
26		pwd, err := os.Getwd()
27		if err != nil {
28			return fmt.Errorf("unable to determine current workding dir: %s\n", err.Error())
29		}
30
31		if !gopath.Contains(pwd) {
32			return fmt.Errorf("gqlgen must be run from inside your $GOPATH\n")
33		}
34		if context.Bool("verbose") {
35			log.SetFlags(0)
36		} else {
37			log.SetOutput(ioutil.Discard)
38		}
39		return nil
40	}
41
42	app.Action = genCmd.Action
43	app.Commands = []cli.Command{
44		genCmd,
45		initCmd,
46		versionCmd,
47	}
48
49	if err := app.Run(os.Args); err != nil {
50		fmt.Fprintf(os.Stderr, err.Error())
51		os.Exit(1)
52	}
53}