diff --git a/main.go b/main.go index eba3314..50a9203 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "go/build" "io" "io/fs" "io/ioutil" @@ -141,6 +142,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() { @@ -151,7 +159,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) } @@ -166,8 +174,8 @@ func main() { pf := plat.GeneratePlatformFeatures() pf.Resolve(globalTypes, globalValues) - for tc, reg := range pf.FilterByCategory() { - printCategory(tc, reg, plat, commandCount) + for tc, reg := range pf.FilterByCategory() { + printCategory(tc, reg, plat, commandCount, goimportsPath) if tc == def.CatCommand { commandCount += len(reg.ResolvedTypes) } @@ -180,7 +188,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) { +func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platform, startingCount int, goimportsPath string) { if tc == def.CatInclude { return } @@ -247,7 +255,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 @@ -348,5 +356,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") }