From 2982f8fc6bfb2dda43d3b80c9c7f009d8dec5e04 Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Tue, 7 Mar 2023 11:38:27 -0500 Subject: [PATCH] Look harder for goimports goimports may be in GOPATH's bin directory which may not be on the user's PATH. Add GOPATH (falling back to go's default if not set explicitly) to PATH when looking for goimports. --- main.go | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 41a63d9..afbb0ac 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "go/build" "io" "io/fs" "io/ioutil" @@ -143,6 +144,13 @@ func main() { vk1_0.Resolve(globalTypes, globalValues) + goimportsPath, err := findGoimports() + if err != nil { + logrus. + WithField("error", err.Error()). + Error("Could not find goimports") + } + commandCount := 0 for tc, reg := range vk1_0.FilterByCategory() { //ResolvedTypes().FilterByCategory() { @@ -153,7 +161,7 @@ func main() { reg.ResolvedTypes["VK_DEFINE_HANDLE"].PushValue(globalValues["VK_NULL_HANDLE"]) } - printCategory(tc, reg, nil, 0) + printCategory(tc, reg, nil, 0, goimportsPath) if tc == def.CatCommand { commandCount += len(reg.ResolvedTypes) } @@ -173,7 +181,7 @@ func main() { pf.Resolve(globalTypes, globalValues) for tc, reg := range pf.FilterByCategory() { //ResolvedTypes().FilterByCategory() { - printCategory(tc, reg, plat, commandCount) + printCategory(tc, reg, plat, commandCount, goimportsPath) if tc == def.CatCommand { commandCount += len(reg.ResolvedTypes) } @@ -241,7 +249,7 @@ func main() { const fileHeader string = "// Code generated by go-vk from %s at %s. DO NOT EDIT.\n\npackage vk\n\n" // fix doc/issue-1 -func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platform, startingCount int) { //} pt *def.PlatformType, cat def.TypeCategory, tr def.TypeRegistry) { +func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platform, startingCount int, goimportsPath string) { //} pt *def.PlatformType, cat def.TypeCategory, tr def.TypeRegistry) { if tc == def.CatInclude { return } @@ -317,7 +325,7 @@ func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platfor logrus.WithField("file", filename+".go").Info("Running goimports") - cmd := exec.Command("goimports", "-w", outpath) + cmd := exec.Command(goimportsPath, "-w", outpath) e := &strings.Builder{} cmd.Stderr = e @@ -420,5 +428,27 @@ func copyStaticFiles() { if err != nil { panic(err) } +} + +func findGoimports() (path string, err error) { + // goimports is probably in GOPATH which may not be in the user's PATH + goPath := os.Getenv("GOPATH") + if goPath == "" { + // If GOPATH is not set, use go's default + goPath = build.Default.GOPATH + } + + // There may be multiple paths, so split and add "/bin" to each + paths := strings.Split(goPath, string(os.PathListSeparator)) + goPath = "" + for _, path := range paths { + goPath += fmt.Sprintf("%s%sbin%s", path, string(os.PathSeparator), string(os.PathListSeparator)) + } + + // Add PATH paths to the end + goPath += os.Getenv("PATH") + + os.Setenv("PATH", goPath) + return exec.LookPath("goimports") }