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

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

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

// reads and parses the go.sum file into a protobuf struct
// this function isn't supposed to change anything, just parse the existing files
func (repo *Repo) ParseGoSum() bool {
	// empty out what was there before
	repo.GoDeps = nil

	// check of the repo is a primitive
	// that means, there is not a go.sum file
	// because the package is completely self contained!
	if err := repo.setPrimitive(); err != nil {
		// temporarily enabled this. this is really noisy
		// log.Info("gitpb.ParseGoSum()", err)
		return false
	}
	if repo.GetGoPrimitive() {
		// log.Info("This repo is primitive!")
		return true
	}
	tmp := filepath.Join(repo.FullPath, "go.sum")
	gosum, err := os.Open(tmp)
	defer gosum.Close()
	if err != nil {
		log.Info("gitpb.ParseGoSum() missing go.sum. Some error happened with go mod init & tidy", err)
		return false
	}

	scanner := bufio.NewScanner(gosum)
	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")
			}
			new1 := GoDep{
				GoPath:  godep,
				Version: version,
			}
			if repo.GoDeps == nil {
				repo.GoDeps = new(GoDeps)
			}
			repo.GoDeps.AppendByGoPath(&new1)
		} else {
			log.Info("gitpb.ParseGoSum() go.sum parse error invalid:", line)
			return false
		}
	}

	if err := scanner.Err(); err != nil {
		repo.GoDeps = nil
		log.Info("gitpb.ParseGoSum()", err)
		return false
	}
	return true
}

// attempt to parse go.* files in a directory
func GoSumParseDir(moddir string) (*GoDeps, error) {
	isprim, err := computePrimitive(moddir)
	if err != nil {
		// "go mod init" failed
		return nil, err
	}
	if isprim {
		// might be a GO primitive. no go.sum file
		return nil, nil
	}
	// go.sum exists. parse the go.sum file
	return parseGoSum(moddir)
}

// Detect a 'Primitive' package. Sets the isPrimitive flag
// will return true if the repo is truly not dependent on _anything_ else
// like spew or lib/widget
// it assumes 'go mod init' and 'go mod tidy' ran without error
func computePrimitive(moddir string) (bool, error) {
	// go mod init & go mod tidy ran without errors
	log.Log(INFO, "isPrimitiveGoMod()", moddir)
	gomod, err := os.Open(filepath.Join(moddir, "go.mod"))
	if err != nil {
		log.Log(INFO, "missing go.mod", moddir)
		return false, err
	}
	defer gomod.Close()

	if shell.Exists(filepath.Join(moddir, "go.sum")) {
		return false, nil
	}

	scanner := bufio.NewScanner(gomod)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())

		parts := strings.Fields(line)
		log.Log(INFO, "  gomod:", parts)
		if len(parts) >= 1 {
			log.Log(INFO, "  gomod: part[0] =", parts[0])
			if parts[0] == "require" {
				log.Log(INFO, "  should return false here")
				return false, errors.New("go.mod file is not primitive")
			}
			/*
				if parts[0] == "go" {
					if parts[1] != "1.21" {
						log.Log(WARN, "go not set to 1.21 for", repo.GetGoPath())
						// return false, errors.New("go not set to 1.21 for " + repo.GetGoPath())
					}
				}
			*/
		}
	}
	return true, nil
}

// parse the go.sum file into a protobuf
func parseGoSum(moddir string) (*GoDeps, error) {
	godeps := new(GoDeps)

	tmp, err := os.Open(filepath.Join(moddir, "go.sum"))
	defer tmp.Close()
	if err != nil {
		log.Info("gitpb.ParseGoSum() missing go.sum. Some error happened with go mod init & tidy", err)
		return nil, err
	}

	scanner := bufio.NewScanner(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")
			}
			new1 := GoDep{
				GoPath:  godep,
				Version: version,
			}
			godeps.AppendByGoPath(&new1)
		} else {
			return nil, fmt.Errorf("gitpb.ParseGoSum() go.sum parse error invalid: %s", line)
		}
	}

	if err := scanner.Err(); err != nil {
		godeps = nil
		return nil, err
	}
	return godeps, nil
}

func (repo *Repo) GoSumFromPkgDir() (*GoDeps, error) {
	homedir, err := os.UserHomeDir()
	if err != nil {
		return nil, err
	}
	rver := repo.GetLastTag()
	if rver == "" {
		return nil, errors.New("could not get master version")
	}
	goget := repo.GetGoPath() + "@" + rver
	moddir := filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver)

	if !shell.IsDir(moddir) {
		cmd := []string{"go", "get", goget}
		repo.RunVerboseOnError(cmd)
	}

	if !shell.IsDir(moddir) {
		return nil, errors.New("missing go/pkg/mod. Run: go get " + goget)
	}
	return GoSumParseDir(moddir)
}

func (repo *Repo) GoSumFromRepo() (*GoDeps, error) {
	return GoSumParseDir(repo.GetFullPath())
}