From 10e1f545bbb9df6590624252e0a1aabaeaa00f6c Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 11 Dec 2024 13:55:21 -0600 Subject: [PATCH] attempt simple go.sum trim --- build.go | 20 +++++++++---- cleanGoSum.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ config.go | 2 +- forgepb.go | 3 ++ goWork.go | 2 +- 5 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 forgepb.go diff --git a/build.go b/build.go index cf1f518..14c9f6f 100644 --- a/build.go +++ b/build.go @@ -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 { 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 +} diff --git a/config.go b/config.go index fcd3b4d..599db3e 100644 --- a/config.go +++ b/config.go @@ -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) diff --git a/forgepb.go b/forgepb.go new file mode 100644 index 0000000..1a4c619 --- /dev/null +++ b/forgepb.go @@ -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` diff --git a/goWork.go b/goWork.go index c709781..afd56c9 100644 --- a/goWork.go +++ b/goWork.go @@ -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 }