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

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

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

	"go.wit.com/log"
)

// reads and parses the go.sum file
// does not change anything
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
		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
}