Skip to content

Commit

Permalink
Remove GOPATH entirely in go.mod mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-sutton committed Apr 21, 2022
1 parent 6436a44 commit d16d27b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
25 changes: 7 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type config struct {
goPackageName *string

dynamoPath string
goPackagePath string
goAbsolutePackagePath string
jsClientPath string

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) == "" {
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d16d27b

Please sign in to comment.