2024-12-10 14:07:14 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-13 02:21:25 -06:00
|
|
|
"errors"
|
2024-12-11 00:03:08 -06:00
|
|
|
"fmt"
|
2024-12-10 14:07:14 -06:00
|
|
|
"os"
|
2024-12-13 02:21:25 -06:00
|
|
|
"path/filepath"
|
2024-12-11 00:03:08 -06:00
|
|
|
"strings"
|
2024-12-10 14:07:14 -06:00
|
|
|
|
|
|
|
"go.wit.com/dev/alexflint/arg"
|
|
|
|
"go.wit.com/lib/protobuf/forgepb"
|
|
|
|
"go.wit.com/lib/protobuf/gitpb"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
|
|
|
|
|
|
|
var pp *arg.Parser
|
|
|
|
var forge *forgepb.Forge
|
|
|
|
var check *gitpb.Repo
|
|
|
|
|
|
|
|
func main() {
|
2024-12-12 18:59:16 -06:00
|
|
|
log.Info("go-mod-clean version", VERSION, "built on", BUILDTIME)
|
2024-12-10 14:07:14 -06:00
|
|
|
pp = arg.MustParse(&argv)
|
|
|
|
|
|
|
|
// load the ~/.config/forge/ config
|
|
|
|
// this lets you configure repos you have read/write access too
|
|
|
|
forge = forgepb.Init()
|
|
|
|
|
2024-12-11 00:03:08 -06:00
|
|
|
// figure out what directory we are running in
|
|
|
|
check = findPwdRepo()
|
2024-12-10 14:07:14 -06:00
|
|
|
if check == nil {
|
2024-12-11 00:03:08 -06:00
|
|
|
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
|
|
|
|
// skip restore if --force
|
|
|
|
if !argv.Force {
|
|
|
|
// try to restore from the git metadata
|
|
|
|
if restoreFromGit(check) {
|
|
|
|
okExit("go.mod was restored from the git notes")
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if check.GetMasterBranchName() != check.GetCurrentBranchName() {
|
|
|
|
log.Info("")
|
|
|
|
log.Info("You can only run go-mod-clean on a git master branch.")
|
|
|
|
log.Info("Publishing go.mod & go.sum files must come from from git version tag")
|
|
|
|
log.Info("Anything else doesn't make sense.")
|
|
|
|
log.Info("")
|
|
|
|
badExit(errors.New("not git master branch"))
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-create go.sum and go.mod
|
2024-12-13 02:21:25 -06:00
|
|
|
if _, err := redoGoMod(check); err != nil {
|
2024-12-11 00:03:08 -06:00
|
|
|
badExit(err)
|
|
|
|
}
|
|
|
|
|
2024-12-13 02:21:25 -06:00
|
|
|
if argv.Trim {
|
|
|
|
// try to trim junk
|
|
|
|
if err := trimGoSum(check); err != nil {
|
|
|
|
badExit(err)
|
|
|
|
}
|
2024-12-11 13:51:06 -06:00
|
|
|
}
|
|
|
|
|
2024-12-11 00:03:08 -06:00
|
|
|
// check go.sum file
|
2024-12-13 02:21:25 -06:00
|
|
|
if err := cleanGoDepsCheckOk(check); err != nil {
|
2024-12-11 01:14:24 -06:00
|
|
|
log.Info("forge.FinalGoDepsCheck() failed. boo. :", check.GoPath)
|
|
|
|
badExit(err)
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
2024-12-11 13:51:06 -06:00
|
|
|
|
2024-12-13 02:21:25 -06:00
|
|
|
// put the files in the notes section in git
|
|
|
|
// this way, git commits are not messed up
|
|
|
|
// with this autogenerated code
|
|
|
|
if err := saveAsMetadata(check); err != nil {
|
|
|
|
log.Info("save go.mod as git metadata failed", check.GoPath, err)
|
|
|
|
badExit(err)
|
|
|
|
}
|
|
|
|
|
2024-12-11 13:51:06 -06:00
|
|
|
log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
|
|
|
|
okExit(check.GoPath + " go.sum seems clean")
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func findPwdRepo() *gitpb.Repo {
|
|
|
|
var check *gitpb.Repo
|
|
|
|
// attempt to use the working directory
|
|
|
|
// this is probably what happens most of the time
|
|
|
|
pwd, _ := os.Getwd()
|
|
|
|
if strings.HasPrefix(pwd, forge.GetGoSrc()) {
|
|
|
|
gopath := strings.TrimPrefix(pwd, forge.GetGoSrc())
|
|
|
|
gopath = strings.Trim(gopath, "/")
|
|
|
|
log.Info("findRepo() trying gopath", gopath)
|
|
|
|
check = forge.Repos.FindByGoPath(gopath)
|
|
|
|
if check != nil {
|
|
|
|
log.Info("findRepo() worked", check.GoPath)
|
|
|
|
return check
|
|
|
|
}
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
2024-12-11 00:03:08 -06:00
|
|
|
return nil
|
2024-12-10 14:07:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func okExit(thing string) {
|
2024-12-13 02:21:25 -06:00
|
|
|
log.DaemonMode(true)
|
2024-12-10 14:07:14 -06:00
|
|
|
log.Info(thing, "ok")
|
2024-12-13 02:21:25 -06:00
|
|
|
// log.Info("Finished go-mod-clean on", check.GetGoPath(), "ok")
|
2024-12-10 14:07:14 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func badExit(err error) {
|
2024-12-13 02:21:25 -06:00
|
|
|
log.DaemonMode(true)
|
2024-12-12 18:59:16 -06:00
|
|
|
log.Info("go-mod-clean failed: ", err, forge.GetGoSrc())
|
2024-12-10 14:07:14 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
|
|
|
|
// todo: do this the right way in git
|
|
|
|
func saveAsMetadata(repo *gitpb.Repo) error {
|
|
|
|
cname := check.GetCurrentBranchName()
|
|
|
|
cmd := []string{"git", "notes", "remove", cname}
|
|
|
|
if err := check.StrictRun(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if check.GoPrimitive {
|
|
|
|
cmd = []string{"git", "notes", "add", "-F", "go.mod", cname}
|
|
|
|
if err := check.StrictRun(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd = []string{"git", "notes", "add", "-F", "go.mod", cname}
|
|
|
|
if err := check.StrictRun(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = []string{"git", "notes", "append", "-m", "GOSUM:", cname}
|
|
|
|
if err := check.StrictRun(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = []string{"git", "notes", "append", "-F", "go.sum", cname}
|
|
|
|
if err := check.StrictRun(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreFromGit(repo *gitpb.Repo) bool {
|
|
|
|
result := repo.Run([]string{"git", "notes", "show"})
|
|
|
|
if result.Exit != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if result.Error != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(result.Stdout) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
all := strings.Join(result.Stdout, "\n")
|
|
|
|
parts := strings.Split(all, "GOSUM:")
|
|
|
|
|
|
|
|
gomod := filepath.Join(filepath.Join(check.FullPath, "go.mod"))
|
|
|
|
newf, _ := os.OpenFile(gomod, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
fmt.Fprint(newf, strings.TrimSpace(parts[0]))
|
|
|
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
gosum := filepath.Join(filepath.Join(check.FullPath, "go.sum"))
|
|
|
|
newf, _ := os.OpenFile(gosum, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
fmt.Fprint(newf, strings.TrimSpace(parts[1]))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|