From efb31b54320f1311aec963fd751cfee874c353cf Mon Sep 17 00:00:00 2001 From: soeti <63041298+soeti@users.noreply.github.com> Date: Fri, 25 Nov 2022 23:42:26 +0200 Subject: [PATCH] Check if go.mod exists while init (#2432) * Add check go.mod first to prevent cascade errors in "init" directive * Fix formatting * Fix formatting with gofmt * Security update go.mod golang.org/x/text@v0.3.7 to v0.3.8 * Revert "Security update go.mod golang.org/x/text@v0.3.7 to v0.3.8" This reverts commit c23d183d9da4e33993e600beefcccd1fc4ec6264. * Security update go.mod golang.org/x/text@v0.3.7 to v0.3.8 * Adjust go.mod file to look in parent directories as well Signed-off-by: Steve Coffman Signed-off-by: Steve Coffman Co-authored-by: Steve Coffman --- _examples/go.mod | 2 +- _examples/go.sum | 3 ++- go.mod | 2 +- go.sum | 3 ++- main.go | 54 ++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/_examples/go.mod b/_examples/go.mod index 5f87adc514..a0debb38d1 100644 --- a/_examples/go.mod +++ b/_examples/go.mod @@ -23,6 +23,6 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect - golang.org/x/text v0.3.7 + golang.org/x/text v0.3.8 sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 // indirect ) diff --git a/_examples/go.sum b/_examples/go.sum index 58cd0bb709..d57ef21925 100644 --- a/_examples/go.sum +++ b/_examples/go.sum @@ -133,8 +133,9 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/go.mod b/go.mod index 7c4b827c6a..0e51ac4feb 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/stretchr/testify v1.7.1 github.com/urfave/cli/v2 v2.8.1 github.com/vektah/gqlparser/v2 v2.5.1 - golang.org/x/text v0.3.7 + golang.org/x/text v0.3.8 golang.org/x/tools v0.1.12 google.golang.org/protobuf v1.28.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 7905676342..3f9b69b51e 100644 --- a/go.sum +++ b/go.sum @@ -81,8 +81,9 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= diff --git a/main.go b/main.go index b545090755..e0420d1502 100644 --- a/main.go +++ b/main.go @@ -12,12 +12,13 @@ import ( "os" "path/filepath" + "github.com/urfave/cli/v2" + "github.com/99designs/gqlgen/api" "github.com/99designs/gqlgen/codegen/config" "github.com/99designs/gqlgen/graphql" "github.com/99designs/gqlgen/internal/code" "github.com/99designs/gqlgen/plugin/servergen" - "github.com/urfave/cli/v2" ) //go:embed init-templates/schema.graphqls @@ -39,6 +40,28 @@ func fileExists(filename string) bool { return !errors.Is(err, fs.ErrNotExist) } +// see Go source code: +// https://github.com/golang/go/blob/f57ebed35132d02e5cf016f324853217fb545e91/src/cmd/go/internal/modload/init.go#L1283 +func findModuleRoot(dir string) (roots string) { + if dir == "" { + panic("dir not set") + } + dir = filepath.Clean(dir) + + // Look for enclosing go.mod. + for { + if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() { + return dir + } + d := filepath.Dir(dir) + if d == dir { // the parent of the root is itself, so we can go no further + break + } + dir = d + } + return "" +} + func initFile(filename, contents string) error { if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil { return fmt.Errorf("unable to create directory for file '%s': %w\n", filename, err) @@ -56,17 +79,36 @@ var initCmd = &cli.Command{ Flags: []cli.Flag{ &cli.BoolFlag{Name: "verbose, v", Usage: "show logs"}, &cli.StringFlag{Name: "config, c", Usage: "the config filename", Value: "gqlgen.yml"}, - &cli.StringFlag{Name: "server", Usage: "where to write the server stub to", Value: "server.go"}, - &cli.StringFlag{Name: "schema", Usage: "where to write the schema stub to", Value: "graph/schema.graphqls"}, + &cli.StringFlag{ + Name: "server", + Usage: "where to write the server stub to", + Value: "server.go", + }, + &cli.StringFlag{ + Name: "schema", + Usage: "where to write the schema stub to", + Value: "graph/schema.graphqls", + }, }, Action: func(ctx *cli.Context) error { configFilename := ctx.String("config") serverFilename := ctx.String("server") schemaFilename := ctx.String("schema") - pkgName := code.ImportPathForDir(".") + cwd, err := os.Getwd() + if err != nil { + log.Println(err) + return fmt.Errorf("unable to determine current directory:%w", err) + } + pkgName := code.ImportPathForDir(cwd) if pkgName == "" { - return fmt.Errorf("unable to determine import path for current directory, you probably need to run 'go mod init' first") + return fmt.Errorf( + "unable to determine import path for current directory, you probably need to run 'go mod init' first", + ) + } + modRoot := findModuleRoot(cwd) + if modRoot == "" { + return fmt.Errorf("go.mod is missing. Please, do 'go mod init' first\n") } // check schema and config don't already exist @@ -75,7 +117,7 @@ var initCmd = &cli.Command{ return fmt.Errorf("%s already exists", filename) } } - _, err := config.LoadConfigFromDefaultLocations() + _, err = config.LoadConfigFromDefaultLocations() if err == nil { return fmt.Errorf("gqlgen.yml already exists in a parent directory\n") }