summaryrefslogtreecommitdiff
path: root/goDep.redoGoMod.go
blob: 6aeb298bdd730a6cbac3944923f956326167c591 (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
package gitpb

// does processing on the go.mod and go.sum files

import (
	"errors"
	"os"

	"go.wit.com/log"
)

// remove every go.mod and go.sum
// testing to see where this stuff is coming from
func (repo *Repo) EraseGoMod() {
	// unset the go development ENV var to generate release files
	if ok, err := repo.strictRun([]string{"rm", "-f", "go.mod", "go.sum"}); !ok {
		log.Warn("rm go.mod go.sum failed", err)
	}
}

// poor name perhaps. It's because in most of these
// repos you can also type "make redomod" to do the same thing
// since it's a Makefile task that is also useful to be able to run
// from the command line
func (repo *Repo) RedoGoMod() (bool, error) {
	// unset the go development ENV var to generate release files
	os.Unsetenv("GO111MODULE")
	if ok, err := repo.strictRun([]string{"rm", "-f", "go.mod", "go.sum"}); !ok {
		log.Warn("rm go.mod go.sum failed", err)
		return ok, err
	}
	if ok, err := repo.strictRun([]string{"go", "mod", "init", repo.GoPath}); !ok {
		log.Warn("go mod init failed", err)
		return ok, err
	}
	if ok, err := repo.strictRun([]string{"go", "mod", "tidy"}); !ok {
		log.Warn("go mod tidy failed", err)
		return ok, err
	}
	// most things should build with golang after 1.20
	if ok, err := repo.strictRun([]string{"go", "mod", "edit", "-go=1.20"}); !ok {
		log.Warn("go mod edit failed", err)
		return ok, err
	}
	// log.Info("MakeRedomod() worked", repo.GoPath)

	if repo.Exists("go.sum") {
		// return the attempt to parse go.mod & go.sum
		return repo.ParseGoSum()
	}
	repo.GoDeps = new(GoDeps)
	repo.GoPrimitive = false

	ok, err := repo.IsPrimitive()
	if err != nil {
		// this means this repo does not depend on any other package
		log.Info("PRIMATIVE repo error:", repo.GoPath, "err =", err)
		return false, err
	}
	if ok {
		// this means the repo is primitive so there is no go.sum
		repo.GoPrimitive = true
		return true, nil
	}
	// this should never happen
	return false, errors.New("MakeRedomod() logic failed")
}

// returns true if the last published
func (repo *Repo) GoDepsLen() int {
	if repo.GoDeps == nil {
		return 0
	}
	return repo.GoDeps.Len()
}

// returns true if the last published
func (repo *Repo) PublishedLen() int {
	if repo.Published == nil {
		return 0
	}
	return repo.Published.Len()
}

// returns true if the last published
func (all *Repos) GoDepsChanged(repo *Repo) (bool, error) {
	var upgrade bool = false
	if repo.GoDeps == nil {
		repo.RedoGoMod()
	}
	if repo.GoDeps.Len() == 0 {
		repo.RedoGoMod()
	}
	log.Printf("current repo %s go dependancy count: %d", repo.GetGoPath(), repo.GoDeps.Len())
	deps := repo.GoDeps.SortByGoPath()
	for deps.Scan() {
		depRepo := deps.Next()
		if repo.Published == nil {
			return false, errors.New("repo published deps info is nil")
		}
		found := repo.Published.FindByGoPath(depRepo.GetGoPath())
		if found == nil {
			log.Printf("dep %-50s %-10s vs %-10s", depRepo.GetGoPath(), depRepo.GetVersion(), "NEW")
			upgrade = true
			continue
			// return upgrade, errors.New("new repo added " + depRepo.GetGoPath())
		}
		if depRepo.GetVersion() == found.GetVersion() {
			// log.Printf("deps %-50s %-10s vs %-10s", depRepo.GetGoPath(), depRepo.GetVersion(), found.GetVersion())
		} else {
			log.Printf("dep %-50s %-10s vs %-10s BROKEN", depRepo.GetGoPath(), depRepo.GetVersion(), found.GetVersion())
			upgrade = true
		}
	}
	return upgrade, nil
}