diff --git a/cmd/gen.go b/cmd/gen.go index e00f65590e0..d2ec8ba3452 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "github.com/99designs/gqlgen/api" @@ -23,8 +22,6 @@ var genCmd = &cli.Command{ if configFilename := ctx.String("config"); configFilename != "" { cfg, err = config.LoadConfig(configFilename) if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(1) return err } } else { @@ -32,15 +29,11 @@ var genCmd = &cli.Command{ if os.IsNotExist(errors.Cause(err)) { cfg = config.DefaultConfig() } else if err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(2) return err } } if err = api.Generate(cfg); err != nil { - fmt.Fprintln(os.Stderr, err.Error()) - os.Exit(3) return err } return nil diff --git a/cmd/init.go b/cmd/init.go index 084146e2d3f..121805af539 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -120,13 +120,16 @@ var initCmd = &cli.Command{ pkgName := code.ImportPathForDir(".") if pkgName == "" { - fmt.Fprintln(os.Stderr, "unable to determine import path for current directory, you probably need to run go mod init first") - os.Exit(1) + return fmt.Errorf("unable to determine import path for current directory, you probably need to run go mod init first") } - initSchema(ctx.String("schema")) + if err := initSchema(ctx.String("schema")); err != nil { + return err + } if !configExists(configFilename) { - initConfig(configFilename, pkgName) + if err := initConfig(configFilename, pkgName); err != nil { + return err + } } GenerateGraphServer(serverFilename) @@ -158,14 +161,13 @@ func configExists(configFilename string) bool { return cfg != nil } -func initConfig(configFilename string, pkgName string) { +func initConfig(configFilename string, pkgName string) error { if configFilename == "" { configFilename = "gqlgen.yml" } if err := os.MkdirAll(filepath.Dir(configFilename), 0755); err != nil { - fmt.Fprintln(os.Stderr, "unable to create config dir: "+err.Error()) - os.Exit(1) + return fmt.Errorf("unable to create config dir: " + err.Error()) } var buf bytes.Buffer @@ -174,24 +176,24 @@ func initConfig(configFilename string, pkgName string) { } if err := ioutil.WriteFile(configFilename, buf.Bytes(), 0644); err != nil { - fmt.Fprintln(os.Stderr, "unable to write cfg file: "+err.Error()) - os.Exit(1) + return fmt.Errorf("unable to write cfg file: " + err.Error()) } + + return nil } -func initSchema(schemaFilename string) { +func initSchema(schemaFilename string) error { _, err := os.Stat(schemaFilename) if !os.IsNotExist(err) { - return + return nil } if err := os.MkdirAll(filepath.Dir(schemaFilename), 0755); err != nil { - fmt.Fprintln(os.Stderr, "unable to create schema dir: "+err.Error()) - os.Exit(1) + return fmt.Errorf("unable to create schema dir: " + err.Error()) } if err = ioutil.WriteFile(schemaFilename, []byte(strings.TrimSpace(schemaDefault)), 0644); err != nil { - fmt.Fprintln(os.Stderr, "unable to write schema file: "+err.Error()) - os.Exit(1) + return fmt.Errorf("unable to write schema file: " + err.Error()) } + return nil }