attempt simple go.sum trim

This commit is contained in:
Jeff Carr 2024-12-11 13:55:21 -06:00
parent 7512463a57
commit 10e1f545bb
5 changed files with 100 additions and 8 deletions

View File

@ -1,14 +1,12 @@
package forgepb
// for golang repos, this is an attempt to build the package
// there might be some 'standard' ways to implement a build
// that make sense.
//
// Additions to 'go build' that are attempted here:
//
// * detect packages that are plugins
// * autogen packages that have .proto protobuf files
// * define some 'standard' ldflags
// * run autogenpb packages that have .proto protobuf files
// * use some 'standard' ldflags (VERISON, BUILDTIME)
//
import (
@ -66,6 +64,8 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err
repo.RedoGoMod()
f.Repos.ConfigSave()
}
// run autogenpb in all protobuf repos
loop1 := repo.GoDeps.SortByGoPath()
for loop1.Scan() {
t := loop1.Next()
@ -80,7 +80,14 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err
if repo.CheckDirty() {
version = version + "-dirty"
}
cmd := []string{"go", goWhat, "-v"}
cmd := []string{"go", goWhat}
// if this is a plugin, use buildmode=plugin
if repo.RepoType() == "plugin" {
_, fname := filepath.Split(repo.FullPath)
cmd = append(cmd, "-buildmode=plugin", "-o", fname+".so")
}
cmd = append(cmd, "-v")
// set standard ldflag options
now := time.Now()
@ -105,6 +112,7 @@ func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) err
} else {
log.Info("GO111MODULE=", testenv, "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
}
log.Info("running:", repo.FullPath)
log.Info("running:", cmd)
result := repo.RunRealtime(cmd)
if result.Exit == 0 {

View File

@ -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
}

View File

@ -150,7 +150,7 @@ func loadFile(filename string) ([]byte, error) {
func configWrite(filename string, data []byte) error {
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename)
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer cfgfile.Close()
if err != nil {
log.Warn("open config file :", err)

3
forgepb.go Normal file
View File

@ -0,0 +1,3 @@
// Package forgepb describes the protobuf's used by 'go.wit.com/apps/forge'
package forgepb // import "go.wit.com/lib/protobuf/forgepb"
// `go-clean go=1.18`

View File

@ -17,7 +17,7 @@ func (f *Forge) MakeGoWork() error {
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)
workf, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}