summaryrefslogtreecommitdiff
path: root/repo.new.go
blob: 148005111a9228b4f4b0b4a12018e0a46a92fba4 (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
package gitpb

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

	"go.wit.com/log"
)

// scans in a new git repo. If it detects the repo is a golang project,
// then it parses the go.mod/go.sum files
// TODO: try adding python, rails, perl, rust, other language things?
// I probably will never have time to try that, but I'd take patches for anyone
// that might see this note and feel so inclined.
func (all *Repos) NewGoPath(basepath string, gopath string) (*Repo, error) {
	if r := all.FindByGoPath(gopath); r != nil {
		// already had this gopath
		return r, nil
	}

	// if .git doesn't exist, error out here
	gitpath := filepath.Join(basepath, gopath, ".git")
	_, err := os.Stat(gitpath)
	if err != nil {
		return nil, err
	}

	// add a new one here
	newr := Repo{
		FullPath: filepath.Join(basepath, gopath),
		GoPath:   gopath,
	}
	// newr.UpdateGit()
	newr.UpdateGitTags()

	all.add(&newr)
	return &newr, nil
}

// 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) isPrimativeGoMod() (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)
		repo.GoDeps = 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(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")
			}

		}
	}
	return true, nil
}