From a0158a4edd009fbfd6f67a1ec63d1b69c56b719b Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 27 Aug 2018 17:16:15 +1000 Subject: [PATCH] Drop old cli flags --- Gopkg.lock | 48 ++++++++++++---------------------- cmd/gen.go | 38 +++++++-------------------- cmd/init.go | 58 +++++++++++++---------------------------- cmd/root.go | 62 ++++++++++++++++---------------------------- example/todo/todo.go | 2 +- 5 files changed, 67 insertions(+), 141 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index a10062414a5..7399021ad65 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,7 +26,7 @@ version = "v3.3.2" [[projects]] - digest = "1:f3df613325a793ffb3d0ce7644a3bb6f62db45ac744dafe20172fe999c61cdbf" + digest = "1:78907d832e27dbfc6e3fdfc52bd2e5e2e05c1d0e3789d4825b824489fbeab233" name = "github.com/gogo/protobuf" packages = [ "io", @@ -71,14 +71,6 @@ pruneopts = "UT" revision = "0fb14efe8c47ae851c0034ed7a448854d3d34cf3" -[[projects]] - digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be" - name = "github.com/inconshreveable/mousetrap" - packages = ["."] - pruneopts = "UT" - revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75" - version = "v1.0" - [[projects]] branch = "master" digest = "1:ab9cfaf00fc5db5fd9d8e5f33da52e62bcc977d1976503dcc2a1492f391bd9ed" @@ -107,7 +99,7 @@ version = "v1.0.0" [[projects]] - digest = "1:450b7623b185031f3a456801155c8320209f75d0d4c4e633c6b1e59d44d6e392" + digest = "1:27af6024faa3c28426a698b8c653be0fd908bc96e25b7d76f2192eb342427db6" name = "github.com/opentracing/opentracing-go" packages = [ ".", @@ -151,23 +143,7 @@ revision = "ffb13db8def02f545acc58bd288ec6057c2bbfb9" [[projects]] - digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939" - name = "github.com/spf13/cobra" - packages = ["."] - pruneopts = "UT" - revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385" - version = "v0.0.3" - -[[projects]] - digest = "1:9424f440bba8f7508b69414634aef3b2b3a877e522d8a4624692412805407bb7" - name = "github.com/spf13/pflag" - packages = ["."] - pruneopts = "UT" - revision = "583c0c0531f06d5278b7d917446061adc344b5cd" - version = "v1.0.1" - -[[projects]] - digest = "1:7e8d267900c7fa7f35129a2a37596e38ed0f11ca746d6d9ba727980ee138f9f6" + digest = "1:73697231b93fb74a73ebd8384b68b9a60c57ea6b13c56d2425414566a72c8e6d" name = "github.com/stretchr/testify" packages = [ "assert", @@ -177,6 +153,14 @@ revision = "12b6f73e6084dad08a7c6e575284b177ecafbc71" version = "v1.2.1" +[[projects]] + digest = "1:b24d38b282bacf9791408a080f606370efa3d364e4b5fd9ba0f7b87786d3b679" + name = "github.com/urfave/cli" + packages = ["."] + pruneopts = "UT" + revision = "cfb38830724cc34fedffe9a2a29fb54fa9169cd1" + version = "v1.20.0" + [[projects]] branch = "master" digest = "1:3684f335059f8441260c3e949c0b99de97ea1057ecc493fdbd72ff3c27863277" @@ -187,7 +171,7 @@ [[projects]] branch = "master" - digest = "1:c6b203aeee848da05afba3a2dfaa47bb45668b1511f238e0cf5178e50c9607a2" + digest = "1:3e2c3d0b8cb780a09467884bebc032c831ae273d1d993b8f55fa61084e37278c" name = "github.com/vektah/gqlparser" packages = [ ".", @@ -211,7 +195,7 @@ [[projects]] branch = "master" - digest = "1:3cbc05413b8aac22b1f6d4350ed696b5a83a8515a4136db8f1ec3a0aee3d76e1" + digest = "1:77fe642412bfed48743e2b75163e3ab5c430cfe22dd488788647b89b28794635" name = "golang.org/x/tools" packages = [ "go/ast/astutil", @@ -232,7 +216,7 @@ [[projects]] branch = "master" - digest = "1:741ebea9214cc226789d3003baeca9b169e04b5b336fb1a3b2c16e75bd296bb5" + digest = "1:7ddb3a7b35cc853fe0db36a1b2473bdff03f28add7d28e4725e692603111266e" name = "sourcegraph.com/sourcegraph/appdash" packages = [ ".", @@ -248,7 +232,7 @@ [[projects]] branch = "master" - digest = "1:8e0a2957fe342f22d70a543c3fcdf390f7627419c3d82d87ab4fd715a9ef5716" + digest = "1:be108b48d79c3b3c345811a57a47ee87fdbe895beb4bb56239da71d4943e5be7" name = "sourcegraph.com/sourcegraph/appdash-data" packages = ["."] pruneopts = "UT" @@ -267,9 +251,9 @@ "github.com/opentracing/opentracing-go/ext", "github.com/opentracing/opentracing-go/log", "github.com/pkg/errors", - "github.com/spf13/cobra", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", + "github.com/urfave/cli", "github.com/vektah/dataloaden", "github.com/vektah/gqlparser", "github.com/vektah/gqlparser/ast", diff --git a/cmd/gen.go b/cmd/gen.go index a941eda8693..0999b9de922 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -7,21 +7,20 @@ import ( "github.com/99designs/gqlgen/codegen" "github.com/pkg/errors" - "github.com/spf13/cobra" + "github.com/urfave/cli" ) -func init() { - rootCmd.AddCommand(genCmd) -} - -var genCmd = &cobra.Command{ - Use: "gen", - Short: "Generate models & resolvers .go", - Long: "", - Run: func(cmd *cobra.Command, args []string) { +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 != "" { + if configFilename := ctx.String("config"); configFilename != "" { config, err = codegen.LoadConfig(configFilename) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) @@ -37,23 +36,6 @@ var genCmd = &cobra.Command{ } } - // overwrite by commandline options - if schemaFilename != "" { - config.SchemaFilename = schemaFilename - } - if models != "" { - config.Model.Filename = models - } - if output != "" { - config.Exec.Filename = output - } - if packageName != "" { - config.Exec.Package = packageName - } - if modelPackageName != "" { - config.Model.Package = modelPackageName - } - schemaRaw, err := ioutil.ReadFile(config.SchemaFilename) if err != nil { fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error()) diff --git a/cmd/init.go b/cmd/init.go index f8b31945cfd..2452510a852 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -9,14 +9,10 @@ import ( "github.com/99designs/gqlgen/codegen" "github.com/pkg/errors" - "github.com/spf13/cobra" + "github.com/urfave/cli" "gopkg.in/yaml.v2" ) -func init() { - rootCmd.AddCommand(initCmd) -} - var configComment = ` # .gqlgen.yml example # @@ -55,19 +51,24 @@ type Mutation { } ` -var initCmd = &cobra.Command{ - Use: "init", - Short: "Generate gqlgen skeleton", - Long: "", - Run: func(cmd *cobra.Command, args []string) { - initSchema() - config := initConfig() +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) + GenerateGraphServer(config, ctx.String("server")) }, } -func GenerateGraphServer(config *codegen.Config) { +func GenerateGraphServer(config *codegen.Config, serverFilename string) { schemaRaw, err := ioutil.ReadFile(config.SchemaFilename) if err != nil { fmt.Fprintln(os.Stderr, "unable to open schema: "+err.Error()) @@ -80,10 +81,6 @@ func GenerateGraphServer(config *codegen.Config) { os.Exit(1) } - if serverFilename == "" { - serverFilename = "server/server.go" - } - if err := codegen.Generate(*config); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) @@ -97,9 +94,10 @@ func GenerateGraphServer(config *codegen.Config) { fmt.Fprintf(os.Stdout, "Exec \"go run ./%s\" to start GraphQL server\n", serverFilename) } -func initConfig() *codegen.Config { +func initConfig(ctx *cli.Context) *codegen.Config { var config *codegen.Config var err error + configFilename := ctx.String("config") if configFilename != "" { config, err = codegen.LoadConfig(configFilename) } else { @@ -126,22 +124,6 @@ func initConfig() *codegen.Config { Type: "Resolver", } - if schemaFilename != "" { - config.SchemaFilename = schemaFilename - } - if models != "" { - config.Model.Filename = models - } - if output != "" { - config.Exec.Filename = output - } - if packageName != "" { - config.Exec.Package = packageName - } - if modelPackageName != "" { - config.Model.Package = modelPackageName - } - var buf bytes.Buffer buf.WriteString(strings.TrimSpace(configComment)) buf.WriteString("\n\n") @@ -164,11 +146,7 @@ func initConfig() *codegen.Config { return config } -func initSchema() { - if schemaFilename == "" { - schemaFilename = "schema.graphql" - } - +func initSchema(schemaFilename string) { _, err := os.Stat(schemaFilename) if !os.IsNotExist(err) { return diff --git a/cmd/root.go b/cmd/root.go index 1e9894cedbf..394f370b492 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,59 +7,41 @@ import ( "os" "github.com/99designs/gqlgen/internal/gopath" - "github.com/spf13/cobra" + "github.com/urfave/cli" ) -var configFilename string -var verbose bool - -var output string -var models string -var schemaFilename string -var packageName string -var modelPackageName string -var serverFilename string - -func init() { - rootCmd.PersistentFlags().StringVarP(&configFilename, "config", "c", "", "the file to configuration to") - rootCmd.PersistentFlags().StringVarP(&serverFilename, "server", "s", "", "the file to write server to") - rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "show logs") - - rootCmd.PersistentFlags().StringVar(&output, "out", "", "the file to write to") - rootCmd.PersistentFlags().StringVar(&models, "models", "", "the file to write the models to") - rootCmd.PersistentFlags().StringVar(&schemaFilename, "schema", "", "the graphql schema to generate types from") - rootCmd.PersistentFlags().StringVar(&packageName, "package", "", "the package name") - rootCmd.PersistentFlags().StringVar(&modelPackageName, "modelpackage", "", "the package name to use for models") -} - -var rootCmd = &cobra.Command{ - Use: "gqlgen", - Short: "go generate based graphql server library", - Long: `This is a library for quickly creating strictly typed graphql servers in golang. - See https://gqlgen.com/ for a getting started guide.`, - PersistentPreRun: func(cmd *cobra.Command, args []string) { +func Execute() { + app := cli.NewApp() + app.Name = "gqlgen" + app.Usage = genCmd.Usage + app.Description = "This is a library for quickly creating strictly typed graphql servers in golang. See https://gqlgen.com/ for a getting started guide." + app.HideVersion = true + app.Flags = genCmd.Flags + app.Before = func(context *cli.Context) error { pwd, err := os.Getwd() if err != nil { - fmt.Fprintf(os.Stderr, "unable to determine current workding dir: %s\n", err.Error()) - os.Exit(1) + return fmt.Errorf("unable to determine current workding dir: %s\n", err.Error()) } if !gopath.Contains(pwd) { - fmt.Fprintf(os.Stderr, "gqlgen must be run from inside your $GOPATH\n") - os.Exit(1) + return fmt.Errorf("gqlgen must be run from inside your $GOPATH\n") } - if verbose { + if context.Bool("verbose") { log.SetFlags(0) } else { log.SetOutput(ioutil.Discard) } - }, - Run: genCmd.Run, // delegate to gen subcommand -} + return nil + } -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) + app.Action = genCmd.Action + app.Commands = []cli.Command{ + genCmd, + initCmd, + } + + if err := app.Run(os.Args); err != nil { + fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } } diff --git a/example/todo/todo.go b/example/todo/todo.go index dc51b3e8f69..05846810e2b 100644 --- a/example/todo/todo.go +++ b/example/todo/todo.go @@ -1,4 +1,4 @@ -//go:generate gorunpkg github.com/99designs/gqlgen --out generated.go +//go:generate gorunpkg github.com/99designs/gqlgen package todo