Skip to content

Commit

Permalink
support init subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Jul 21, 2018
1 parent c581017 commit eff49d0
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions cmd/init.go
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)
}
}

0 comments on commit eff49d0

Please sign in to comment.