From c5f7570834f092608cd2137e966399f44aa02d36 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 13 Dec 2024 00:19:33 -0600 Subject: [PATCH] working on go-mod-clean Signed-off-by: Jeff Carr --- config.go | 6 +-- goDep.redoGoMod.go | 132 +++++++++++++-------------------------------- isPrimitive.go | 9 +++- repo.proto | 2 + shell.go | 25 ++++++--- 5 files changed, 69 insertions(+), 105 deletions(-) diff --git a/config.go b/config.go index 38b6518..4d86b81 100644 --- a/config.go +++ b/config.go @@ -56,9 +56,9 @@ func (all *Repos) ConfigLoad() error { return errors.New("gitpb.ConfigLoad() repos.pb is empty") } if all.Repos == nil { - log.Warn("gitpb.ConfigLoad() all.Repos == nil") + // log.Warn("gitpb.ConfigLoad() all.Repos == nil") } else { - log.Warn("gitpb.ConfigLoad() all.Repos.Len()", all.Len()) + log.Warn("gitpb.ConfigLoad() error. should be zero. all.Repos.Len() =", all.Len()) } if err = all.Unmarshal(data); err != nil { log.Warn("gitpb.ConfigLoad() failed", err) @@ -71,7 +71,7 @@ func (all *Repos) ConfigLoad() error { } return err } - log.Info("gitpb.Init()", len(all.Repos), "repos in ~/.config/forge/repos.pb") + log.Info("gitpb.Init() ", len(all.Repos), "repos in ~/.config/forge/repos.pb") return nil } return nil diff --git a/goDep.redoGoMod.go b/goDep.redoGoMod.go index 8406fd4..82d5830 100644 --- a/goDep.redoGoMod.go +++ b/goDep.redoGoMod.go @@ -4,113 +4,57 @@ package gitpb import ( "errors" - "os" - - "go.wit.com/log" + "time" ) -// remove every go.mod and go.sum -// testing to see where this stuff is coming from -func (repo *Repo) EraseGoMod() { - // unset the go development ENV var to generate release files - if ok, err := repo.strictRun([]string{"rm", "-f", "go.mod", "go.sum"}); !ok { - log.Warn("rm go.mod go.sum failed", err) +// checks to see if the go.sum and go.mod files +// match the repo.pb information +func (repo *Repo) ValidGoSum() error { + if repo.GoPrimitive { + // repo thinks it is primitive but has a go.sum file + if repo.Exists("go.sum") { + return errors.New("GoPrimitive == true, but go.sum exists") + } + mtime, err := repo.mtime("go.mod") + if err == nil { + return err + } + if mtime != repo.LastGoDep.AsTime() { + return errors.New("go.mod mtime mis-match") + } } + mtime, err := repo.mtime("go.sum") + if err == nil { + return err + } + if mtime != repo.LastGoDep.AsTime() { + return errors.New("go.sum mtime mis-match") + } + return nil } -// poor name perhaps. It's because in most of these -// repos you can also type "make redomod" to do the same thing -// since it's a Makefile task that is also useful to be able to run -// from the command line -func (repo *Repo) RedoGoMod() (bool, error) { - // unset the go development ENV var to generate release files - os.Unsetenv("GO111MODULE") - if ok, err := repo.strictRun([]string{"rm", "-f", "go.mod", "go.sum"}); !ok { - log.Warn("rm go.mod go.sum failed", err) - return ok, err - } - if ok, err := repo.strictRun([]string{"go", "mod", "init", repo.GoPath}); !ok { - log.Warn("go mod init failed", err) - return ok, err - } - if ok, err := repo.strictRun([]string{"go", "mod", "tidy"}); !ok { - log.Warn("go mod tidy failed", err) - return ok, err - } - // most things should build with golang after 1.20 - if ok, err := repo.strictRun([]string{"go", "mod", "edit", "-go=1.20"}); !ok { - log.Warn("go mod edit failed", err) - return ok, err - } - // log.Info("MakeRedomod() worked", repo.GoPath) - - if repo.Exists("go.sum") { - // return the attempt to parse go.mod & go.sum - return repo.ParseGoSum() - } - repo.GoDeps = new(GoDeps) - repo.GoPrimitive = false - - ok, err := repo.IsPrimitive() - if err != nil { - // this means this repo does not depend on any other package - log.Info("PRIMATIVE repo error:", repo.GoPath, "err =", err) - return false, err - } - if ok { - // this means the repo is primitive so there is no go.sum - repo.GoPrimitive = true - return true, nil - } - // this should never happen - return false, errors.New("MakeRedomod() logic failed") -} - -// returns true if the last published func (repo *Repo) GoDepsLen() int { if repo.GoDeps == nil { return 0 } - return repo.GoDeps.Len() + return len(repo.GoDeps.GoDeps) } -// returns true if the last published -func (repo *Repo) PublishedLen() int { - if repo.Published == nil { - return 0 - } - return repo.Published.Len() +func (repo *Repo) LastGitPull() (time.Time, error) { + return repo.mtime(".git/FETCH_HEAD") } -// returns true if the last published -func (all *Repos) GoDepsChangedOld(repo *Repo) (bool, error) { - var upgrade bool = false - if repo.GoDeps == nil { - repo.RedoGoMod() +func (repo *Repo) GoSumAge() (time.Duration, error) { + var mtime time.Time + var err error + mtime, err = repo.mtime("go.sum") + if err == nil { + return time.Since(mtime), nil } - if repo.GoDeps.Len() == 0 { - repo.RedoGoMod() + mtime, err = repo.mtime("go.mod") + if err == nil { + return time.Since(mtime), nil } - log.Printf("current repo %s go dependancy count: %d", repo.GetGoPath(), repo.GoDeps.Len()) - deps := repo.GoDeps.SortByGoPath() - for deps.Scan() { - depRepo := deps.Next() - if repo.Published == nil { - return false, errors.New("repo published deps info is nil") - } - found := repo.Published.FindByGoPath(depRepo.GetGoPath()) - if found == nil { - log.Printf("dep %-50s %-10s vs %-10s", depRepo.GetGoPath(), depRepo.GetVersion(), "NEW") - upgrade = true - continue - // return upgrade, errors.New("new repo added " + depRepo.GetGoPath()) - } - if depRepo.GetVersion() == found.GetVersion() { - // log.Printf("deps %-50s %-10s vs %-10s", depRepo.GetGoPath(), depRepo.GetVersion(), found.GetVersion()) - } else { - log.Printf("dep %-50s %-10s vs %-10s BROKEN", depRepo.GetGoPath(), depRepo.GetVersion(), found.GetVersion()) - upgrade = true - } - } - return upgrade, nil + now := time.Now() + return time.Since(now), errors.New(repo.GoPath + " go.mod missing") } diff --git a/isPrimitive.go b/isPrimitive.go index 3870343..0455799 100644 --- a/isPrimitive.go +++ b/isPrimitive.go @@ -34,7 +34,7 @@ func (repo *Repo) IsPrimitive() (bool, error) { for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) - parts := strings.Split(line, " ") + parts := strings.Fields(line) log.Log(GITPB, " gomod:", parts) if len(parts) >= 1 { log.Log(GITPB, " gomod: part[0] =", parts[0]) @@ -42,7 +42,12 @@ func (repo *Repo) IsPrimitive() (bool, error) { log.Log(GITPB, " should return false here") return false, errors.New("go.mod file is not primative") } - + if parts[0] == "go" { + if parts[1] != "1.20" { + log.Log(GITPBWARN, "go not set to 1.20 for", repo.GoPath) + return false, errors.New("go not set to 1.20 for " + repo.GoPath) + } + } } } repo.GoPrimitive = true diff --git a/repo.proto b/repo.proto index 288a629..62f287f 100644 --- a/repo.proto +++ b/repo.proto @@ -32,6 +32,8 @@ message Repo { // `autogenpb:marshal` string URL = 18; // the URL. amazingly I didn't add this earlier. duh. bool goProtobuf = 19; // autogen go files from .proto string desc = 20; // what is this repo? + bytes goMod = 21; // the last go.mod file + bytes goSum = 22; // the last go.sum file } message Repos { // `autogenpb:marshal` diff --git a/shell.go b/shell.go index 4098303..3a5fc0d 100644 --- a/shell.go +++ b/shell.go @@ -1,9 +1,12 @@ package gitpb import ( + "errors" + "fmt" "os" "path/filepath" "strings" + "time" "github.com/go-cmd/cmd" "go.wit.com/lib/gui/shell" @@ -46,17 +49,17 @@ func (repo *Repo) RunRealtime(cmd []string) cmd.Status { } // for now, even check cmd.Exit -func (repo *Repo) strictRun(cmd []string) (bool, error) { +func (repo *Repo) StrictRun(cmd []string) error { result := repo.RunQuiet(cmd) if result.Error != nil { - log.Warn("go mod init failed err:", result.Error) - return false, result.Error + log.Warn(repo.GoPath, cmd, "wow. golang is cool. an os.Error:", result.Error) + return result.Error } if result.Exit != 0 { - log.Warn("go mod init exit =", result.Exit) - return false, result.Error + log.Warn(cmd, "failed with", result.Exit) + return errors.New(fmt.Sprint(cmd, "failed with", result.Exit)) } - return true, nil + return nil } func (repo *Repo) Exists(filename string) bool { @@ -85,3 +88,13 @@ func (repo *Repo) IsDirectory() bool { } return info.IsDir() } + +func (repo *Repo) mtime(filename string) (time.Time, error) { + pathf := filepath.Join(repo.FullPath, filename) + statf, err := os.Stat(pathf) + if err == nil { + return statf.ModTime(), nil + } + log.Log(GITPBWARN, "mtime() error", pathf, err) + return time.Now(), err +}