Skip to content

Commit

Permalink
introduce cobra library
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Jul 21, 2018
1 parent 90df37f commit c581017
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 122 deletions.
109 changes: 109 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package cmd

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

"github.com/spf13/cobra"
"github.com/vektah/gqlgen/codegen"
"gopkg.in/yaml.v2"
)

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 config *codegen.Config
var err error
if configFilename != "" {
config, err = codegen.LoadConfig(configFilename)
} else {
config, err = codegen.LoadDefaultConfig()
}
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

// overwrite by commandline options
var emitYamlGuidance bool
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
}
if typemap != "" {
config.Models = loadModelMap()
emitYamlGuidance = true
}

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

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

if emitYamlGuidance {
var b []byte
b, err = yaml.Marshal(config)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error())
os.Exit(1)
}

fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: we are moving away from the json typemap, instead create a gqlgen.yml with the following content:\n\n%s\n", string(b))
}

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

func loadModelMap() codegen.TypeMap {
var goTypes map[string]string
b, err := ioutil.ReadFile(typemap)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open typemap: "+err.Error())
return nil
}

if err = yaml.Unmarshal(b, &goTypes); err != nil {
fmt.Fprintln(os.Stderr, "unable to parse typemap: "+err.Error())
os.Exit(1)
}

typeMap := make(codegen.TypeMap)
for typeName, entityPath := range goTypes {
typeMap[typeName] = codegen.TypeMapEntry{
Model: entityPath,
}
}

return typeMap
}
57 changes: 57 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

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

"github.com/spf13/cobra"
)

var configFilename string
var verbose bool

var output string
var models string
var schemaFilename string
var typemap string
var packageName string
var modelPackageName string

var help = flag.Bool("h", false, "this usage text")

func init() {
rootCmd.PersistentFlags().StringVar(&configFilename, "config", "", "the file to configuration 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(&typemap, "typemap", "", "a json map going from graphql to golang types")
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) {
if verbose {
log.SetFlags(0)
} else {
log.SetOutput(ioutil.Discard)
}
},
Run: genCmd.Run, // delegate to gen subcommand
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
124 changes: 2 additions & 122 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,127 +1,7 @@
package main

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

"github.com/vektah/gqlgen/codegen"
"gopkg.in/yaml.v2"
)

var configFilename = flag.String("config", "", "the file to configuration to")
var output = flag.String("out", "", "the file to write to")
var models = flag.String("models", "", "the file to write the models to")
var schemaFilename = flag.String("schema", "", "the graphql schema to generate types from")
var typemap = flag.String("typemap", "", "a json map going from graphql to golang types")
var packageName = flag.String("package", "", "the package name")
var modelPackageName = flag.String("modelpackage", "", "the package name to use for models")
var help = flag.Bool("h", false, "this usage text")
var verbose = flag.Bool("v", false, "show logs")
import "github.com/vektah/gqlgen/cmd"

func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s schema.graphql\n", os.Args[0])
flag.PrintDefaults()
}

flag.Parse()

if *help {
flag.Usage()
os.Exit(1)
}
if *verbose {
log.SetFlags(0)
} else {
log.SetOutput(ioutil.Discard)
}

var config *codegen.Config
var err error
if *configFilename != "" {
config, err = codegen.LoadConfig(*configFilename)
} else {
config, err = codegen.LoadDefaultConfig()
}
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

// overwrite by commandline options
var emitYamlGuidance bool
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
}
if *typemap != "" {
config.Models = loadModelMap()
emitYamlGuidance = true
}

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

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

if emitYamlGuidance {
var b []byte
b, err = yaml.Marshal(config)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to marshal yaml: "+err.Error())
os.Exit(1)
}

fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: we are moving away from the json typemap, instead create a gqlgen.yml with the following content:\n\n%s\n", string(b))
}

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

func loadModelMap() codegen.TypeMap {
var goTypes map[string]string
b, err := ioutil.ReadFile(*typemap)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to open typemap: "+err.Error())
return nil
}

if err = yaml.Unmarshal(b, &goTypes); err != nil {
fmt.Fprintln(os.Stderr, "unable to parse typemap: "+err.Error())
os.Exit(1)
}

typeMap := make(codegen.TypeMap)
for typeName, entityPath := range goTypes {
typeMap[typeName] = codegen.TypeMapEntry{
Model: entityPath,
}
}

return typeMap
cmd.Execute()
}

0 comments on commit c581017

Please sign in to comment.