Skip to content

Commit

Permalink
Search for config
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Jul 8, 2018
1 parent 61f3717 commit 25cfbf0
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 28 deletions.
70 changes: 70 additions & 0 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,55 @@ package codegen

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

"github.com/pkg/errors"
"github.com/vektah/gqlgen/neelance/schema"
"gopkg.in/yaml.v2"
)

var defaults = Config{
SchemaFilename: "schema.graphql",
Model: PackageConfig{Filename: "models_gen.go"},
Exec: PackageConfig{Filename: "generated.go"},
}

var cfgFilenames = []string{".gqlgen.yml", "gqlgen.yml", "gqlgen.yaml"}

// LoadDefaultConfig looks for a config file in the current directory, and all parent directories
// walking up the tree. The closest config file will be returned.
func LoadDefaultConfig() (*Config, error) {
cfgFile, err := findCfg()
if err != nil || cfgFile == "" {
cpy := defaults
return &cpy, err
}

err = os.Chdir(filepath.Dir(cfgFile))
if err != nil {
return nil, errors.Wrap(err, "unable to enter config dir")
}
return LoadConfig(cfgFile)
}

// LoadConfig reads the gqlgen.yml config file
func LoadConfig(filename string) (*Config, error) {
config := defaults

b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrap(err, "unable to read config")
}

if err := yaml.Unmarshal(b, &config); err != nil {
return nil, errors.Wrap(err, "unable to parse config")
}

return &config, nil
}

type Config struct {
SchemaFilename string `yaml:"schema,omitempty"`
SchemaStr string `yaml:"-"`
Expand Down Expand Up @@ -75,3 +118,30 @@ func (tm TypeMap) Check() error {
}
return nil
}

// findCfg searches for the config file in this directory and all parents up the tree
// looking for the closest match
func findCfg() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", errors.Wrap(err, "unable to get working dir to findCfg")
}

cfg := findCfgInDir(dir)
for cfg == "" && dir != "/" {
dir = filepath.Dir(dir)
cfg = findCfgInDir(dir)
}

return cfg, nil
}

func findCfgInDir(dir string) string {
for _, cfgName := range cfgFilenames {
path := filepath.Join(dir, cfgName)
if _, err := os.Stat(path); err == nil {
return path
}
}
return ""
}
54 changes: 54 additions & 0 deletions codegen/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package codegen

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestLoadConfig(t *testing.T) {
t.Run("config does not exist", func(t *testing.T) {
_, err := LoadConfig("doesnotexist.yml")
require.EqualError(t, err, "unable to read config: open doesnotexist.yml: no such file or directory")
})

t.Run("malformed config", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/malformedconfig.yml")
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into codegen.Config")
})
}

func TestLoadDefaultConfig(t *testing.T) {
testDir, err := os.Getwd()
require.NoError(t, err)
var cfg *Config

t.Run("will find closest match", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "subdir"))
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
require.NoError(t, err)
require.Equal(t, cfg.SchemaFilename, "inner")
})

t.Run("will find config in parent dirs", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "otherdir"))
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
require.NoError(t, err)
require.Equal(t, cfg.SchemaFilename, "outer")
})

t.Run("will fallback to defaults", func(t *testing.T) {
err = os.Chdir(testDir)
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
require.NoError(t, err)
require.Equal(t, cfg.SchemaFilename, "schema.graphql")
})
}
1 change: 1 addition & 0 deletions codegen/testdata/cfg/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
schema: outer
1 change: 1 addition & 0 deletions codegen/testdata/cfg/malformedconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdf
Empty file.
1 change: 1 addition & 0 deletions codegen/testdata/cfg/subdir/gqlgen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
schema: inner
2 changes: 1 addition & 1 deletion docs/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ go get github.com/vektah/gorunpkg

Now at the top of our graph.go:
```go
//go:generate gorunpkg gqlgen
//go:generate gorunpkg github.com/vektah/gqlgen

package graph
```
Expand Down
39 changes: 13 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"gopkg.in/yaml.v2"
)

var configFilename = flag.String("config", ".gqlgen.yml", "the file to configuration to")
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")
Expand All @@ -32,7 +32,17 @@ func main() {
os.Exit(1)
}

config := loadConfig()
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
Expand Down Expand Up @@ -76,7 +86,7 @@ func main() {
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))
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)
Expand All @@ -86,29 +96,6 @@ func main() {
}
}

func loadConfig() *codegen.Config {
config := &codegen.Config{
SchemaFilename: "schema.graphql",
Model: codegen.PackageConfig{Filename: "models_gen.go"},
Exec: codegen.PackageConfig{Filename: "generated.go"},
}

b, err := ioutil.ReadFile(*configFilename)
if os.IsNotExist(err) {
return config
} else if err != nil {
fmt.Fprintln(os.Stderr, "unable to open config: "+err.Error())
os.Exit(1)
}

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

return config
}

func loadModelMap() codegen.TypeMap {
var goTypes map[string]string
b, err := ioutil.ReadFile(*typemap)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/resolvers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gorunpkg github.com/vektah/gqlgen
//go:generate gorunpkg github.com/vektah/gqlgen -config config.yaml

package test

Expand Down

0 comments on commit 25cfbf0

Please sign in to comment.