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

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

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

	"go.wit.com/log"
)

// reads and parses the go.sum file
// does not change anything
func (repo *Repo) ParseGoSum() (bool, error) {
	// empty out what was there before
	repo.GoDeps = nil

	// check of the repo is a primative
	// that means, there is not a go.sum file
	// because the package is completely self contained!
	if ok, _ := repo.IsPrimitive(); ok {
		log.Info("This repo is primative!")
		return true, nil
	}
	tmp := filepath.Join(repo.FullPath, "go.sum")
	gosum, err := os.Open(tmp)
	defer gosum.Close()
	if err != nil {
		return false, err
	}

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

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

// reads and parses the go.sum file
// is identical to the one above, change that
func (repo *Repo) UpdatePublished() (bool, error) {
	// empty out what was there before
	repo.Published = nil
	tmp := filepath.Join(repo.FullPath, "go.sum")
	gosum, err := os.Open(tmp)
	if err != nil {
		log.Warn("missing go.sum", repo.FullPath)
		return false, err
	}
	defer gosum.Close()

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

	if err := scanner.Err(); err != nil {
		repo.Published = nil
		return false, err
	}
	return true, nil
}