go-mod-clean/main.go

154 lines
3.5 KiB
Go

package main
import (
"errors"
"os"
"path/filepath"
"strings"
"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
var configSave bool
func main() {
var check *gitpb.Repo
log.Info("go-mod-clean version", VERSION, "built on", BUILDTIME)
pp = arg.MustParse(&argv)
// load the ~/.config/forge/ config
// this lets you configure repos you have read/write access too
forge = forgepb.Init()
if argv.All {
// run this on every single repo
// do this before publishing new golang versions
all := forge.Repos.SortByFullPath()
for all.Scan() {
check = all.Next()
if err := doMain(check); err != nil {
badExit(check, err)
}
}
} else {
// figure out what directory we are running in
check = findPwdRepo()
if check == nil {
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
badExit(nil, nil)
}
if err := doMain(check); err != nil {
badExit(check, err)
}
if argv.Force {
if err := doForce(check); err != nil {
badExit(check, err)
}
} else {
if err := doSmart(check); err != nil {
badExit(check, err)
}
}
}
if configSave {
forge.ConfigSave()
}
log.Info("forge.FinalGoDepsCheck() worked :", check.GetGoPath())
okExit(check, "go.sum seems clean")
}
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.FindByFullPath(pwd)
if check != nil {
log.Info("findRepo() worked", check.GetGoPath())
return check
}
}
return nil
}
func saveAsMetadata(repo *gitpb.Repo) error {
cname := repo.GetCurrentBranchName()
if repo.GoInfo.GoPrimitive {
if err := repo.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
return err
}
} else {
if err := repo.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); err != nil {
return err
}
}
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.GetGoPath()+"@"+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.GetGoPath()+"@"+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 {
if argv.Strict {
return doStrict(repo)
}
if argv.Force {
err := doForce(repo)
return err
}
// if --force or --strict is not set, fall back to a "smart" guess
// at what the user probably would want
return doSmart(repo)
}