diff --git a/main.go b/main.go index eb94e47d..a46c6916 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,6 @@ type config struct { goPackageName *string dynamoPath string - goPackagePath string goAbsolutePackagePath string jsClientPath string @@ -269,20 +268,20 @@ func (c *config) setGoPaths(outputPath, goPackageName string) error { return fmt.Errorf("go-package is required") } // if the repo does not use modules, the package name is equivalent to the package path - c.goPackagePath = goPackageName + c.goAbsolutePackagePath = filepath.Join(os.Getenv("GOPATH"), "src", goPackageName) } else { defer modFile.Close() - // TODO: do not rely on GOPATH when repo uses modules - goPath := os.Getenv("GOPATH") - if goPath == "" { - return fmt.Errorf("GOPATH must be set") - } if outputPath == "" { return fmt.Errorf("output-path is required") } - c.goPackagePath = getModulePackagePath(goPath, path.Clean(outputPath)) + absolutePath, err := filepath.Abs(outputPath) + if err != nil { + return fmt.Errorf("converting output-path to absolute path: %v", err) + } + c.goAbsolutePackagePath = absolutePath *c.goPackageName = getModulePackageName(modFile, path.Clean(outputPath)) + } return nil } @@ -319,7 +318,6 @@ func (c *config) setClientLanguage(clientLanguage, jsModulePath string) error { func (c *config) setGeneratedFilePaths() { const serverDir = "server" - c.goAbsolutePackagePath = filepath.Join(os.Getenv("GOPATH"), "src", c.goPackagePath) if c.generateDynamo { // set path of generated dynamo code if none specified if swag.StringValue(c.relativeDynamoPath) == "" { @@ -333,15 +331,6 @@ func (c *config) setGeneratedFilePaths() { } } -func getModulePackagePath(goPath, outputPath string) string { - pwd, err := os.Getwd() - if err != nil { - log.Fatalf("Error getting current directory: %s", err.Error()) - } - goSrcPath := fmt.Sprintf("%v%v", goPath, "/src/") - return path.Join(strings.TrimPrefix(pwd, goSrcPath), outputPath) -} - // getModulePackageName gets the package name of the generated code // Example: if packagePath = github.com/Clever/wag/v8/gen-go and the module name is github.com/Clever/wag/v8/v2 // the function will return github.com/Clever/wag/v8/v2/gen-go diff --git a/main_test.go b/main_test.go index 6394aebd..902cc199 100644 --- a/main_test.go +++ b/main_test.go @@ -25,7 +25,6 @@ func Test_config_validate(t *testing.T) { outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), jsModulePath: swag.String("jsModulePath"), - goPackagePath: "github.com/Clever/wag/output-path", relativeDynamoPath: swag.String("server/db"), generateDynamo: true, generateServer: true, @@ -48,7 +47,6 @@ func Test_config_validate(t *testing.T) { outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), jsModulePath: swag.String("jsModulePath"), - goPackagePath: "github.com/Clever/wag/output-path", generateServer: false, generateDynamo: false, generateTracing: true, @@ -70,7 +68,6 @@ func Test_config_validate(t *testing.T) { clientLanguage: swag.String("go"), outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), - goPackagePath: "github.com/Clever/wag/output-path", generateServer: false, generateDynamo: false, generateTracing: true, @@ -94,7 +91,6 @@ func Test_config_validate(t *testing.T) { outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), jsModulePath: swag.String("jsModulePath"), - goPackagePath: "github.com/Clever/wag/output-path", generateServer: false, generateDynamo: false, generateTracing: false, @@ -117,7 +113,6 @@ func Test_config_validate(t *testing.T) { goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), jsModulePath: swag.String("jsModulePath"), relativeDynamoPath: swag.String("server/db"), - goPackagePath: "github.com/Clever/wag/output-path", generateServer: true, generateDynamo: true, generateTracing: true, @@ -138,7 +133,6 @@ func Test_config_validate(t *testing.T) { outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), relativeDynamoPath: swag.String("server/db"), - goPackagePath: "github.com/Clever/wag/output-path", generateServer: true, generateTracing: true, generateGoClient: true, @@ -177,7 +171,6 @@ func Test_config_validate(t *testing.T) { output: config{ outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), - goPackagePath: "github.com/Clever/wag/output-path", relativeDynamoPath: swag.String("gen-db/db"), dynamoOnly: swag.Bool(true), generateServer: false, @@ -198,7 +191,6 @@ func Test_config_validate(t *testing.T) { output: config{ outputPath: swag.String("output-path"), goPackageName: swag.String("github.com/Clever/wag/v8/output-path"), - goPackagePath: "github.com/Clever/wag/output-path", relativeDynamoPath: swag.String("db"), dynamoOnly: swag.Bool(true), generateServer: false,