Skip to content

Commit

Permalink
Merge pull request #322 from 99designs/drop-old-flags
Browse files Browse the repository at this point in the history
Drop old cli flags
  • Loading branch information
vektah authored Aug 28, 2018
2 parents 8c17eea + a0158a4 commit 40943c6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 141 deletions.
48 changes: 16 additions & 32 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 10 additions & 28 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
58 changes: 18 additions & 40 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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
Expand Down
62 changes: 22 additions & 40 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion example/todo/todo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gorunpkg github.com/99designs/gqlgen --out generated.go
//go:generate gorunpkg github.com/99designs/gqlgen

package todo

Expand Down

0 comments on commit 40943c6

Please sign in to comment.