Skip to content

Commit

Permalink
remove os.Exits
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Mar 4, 2020
1 parent 294884a commit cd2b53f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
7 changes: 0 additions & 7 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/99designs/gqlgen/api"
Expand All @@ -23,24 +22,18 @@ 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 {
cfg, err = config.LoadConfigFromDefaultLocations()
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
Expand Down
32 changes: 17 additions & 15 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit cd2b53f

Please sign in to comment.