summaryrefslogtreecommitdiff
path: root/doSmart.go
blob: 1f3213868672f95104671c91097f4a2641c801df (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
package main

import (
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// this will make go.mod and go.sum files, but you have to
// have the files in .gitignore for now
func doSmart(repo *gitpb.Repo) error {
	// erase the go.mod and go.sum files
	eraseGoMod(repo)

	// if the repo has go.mod in the repo...
	if err := repo.RepoIgnoresGoMod(); err != nil {
		if repo.ParseGoSum() {
			return nil
		}
		log.Info("go-mod-clean can not run on this repo", repo.GetGoPath())
		log.Info("go.mod and go.sum must be git metadata to continue")
		// return nil
	}
	if repo.Exists("go.mod") {
		return nil
	}

	// try to restore from the git metadata
	cname := repo.GetCurrentBranchName()
	if err := repo.AutogenRestore(cname); err == nil {
		log.Info(repo.GetGoPath(), "files were restored ok from git metadata (notes)")
	}
	if repo.Exists("go.mod") {
		return nil
	}

	// attempt to restore from ~/go/pkg/mod/
	if err := restoreFromGoPkg(repo); err == nil {
		log.Info(repo.GetGoPath(), "files were restored ok ~/go/mod/")
	}
	if repo.Exists("go.mod") {
		return nil
	}

	// actually will re-create go.sum and go.mod now
	if err := redoGoMod(repo); err != nil {
		log.Info(repo.GetGoPath(), "the files were restored with redoGoMod()")
	}
	if repo.Exists("go.mod") {
		return nil
	}

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

	if repo.Exists("go.mod") {
		return nil
	}
	// last chance. just run go mod init
	repo.RunVerbose([]string{"go", "mod", "init"})

	return nil
}