also attempt to restore from ~/go/pkg/mod/

This commit is contained in:
Jeff Carr 2024-12-15 00:11:08 -06:00
parent 0d6166a5a2
commit 84cf8265a0
1 changed files with 64 additions and 18 deletions

82
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"os" "os"
"path/filepath"
"strings" "strings"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
@ -45,7 +46,7 @@ func main() {
check = findPwdRepo() check = findPwdRepo()
if check == nil { if check == nil {
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)") log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
os.Exit(-1) badExit(nil, nil)
} }
if err := doMain(check); err != nil { if err := doMain(check); err != nil {
@ -54,7 +55,7 @@ func main() {
} }
if configSave { if configSave {
forge.ConfigSave() // forge.ConfigSave()
} }
log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath) log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
@ -93,6 +94,41 @@ func saveAsMetadata(repo *gitpb.Repo) error {
return nil return nil
} }
func restoreFromGoPkg(repo *gitpb.Repo) error {
homedir, err := os.UserHomeDir()
if err != nil {
badExit(nil, err)
}
rver := repo.GetMasterVersion()
if rver == "" {
return errors.New("could not get master version")
}
modfile := filepath.Join(homedir, "go/pkg/mod", repo.GoPath+"@"+rver, "go.mod")
log.Info("mod path should be", modfile)
data, err := os.ReadFile(modfile)
if err != nil {
return err
}
modf, err := os.OpenFile("go.mod", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer modf.Close()
modf.Write(data)
modfile = filepath.Join(homedir, "go/pkg/mod", repo.GoPath+"@"+rver, "go.sum")
log.Info("mod path should be", modfile)
data, err = os.ReadFile(modfile)
if err == nil {
sumf, _ := os.OpenFile("go.sum", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer sumf.Close()
sumf.Write(data)
}
// try go.sum, but no error checking since it might not be there
return nil
}
func doMain(repo *gitpb.Repo) error { func doMain(repo *gitpb.Repo) error {
var perfect bool = true var perfect bool = true
if !repo.IsValid() { if !repo.IsValid() {
@ -104,27 +140,37 @@ func doMain(repo *gitpb.Repo) error {
} }
log.Info(repo.GoPath, "is valid according to forge") log.Info(repo.GoPath, "is valid according to forge")
// skip restore if --force // purge the git meta-data if --force
if argv.Force { if argv.Force {
repo.Run([]string{"git", "notes", "remove"}) repo.Run([]string{"git", "notes", "remove"})
eraseGoMod(repo)
}
// erase the go.mod and go.sum files
cname := repo.GetCurrentBranchName()
// try to restore from the git metadata
if err := repo.AutogenRestore(cname); err != nil {
// ignore errors here
}
if err := repo.ValidGoSum(); err == nil {
log.Info(repo.GoPath, "go.mod and go.sum were restored ok")
configSave = true
return nil
} }
// double check here. use --force to remake them // erase the go.mod and go.sum files
eraseGoMod(repo)
if !argv.Strict {
if err := restoreFromGoPkg(repo); err == nil {
configSave = true
return nil
}
}
// try to restore from the git metadata
cname := repo.GetCurrentBranchName()
if err := repo.AutogenRestore(cname); err == nil {
log.Info(repo.GoPath, "go.mod and go.sum were restored ok")
if !argv.Strict {
configSave = true
return nil
}
}
// if they were auto restored, one should exit here
if err := repo.ValidGoSum(); err == nil { if err := repo.ValidGoSum(); err == nil {
log.Info(repo.GoPath, "go.mod and go.sum are already valid") if !argv.Strict {
return nil log.Info(repo.GoPath, "go.mod and go.sum were restored ok")
return nil
}
} }
if repo.GetMasterBranchName() != repo.GetCurrentBranchName() { if repo.GetMasterBranchName() != repo.GetCurrentBranchName() {