Skip to content

Commit

Permalink
Better error on init if file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Aug 6, 2018
1 parent fb03bad commit de0dc3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,25 @@ func initConfig() *codegen.Config {
var err error
if configFilename != "" {
config, err = codegen.LoadConfig(configFilename)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
return config
} else {
config, err = codegen.LoadConfigFromDefaultLocations()
}

config, err = codegen.LoadConfigFromDefaultLocations()
if config != nil {
return config
} else if !os.IsNotExist(errors.Cause(err)) {
fmt.Fprintf(os.Stderr, "init failed: a configuration file already exists at %s\n", config.FilePath)
os.Exit(1)
}

if !os.IsNotExist(errors.Cause(err)) {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

configFilename = "gqlgen.yml"
if configFilename != "" {
configFilename = "gqlgen.yml"
}
config = codegen.DefaultConfig()

config.Resolver = codegen.PackageConfig{
Filename: "resolver.go",
Type: "Resolver",
Expand Down
4 changes: 4 additions & 0 deletions codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func LoadConfig(filename string) (*Config, error) {
return nil, errors.Wrap(err, "unable to parse config")
}

config.FilePath = filename

return config, nil
}

Expand All @@ -63,6 +65,8 @@ type Config struct {
Resolver PackageConfig `yaml:"resolver,omitempty"`
Models TypeMap `yaml:"models,omitempty"`

FilePath string `yaml:"-"`

schema *ast.Schema `yaml:"-"`
}

Expand Down

0 comments on commit de0dc3d

Please sign in to comment.