diff options
author | Michael Muré <batolettre@gmail.com> | 2019-07-09 13:22:59 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-07-09 13:22:59 +0200 |
commit | 14022953823fc768849b6cc826b05b61a89b7626 (patch) | |
tree | 4d4f70ee46b9cc230d6602cb066c697f8a0a4e8e /vendor/github.com/99designs/gqlgen/cmd/init.go | |
parent | a5c42b7c11fc082d47027ba5e0dbdeddcc14e62e (diff) | |
download | git-bug-14022953823fc768849b6cc826b05b61a89b7626.tar.gz |
graphql: don't use the gqlgen command to generate to avoid pulling urfave/cli
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/cmd/init.go')
-rw-r--r-- | vendor/github.com/99designs/gqlgen/cmd/init.go | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/vendor/github.com/99designs/gqlgen/cmd/init.go b/vendor/github.com/99designs/gqlgen/cmd/init.go deleted file mode 100644 index e07bed97..00000000 --- a/vendor/github.com/99designs/gqlgen/cmd/init.go +++ /dev/null @@ -1,144 +0,0 @@ -package cmd - -import ( - "bytes" - "fmt" - "io/ioutil" - "os" - "strings" - - "github.com/99designs/gqlgen/api" - "github.com/99designs/gqlgen/plugin/servergen" - - "github.com/99designs/gqlgen/codegen/config" - "github.com/pkg/errors" - "github.com/urfave/cli" - yaml "gopkg.in/yaml.v2" -) - -var configComment = ` -# .gqlgen.yml example -# -# Refer to https://gqlgen.com/config/ -# for detailed .gqlgen.yml documentation. -` - -var schemaDefault = ` -# GraphQL schema example -# -# https://gqlgen.com/getting-started/ - -type Todo { - id: ID! - text: String! - done: Boolean! - user: User! -} - -type User { - id: ID! - name: String! -} - -type Query { - todos: [Todo!]! -} - -input NewTodo { - text: String! - userId: String! -} - -type Mutation { - createTodo(input: NewTodo!): Todo! -} -` - -var initCmd = cli.Command{ - Name: "init", - Usage: "create a new gqlgen project", - Flags: []cli.Flag{ - cli.BoolFlag{Name: "verbose, v", Usage: "show logs"}, - cli.StringFlag{Name: "config, c", Usage: "the config filename"}, - cli.StringFlag{Name: "server", Usage: "where to write the server stub to", Value: "server/server.go"}, - cli.StringFlag{Name: "schema", Usage: "where to write the schema stub to", Value: "schema.graphql"}, - }, - Action: func(ctx *cli.Context) { - initSchema(ctx.String("schema")) - config := initConfig(ctx) - - GenerateGraphServer(config, ctx.String("server")) - }, -} - -func GenerateGraphServer(cfg *config.Config, serverFilename string) { - err := api.Generate(cfg, api.AddPlugin(servergen.New(serverFilename))) - if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - } - - fmt.Fprintf(os.Stdout, "Exec \"go run ./%s\" to start GraphQL server\n", serverFilename) -} - -func initConfig(ctx *cli.Context) *config.Config { - var cfg *config.Config - var err error - configFilename := ctx.String("config") - if configFilename != "" { - cfg, err = config.LoadConfig(configFilename) - } else { - cfg, err = config.LoadConfigFromDefaultLocations() - } - - if cfg != nil { - fmt.Fprintf(os.Stderr, "init failed: a configuration file already exists\n") - os.Exit(1) - } - - if !os.IsNotExist(errors.Cause(err)) { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) - } - - if configFilename == "" { - configFilename = "gqlgen.yml" - } - cfg = config.DefaultConfig() - - cfg.Resolver = config.PackageConfig{ - Filename: "resolver.go", - Type: "Resolver", - } - - var buf bytes.Buffer - buf.WriteString(strings.TrimSpace(configComment)) - buf.WriteString("\n\n") - var b []byte - b, err = yaml.Marshal(cfg) - if err != nil { - fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error()) - os.Exit(1) - } - buf.Write(b) - - err = ioutil.WriteFile(configFilename, buf.Bytes(), 0644) - if err != nil { - fmt.Fprintln(os.Stderr, "unable to write cfg file: "+err.Error()) - os.Exit(1) - } - - return cfg -} - -func initSchema(schemaFilename string) { - _, err := os.Stat(schemaFilename) - if !os.IsNotExist(err) { - return - } - - err = ioutil.WriteFile(schemaFilename, []byte(strings.TrimSpace(schemaDefault)), 0644) - if err != nil { - fmt.Fprintln(os.Stderr, "unable to write schema file: "+err.Error()) - os.Exit(1) - } -} |