summaryrefslogtreecommitdiff
path: root/main.go
blob: 61bf9b23718815b928fb3561e03f7ad01bbf962b (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main

import (
	"errors"
	"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
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.SortByGoPath()
		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)")
			os.Exit(-1)
		}

		if err := doMain(check); err != nil {
			badExit(check, err)
		}
	}

	if configSave {
		forge.ConfigSave()
	}

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

func saveAsMetadata(repo *gitpb.Repo) error {
	cname := repo.GetCurrentBranchName()
	if repo.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 doMain(repo *gitpb.Repo) error {
	var perfect bool = true
	if !repo.IsValid() {
		log.Info(repo.GoPath, "is invalid. fix your repos.pb file with 'forge' first")
		log.Info("")
		log.Info("go install go.wit.com/apps/forge@latest")
		log.Info("")
		return errors.New(repo.GoPath + " is invalid. fix your repository list with 'forge' first")
	} else {
		log.Info(repo.GoPath, "is valid according to forge")
	}

	// skip restore if --force
	if !argv.Force {
		// erase the go.mod and go.sum files
		eraseGoMod(repo)
		cname := repo.GetCurrentBranchName()
		// try to restore from the git metadata
		if err := repo.AutogenRestore(cname); err != nil {
			// ignore errors here
		}
		if err := repo.ValidGoSum(); err == nil {
			log.Info(repo.GoPath, "go.mod and go.sum were restored ok")
			configSave = true
			return nil
		}
	}

	// double check here. use --force to remake them
	if err := repo.ValidGoSum(); err == nil {
		log.Info(repo.GoPath, "go.mod and go.sum are already valid")
		return nil
	}

	if repo.GetMasterBranchName() != repo.GetCurrentBranchName() {
		perfect = false
		if argv.Strict {
			log.Info("")
			log.Info("You are not operating on your git master branch.")
			log.Info("Publishing go.mod & go.sum files must come from from git version tag on the master branch")
			log.Info("")
			return errors.New(repo.GoPath + " not in the git master branch")
		}
	}

	ok, err := repoIgnoresGoMod(repo)
	if err != nil {
		log.Info(repo.GoPath, "some wierd git error happened. investigate.", err)
		return err
	}
	// if ok, then git owns 'go.mod' and we can't really do anything
	// todo: ignore this with --force
	if ok {
		log.Info(repo.GoPath, "git says it does not own go.mod")
		// continue and attempt to create go.mod and go.sum
	} else {
		if forge.Config.IsReadOnly(repo.GoPath) {
			log.Info("you can not push to read only repositories.", repo.GoPath)
			log.Info("change your .config/forge/ to indicate you own this repository")
			return nil
		}
		perfect = false
		// continue and attempt to create go.mod and go.sum
	}

	if repo.CheckDirty() {
		perfect = false
		if argv.Strict {
			log.Info("")
			log.Info("You can not continue on a dirty git repo.")
			log.Info("")
			return errors.New(repo.GoPath + " git repo is dirty")
		}
	}

	log.Info(repo.GoPath, "GOING TO MAKE NEW go.* FILES")

	// actually will re-create go.sum and go.mod now
	if err := redoGoMod(repo); err != nil {
		return err
	}

	if argv.Trim {
		// the first time, it'll attempt to fix some stuff
		cleanGoDepsCheckOk(repo)
		// try to trim junk
		if err := trimGoSum(repo); err != nil {
			return err
		}
		repo.ParseGoSum()
	}

	/*
		data, _ := repo.ReadFile("go.mod")
		log.Info(string(data))
		data, _ = repo.ReadFile("go.sum")
		log.Info(string(data))
	*/

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

	// if everything is perfect, save them as git metadata
	if perfect {
		// put the files in the notes section in git
		// this way, git commits are not messed up
		// with this autogenerated code
		if err := saveAsMetadata(repo); err != nil {
			log.Info("save go.mod as git metadata failed", repo.GoPath, err)
			return err
		}
	}

	// everything worked!
	configSave = true
	return nil
}