Skip to content

Commit

Permalink
Merge pull request #229 from vektah/fix-init-command
Browse files Browse the repository at this point in the history
Fixing init command
  • Loading branch information
creativej authored Jul 30, 2018
2 parents 803711e + 078bc98 commit 7031264
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
18 changes: 12 additions & 6 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/vektah/gqlgen/codegen"
"gopkg.in/yaml.v2"
Expand All @@ -19,17 +20,22 @@ var genCmd = &cobra.Command{
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)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else {
config, err = codegen.LoadDefaultConfig()
}
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
config, err = codegen.LoadConfigFromDefaultLocations()
if os.IsNotExist(errors.Cause(err)) {
config = codegen.DefaultConfig()
} else if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}

// overwrite by commandline options
Expand Down
39 changes: 20 additions & 19 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ var initCmd = &cobra.Command{
Short: "Generate .gqlgen.yml",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
initConfig()
initSchema()
initConfig()
},
}

Expand All @@ -70,26 +70,27 @@ func initConfig() {
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"
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
} else if config != nil {
fmt.Fprintln(os.Stderr, "config file is already exists")
os.Exit(0)
}
config = &codegen.Config{
SchemaFilename: "schema.graphql",
Model: codegen.PackageConfig{Filename: "models_gen.go"},
Exec: codegen.PackageConfig{Filename: "generated.go"},
} else {
config, err = codegen.LoadConfigFromDefaultLocations()
if os.IsNotExist(errors.Cause(err)) {
if configFilename == "" {
configFilename = ".gqlgen.yml"
}
config = codegen.DefaultConfig()
} else if config != nil {
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)
}

} 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 != "" {
Expand Down
34 changes: 20 additions & 14 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ import (
"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
// DefaultConfig creates a copy of the default config
func DefaultConfig() *Config {
return &Config{
SchemaFilename: "schema.graphql",
Model: PackageConfig{Filename: "models_gen.go"},
Exec: PackageConfig{Filename: "generated.go"},
}
}

// LoadConfigFromDefaultLocations 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) {
func LoadConfigFromDefaultLocations() (*Config, error) {
cfgFile, err := findCfg()
if err != nil || cfgFile == "" {
cpy := defaults
return &cpy, err
if err != nil {
return nil, err
}

err = os.Chdir(filepath.Dir(cfgFile))
Expand All @@ -39,18 +41,18 @@ func LoadDefaultConfig() (*Config, error) {

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

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

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

return &config, nil
return config, nil
}

type Config struct {
Expand Down Expand Up @@ -170,6 +172,10 @@ func findCfg() (string, error) {
cfg = findCfgInDir(dir)
}

if cfg == "" {
return "", os.ErrNotExist
}

return cfg, nil
}

Expand Down
11 changes: 5 additions & 6 deletions codegen/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLoadDefaultConfig(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "tests", "cfg", "subdir"))
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
cfg, err = LoadConfigFromDefaultLocations()
require.NoError(t, err)
require.Equal(t, cfg.SchemaFilename, "inner")
})
Expand All @@ -45,18 +45,17 @@ func TestLoadDefaultConfig(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "tests", "cfg", "otherdir"))
require.NoError(t, err)

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

t.Run("will fallback to defaults", func(t *testing.T) {
t.Run("will return error if config doesn't exist", 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")
cfg, err = LoadConfigFromDefaultLocations()
require.True(t, os.IsNotExist(err))
})
}

Expand Down

0 comments on commit 7031264

Please sign in to comment.