summaryrefslogtreecommitdiff
path: root/main.go
blob: c3a1d0d823118c6ddbce1b4681612a868b3eb950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main

import (
	"fmt"
	"os"
	"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

func main() {
	log.Info("go-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()
	// rescan just in case (?) todo: decide what forge should default too
	forge.ScanGoSrc()

	// 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)")
		os.Exit(-1)
	}
	log.Info("starting go-clean for", check.GoPath)
	log.Info("go src dir is set to", forge.GetGoSrc())

	if argv.Recursive {
		if forge.IsGoWork() {
			var warning []string
			warning = append(warning, "go-clean --recursive may not work unless you are in ~/go/src")
			warning = append(warning, "you can continue anyway, but it hasn't been tested as much.")
			simpleStdin(true, warning)
		}
		var warning []string
		warning = append(warning, "go-clean will recreate go.mod and go.sum")
		warning = append(warning, "because you have selected --recursive")
		warning = append(warning, "this will redo _every_ repo. This is probably fine.")
		warning = append(warning, fmt.Sprintf("You have %d total repositories in %s", forge.Repos.Len(), forge.GetGoSrc()))
		warning = append(warning, "")
		warning = append(warning, "However, this will also, at times, do:")
		warning = append(warning, "")
		warning = append(warning, "rm -rf ~/go/pkg/")
		warning = append(warning, "rm -rf ~/.config/go-build/")
		warning = append(warning, "")
		warning = append(warning, "Which is also probably fine, but will clear all your build cache and go mod cache")
		warning = append(warning, "")
		simpleStdin(false, warning)
		// purgeGoCaches()
	} else {
		simpleStdin(true, []string{"go-clean will recreate go.mod and go.sum"})
	}

	// re-create go.sum and go.mod
	if _, err := check.RedoGoMod(); err != nil {
		badExit(err)
	}

	// try to trim junk
	if err := forge.TrimGoSum(check); err != nil {
		badExit(err)
	}

	// check go.sum file
	if err := forge.CleanGoDepsCheckOk(check); err != nil {
		log.Info("forge.FinalGoDepsCheck() failed. boo. :", check.GoPath)
		badExit(err)
	}

	log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
	okExit(check.GoPath + " 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.FindByGoPath(gopath)
		if check != nil {
			log.Info("findRepo() worked", check.GoPath)
			return check
		}
	}
	return nil
}

func okExit(thing string) {
	log.Info(thing, "ok")
	log.Info("Finished go-clean on", check.GetGoPath(), "ok")
	os.Exit(0)
}

func badExit(err error) {
	log.Info("go-clean failed: ", err, forge.GetGoSrc())
	os.Exit(-1)
}