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
|
package repostatus
// does processing on the go.mod and go.sum files
import (
"bufio"
"errors"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
// Detect a 'Primative' package. Sets the isPrimative flag
// will return true if the repo is truly not dependent on _anything_ else
// like spew or lib/widget
// it assumes go mod ran init and tidy ran without error
func (rs *RepoStatus) isPrimativeGoMod() (bool, error) {
// go mod init & go mod tidy ran without errors
log.Log(REPO, "isPrimativeGoMod()", rs.realPath.String())
tmp := filepath.Join(rs.realPath.String(), "go.mod")
gomod, err := os.Open(tmp)
if err != nil {
log.Log(REPO, "missing go.mod", rs.realPath.String())
rs.goConfig = nil
return false, err
}
defer gomod.Close()
scanner := bufio.NewScanner(gomod)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
parts := strings.Split(line, " ")
log.Log(REPO, " gomod:", parts)
if len(parts) >= 1 {
log.Log(REPO, " gomod: part[0] =", parts[0])
if parts[0] == "require" {
log.Log(REPO, " should return false here")
return false, errors.New("go.mod file is not primative")
}
}
}
return true, nil
}
// readGoMod reads and parses the go.sum file
// saves the config information in *Repo.goConfig
func (rs *RepoStatus) parseGoSum() (bool, error) {
tmp := filepath.Join(rs.realPath.String(), "go.sum")
gosum, err := os.Open(tmp)
if err != nil {
log.Log(REPO, "missing go.sum", rs.realPath.String())
rs.goConfig = nil
return false, err
}
defer gosum.Close()
var deps GoConfig
deps = make(GoConfig)
scanner := bufio.NewScanner(gosum)
log.Log(REPO, "gosum:", tmp)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
parts := strings.Split(line, " ")
if len(parts) == 3 {
godep := strings.TrimSpace(parts[0])
version := strings.TrimSpace(parts[1])
if strings.HasSuffix(version, "/go.mod") {
version = strings.TrimSuffix(version, "/go.mod")
}
currentversion, ok := deps[godep]
if ok {
// only use the first value found in the file?
// this shouldn't have been possible. this function should
// only be called from MakeRedomod()
// todo: make go things a seperate package so this function
// isn't exported?
if version != currentversion {
log.Log(REPOWARN, "\tgo.sum ", godep, "had both", version, currentversion)
}
} else {
deps[godep] = version
log.Log(REPO, "\t", godep, "=", version)
}
} else {
// I've never seen this happen yet
return false, errors.New("go.sum invalid: " + line)
}
}
if err := scanner.Err(); err != nil {
rs.goConfig = nil
return false, err
}
rs.goConfig = deps
return true, nil
}
func (rs *RepoStatus) GoConfig() map[string]string {
return rs.goConfig
}
// for now, even check cmd.Exit
func (rs *RepoStatus) strictRun(cmd []string) (bool, error) {
r := rs.Run(cmd)
if r.Error != nil {
log.Log(REPO, "go mod init failed err:", r.Error)
return false, r.Error
}
if r.Exit != 0 {
log.Log(REPO, "go mod init exit =", r.Exit)
return false, r.Error
}
return true, nil
}
// 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 (rs *RepoStatus) MakeRedomod() (bool, error) {
if rs.ReadOnly() {
log.Log(REPO, "will not go mod redo read only repos", rs.String())
return false, errors.New(rs.GoPath() + " is read-only ")
}
// unset the go development ENV var to generate release files
os.Unsetenv("GO111MODULE")
if ok, err := rs.strictRun([]string{"rm", "-f", "go.mod", "go.sum"}); !ok {
log.Log(REPO, "rm go.mod go.sum failed", err)
return ok, err
}
if ok, err := rs.strictRun([]string{"go", "mod", "init", rs.GoPath()}); !ok {
log.Log(REPO, "go mod init failed", err)
return ok, err
}
if ok, err := rs.strictRun([]string{"go", "mod", "tidy"}); !ok {
log.Log(REPO, "go mod tidy failed", err)
return ok, err
}
log.Log(REPO, "MakeRedomod() worked", rs.GoPath())
if rs.Exists("go.sum") {
// return the attempt to parse go.mod & go.sum
return rs.parseGoSum()
}
rs.goConfig = nil
rs.primitive.SetText("false")
ok, err := rs.isPrimativeGoMod()
if err != nil {
// this means this repo does not depend on any other package
log.Info("PRIMATIVE repo:", rs.String(), "err =", err)
return false, err
}
if ok {
// this means the repo is primitive so there is no go.sum
rs.primitive.SetText("true")
return true, nil
}
// this should never happen
return false, errors.New("MakeRedomod() logic failed")
}
func (rs *RepoStatus) IsReleased() bool {
if rs.GetTargetVersion() == rs.GetCurrentVersion() {
return true
}
return false
}
|