summaryrefslogtreecommitdiff
path: root/cleanGoSum.go
diff options
context:
space:
mode:
Diffstat (limited to 'cleanGoSum.go')
-rw-r--r--cleanGoSum.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/cleanGoSum.go b/cleanGoSum.go
index 20d3df7..bfbf828 100644
--- a/cleanGoSum.go
+++ b/cleanGoSum.go
@@ -3,6 +3,10 @@ package forgepb
import (
"errors"
"fmt"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
@@ -52,6 +56,11 @@ func (f *Forge) CleanGoDepsCheckOk(check *gitpb.Repo) error {
return nil
}
+ // simple trim
+ if err := f.TrimGoSum(check); err != nil {
+ return err
+ }
+
var err error = nil
log.Printf("current repo %s go dependancy count: %d", check.GetGoPath(), check.GoDepsLen())
deps := check.GoDeps.SortByGoPath()
@@ -98,3 +107,75 @@ func (f *Forge) CleanGoDepsCheckOk(check *gitpb.Repo) error {
}
return err
}
+
+func (f *Forge) TrimGoSum(check *gitpb.Repo) error {
+ var stuff map[string]string
+ stuff = make(map[string]string)
+
+ var modver map[string]string
+ modver = make(map[string]string)
+
+ var good map[string]bool
+ good = make(map[string]bool)
+
+ if check == nil {
+ log.Info("boo, check == nil")
+ return errors.New("*repo == nil")
+ }
+ filename := filepath.Join(filepath.Join(check.FullPath, "go.sum"))
+ data, err := os.ReadFile(filename)
+ if err != nil {
+ return err
+ }
+
+ for _, line := range strings.Split(string(data), "\n") {
+ parts := strings.Fields(line)
+ if len(parts) < 3 {
+ log.Info("BAD:", line)
+ continue
+ }
+
+ gopath := parts[0]
+ version := parts[1]
+ hash := parts[2]
+
+ if strings.HasSuffix(version, "/go.mod") {
+ if _, ok := stuff[gopath]; ok {
+ log.Info("MATCHED: gopath:", gopath, "version:", version)
+ modver[gopath] = version + " " + hash
+ good[gopath] = true
+ } else {
+ log.Info("GARBAGE: gopath:", gopath, "version:", version)
+ }
+ } else {
+ log.Info("GOOD : gopath:", gopath, "version:", version)
+ stuff[gopath] = version + " " + hash
+ }
+ }
+
+ keys := make([]string, 0, len(stuff))
+ for k := range stuff {
+ keys = append(keys, k)
+ }
+
+ // rewrite the go.sum file
+ newfilename := filepath.Join(filepath.Join(check.FullPath, "go.sum"))
+ newf, err := os.OpenFile(newfilename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
+ if err != nil {
+ return err
+ }
+ defer newf.Close()
+ sort.Strings(keys)
+ for _, gopath := range keys {
+ if good[gopath] {
+ fmt.Fprintf(newf, "%s %s\n", gopath, stuff[gopath])
+ fmt.Fprintf(newf, "%s %s\n", gopath, modver[gopath])
+ check := f.Repos.FindByGoPath(gopath)
+ if check == nil {
+ log.Info("gopath does not really exist:", gopath)
+ }
+ }
+ }
+ // fmt.Fprintln(newf, "test")
+ return nil
+}