diff --git a/build.go b/build.go index 0b8b58a..8fee1be 100644 --- a/build.go +++ b/build.go @@ -32,6 +32,10 @@ func (f *Forge) Install(repo *gitpb.Repo, userFlags []string) error { } func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) error { + if repo == nil { + log.Warn("forge.doBuild repo == nil") + return errors.New("forge.doBuild repo == nil") + } // always assume all sources have been downloaded // todo: detect when in ~/go/src vs go.work mode os.Setenv("GO111MODULE", "off") @@ -101,6 +105,14 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err log.Info(strings.Join(result.Stderr, "\n")) log.DaemonMode(false) log.Warn("go build failed", cmd) + pwd, _ := os.Getwd() + log.Warn("go build pwd", pwd) + res2 := repo.RunRealtime(cmd) + if res2.Exit == 0 { + log.Info("again failed", res2.Exit) + log.Info("again failed cmd", strings.Join(cmd, "a")) + log.Info("again failed", strings.Join(res2.Stdout, "\n")) + } return errors.New("go build failed: " + fmt.Sprint(result.Error)) } } diff --git a/goSrcFind.go b/goSrcFind.go index 7393a35..b19e858 100644 --- a/goSrcFind.go +++ b/goSrcFind.go @@ -17,20 +17,26 @@ import ( // otherwise use ~/go/src func (f *Forge) findGoSrc() (string, error) { pwd, err := os.Getwd() + startpwd, _ := os.Getwd() if err == nil { + log.Info("forge.findGoSrc() trying digup", pwd, err) // Check for go.work in the current directory and then move up until root if pwd, err := digup(pwd); err == nil { - log.Info("using go.work file in directory", pwd) + log.Info("forge.findGoSrc() using go.work file in directory", pwd) f.goWork = true // found an existing go.work file // os.Chdir(pwd) return pwd, nil + } else { + log.Info("forge.digup() err", pwd, err) } + } else { + log.Info("forge.findGoSrc() os.Getwd()", pwd, err) } // there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22 pwd, err = useGoSrc() - log.Info("using ~/go/src directory", pwd) + log.Info("forge.findGoSrc() 2 using ~/go/src directory", pwd, "start was", startpwd) return pwd, err } @@ -50,9 +56,12 @@ func useGoSrc() (string, error) { func digup(path string) (string, error) { for { workFilePath := filepath.Join(path, "go.work") + log.Info("digup trying", workFilePath) if _, err := os.Stat(workFilePath); err == nil { + log.Info("digup found", path) return path, nil // Found the go.work file } else if !os.IsNotExist(err) { + log.Info("forgepb.digup() failed", workFilePath, err) return "", err // An error other than not existing } diff --git a/goWork.go b/goWork.go new file mode 100644 index 0000000..c709781 --- /dev/null +++ b/goWork.go @@ -0,0 +1,53 @@ +package forgepb + +import ( + "errors" + "fmt" + "os" + "path/filepath" +) + +// very much a hack job +func (f *Forge) MakeGoWork() error { + if f.IsGoWork() { + // a go.work file was found + } else { + // you can use a go.work file in ~/go/src , but you probably shouldn't unless something + // has gone terribly wrong + return errors.New("if you want a go.work file in ~/go/src/, touch it first") + } + filename := filepath.Join(f.GetGoSrc(), "go.work") + workf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + defer workf.Close() + + fmt.Fprintln(workf, "go 1.20") // fix this + fmt.Fprintln(workf, "") + fmt.Fprintln(workf, "use (") + + loop := f.Repos.SortByGoPath() + for loop.Scan() { + repo := loop.Next() + /* + if !repo.IsGoLang() == "" { + // skip repos that aren't go + // todo: handle non-flat repos? + log.Info("skip non-go", repo.GoPath) + continue + } + */ + fmt.Fprintln(workf, "\t"+repo.GoPath) + /* + if repo.pb.Exists("go.mod") { + // log.Info("ADDING REPO", goSrcDir, repo.GoPath) + } else { + fmt.Fprintln(workf, "\t"+repo.GoPath) + log.Log(REPO, "missing go.mod for", repo.GoPath) + } + */ + } + fmt.Fprintln(workf, ")") + return nil +} diff --git a/init.go b/init.go index 93d0cbb..618bf60 100644 --- a/init.go +++ b/init.go @@ -12,10 +12,13 @@ import ( func Init() *Forge { f := new(Forge) + getwd, _ := os.Getwd() + log.Info("forgepbb.Init() os.Getwd()", getwd) + log.Info("forgepbb.Init() started with FORGE_CONFIG", os.Getenv("FORGE_CONFIG")) + log.Info("forgepbb.Init() started with FORGE_GOSRC", os.Getenv("FORGE_GOSRC")) // TODO: rethink this but it works for now gosrc := os.Getenv("FORGE_GOSRC") if gosrc == "" { - // already set. ignore init() goSrcDir, err := f.findGoSrc() if err != nil { log.Warn("forge init() findGoSrc()", err) @@ -23,6 +26,7 @@ func Init() *Forge { os.Setenv("FORGE_GOSRC", goSrcDir) } f.goSrc = os.Getenv("FORGE_GOSRC") + log.Info("forge.Init() using ~/go/src directory", f.goSrc) // also rethink this, but maybe this is the right thing to do if os.Getenv("FORGE_CONFIG") == "" { @@ -30,6 +34,9 @@ func Init() *Forge { fullpath := filepath.Join(homeDir, ".config/forge") os.Setenv("FORGE_CONFIG", fullpath) } + log.Info("forgepbb.Init() ~/go/src ", f.goSrc) + log.Info("forgepbb.Init() 2 FORGE_CONFIG", os.Getenv("FORGE_CONFIG")) + log.Info("forgepbb.Init() 2 FORGE_GOSRC", os.Getenv("FORGE_GOSRC")) // cache.go has Do() // f.initOnce.Do(f.initWork) diff --git a/repoClone.go b/repoClone.go index f49319b..7a51be1 100644 --- a/repoClone.go +++ b/repoClone.go @@ -139,7 +139,7 @@ func cloneActual(newdir, basedir, giturl string) (string, error) { } cmd := []string{"git", "clone", "--verbose", "--progress", giturl, newdir} - log.Info("Running:", cmd) + log.Info("Running:", basedir, cmd) r := shell.PathRunRealtime(basedir, cmd) if r.Error != nil { log.Warn("git clone error", r.Error) diff --git a/structs.go b/structs.go index 0bb770d..7ec1abb 100644 --- a/structs.go +++ b/structs.go @@ -23,3 +23,7 @@ type Forge struct { func (f *Forge) GetGoSrc() string { return f.goSrc } + +func (f *Forge) IsGoWork() bool { + return f.goWork +}