2024-12-10 14:07:14 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-13 02:21:25 -06:00
|
|
|
"errors"
|
2024-12-10 14:07:14 -06:00
|
|
|
"os"
|
2024-12-15 00:11:08 -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
|
2024-12-13 16:16:18 -06:00
|
|
|
|
|
|
|
// var check *gitpb.Repo
|
|
|
|
var configSave bool
|
2024-12-10 14:07:14 -06:00
|
|
|
|
|
|
|
func main() {
|
2024-12-13 16:16:18 -06:00
|
|
|
var check *gitpb.Repo
|
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-13 16:16:18 -06:00
|
|
|
if argv.All {
|
|
|
|
// run this on every single repo
|
|
|
|
// do this before publishing new golang versions
|
2024-12-17 07:03:07 -06:00
|
|
|
all := forge.Repos.SortByFullPath()
|
2024-12-13 16:16:18 -06:00
|
|
|
for all.Scan() {
|
|
|
|
check = all.Next()
|
2024-12-15 22:39:54 -06:00
|
|
|
if err := doMain(check); err != nil {
|
|
|
|
badExit(check, err)
|
2024-12-13 16:16:18 -06:00
|
|
|
}
|
2024-12-13 12:34:47 -06:00
|
|
|
}
|
2024-12-13 16:16:18 -06:00
|
|
|
} else {
|
|
|
|
// figure out what directory we are running in
|
2024-12-13 18:59:40 -06:00
|
|
|
check = findPwdRepo()
|
2024-12-13 16:16:18 -06:00
|
|
|
if check == nil {
|
|
|
|
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
|
2024-12-15 00:11:08 -06:00
|
|
|
badExit(nil, nil)
|
2024-12-11 00:03:08 -06:00
|
|
|
}
|
2024-12-10 14:07:14 -06:00
|
|
|
|
2024-12-15 22:39:54 -06:00
|
|
|
if err := doMain(check); err != nil {
|
|
|
|
badExit(check, err)
|
|
|
|
}
|
2024-12-15 22:34:20 -06:00
|
|
|
if argv.Force {
|
|
|
|
if err := doForce(check); err != nil {
|
|
|
|
badExit(check, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := doSmart(check); err != nil {
|
|
|
|
badExit(check, err)
|
|
|
|
}
|
2024-12-13 02:21:25 -06:00
|
|
|
}
|
2024-12-11 13:51:06 -06:00
|
|
|
}
|
|
|
|
|
2024-12-13 16:16:18 -06:00
|
|
|
if configSave {
|
2024-12-15 22:34:20 -06:00
|
|
|
forge.ConfigSave()
|
2024-12-13 02:21:25 -06:00
|
|
|
}
|
|
|
|
|
2024-12-17 07:03:07 -06:00
|
|
|
log.Info("forge.FinalGoDepsCheck() worked :", check.GetGoPath())
|
2024-12-13 16:16:18 -06:00
|
|
|
okExit(check, "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)
|
2024-12-17 13:12:52 -06:00
|
|
|
check = forge.Repos.FindByFullPath(pwd)
|
2024-12-11 00:03:08 -06:00
|
|
|
if check != nil {
|
2024-12-17 07:03:07 -06:00
|
|
|
log.Info("findRepo() worked", check.GetGoPath())
|
2024-12-11 00:03:08 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-12-13 02:21:25 -06:00
|
|
|
func saveAsMetadata(repo *gitpb.Repo) error {
|
2024-12-13 16:16:18 -06:00
|
|
|
cname := repo.GetCurrentBranchName()
|
2024-12-17 20:47:09 -06:00
|
|
|
if repo.GetGoPrimitive() {
|
2024-12-13 16:16:18 -06:00
|
|
|
if err := repo.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
|
2024-12-13 02:21:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-12-13 16:16:18 -06:00
|
|
|
if err := repo.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); err != nil {
|
2024-12-13 02:21:25 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-12-13 16:16:18 -06:00
|
|
|
|
2024-12-15 00:11:08 -06:00
|
|
|
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")
|
|
|
|
}
|
2024-12-17 07:03:07 -06:00
|
|
|
modfile := filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver, "go.mod")
|
2024-12-15 00:11:08 -06:00
|
|
|
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)
|
|
|
|
|
2024-12-17 07:03:07 -06:00
|
|
|
modfile = filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver, "go.sum")
|
2024-12-15 00:11:08 -06:00
|
|
|
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
|
|
|
|
}
|
2024-12-15 22:39:54 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|