From d7241728f83cbb8e524ce2e3d765c022a466f5c5 Mon Sep 17 00:00:00 2001 From: Mathew Byrne Date: Mon, 6 Aug 2018 13:17:20 +1000 Subject: [PATCH] Better error on init if file exists --- cmd/init.go | 20 +++++++++++--------- codegen/config.go | 4 ++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 6ed1eb4e478..fc709d6df4f 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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", diff --git a/codegen/config.go b/codegen/config.go index 0b6f98da435..0ba1f65dabc 100644 --- a/codegen/config.go +++ b/codegen/config.go @@ -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 } @@ -63,6 +65,8 @@ type Config struct { Resolver PackageConfig `yaml:"resolver,omitempty"` Models TypeMap `yaml:"models,omitempty"` + FilePath string `yaml:"-"` + schema *ast.Schema `yaml:"-"` }