-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from vektah/feat-init
introduce gen & init subcommand
- Loading branch information
Showing
7 changed files
with
341 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/vektah/gqlgen/codegen" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(initCmd) | ||
} | ||
|
||
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 = &cobra.Command{ | ||
Use: "init", | ||
Short: "Generate .gqlgen.yml", | ||
Long: "", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
initConfig() | ||
initSchema() | ||
}, | ||
} | ||
|
||
func initConfig() { | ||
var config *codegen.Config | ||
var err error | ||
if configFilename != "" { | ||
config, err = codegen.LoadConfig(configFilename) | ||
} else { | ||
config, err = codegen.LoadDefaultConfig() | ||
} | ||
if os.IsNotExist(errors.Cause(err)) { | ||
// ok | ||
if configFilename == "" { | ||
configFilename = ".gqlgen.yml" | ||
} | ||
config = &codegen.Config{ | ||
SchemaFilename: "schema.graphql", | ||
Model: codegen.PackageConfig{Filename: "models_gen.go"}, | ||
Exec: codegen.PackageConfig{Filename: "generated.go"}, | ||
} | ||
|
||
} else if !os.IsNotExist(errors.Cause(err)) { | ||
fmt.Fprintln(os.Stderr, "config file is already exists") | ||
os.Exit(0) | ||
} else if err != nil { | ||
fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
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() | ||
} | ||
|
||
var buf bytes.Buffer | ||
buf.WriteString(strings.TrimSpace(configComment)) | ||
buf.WriteString("\n\n") | ||
{ | ||
var b []byte | ||
b, err = yaml.Marshal(config) | ||
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 config file: "+err.Error()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func initSchema() { | ||
if schemaFilename == "" { | ||
schemaFilename = "schema.graphql" | ||
} | ||
|
||
_, 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"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 | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().StringVarP(&configFilename, "config", "c", "", "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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.