summaryrefslogtreecommitdiff
path: root/isPrimitive.go
blob: 657901321ae5b30601f0dfbf6be0628004cb8555 (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
package gitpb

// only reads in the go.mod file. doesn't change anything

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 (repo *Repo) IsPrimitive() (bool, error) {
	// go mod init & go mod tidy ran without errors
	log.Log(GITPB, "isPrimativeGoMod()", repo.FullPath)
	tmp := filepath.Join(repo.FullPath, "go.mod")
	gomod, err := os.Open(tmp)
	if err != nil {
		log.Log(GITPB, "missing go.mod", repo.FullPath)
		return false, err
	}
	defer gomod.Close()

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

		parts := strings.Split(line, " ")
		log.Log(GITPB, "  gomod:", parts)
		if len(parts) >= 1 {
			log.Log(GITPB, "  gomod: part[0] =", parts[0])
			if parts[0] == "require" {
				log.Log(GITPB, "  should return false here")
				return false, errors.New("go.mod file is not primative")
			}

		}
	}
	repo.GoPrimitive = true
	repo.GoDeps = nil
	return true, nil
}