summaryrefslogtreecommitdiff
path: root/reloadRepoType.go
blob: 435b82e31f3b621a4d86c58fb2e523505877e052 (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
package gitpb

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

import (
	"os"
	"strings"

	"go.wit.com/log"
)

// TODO: this needs to be redone in a smarter way
// to identify which repos have things to build in them
func (repo *Repo) GetRepoType() string {
	if repo == nil {
		return "nil"
	}
	if repo.GoInfo == nil {
		log.Warn("gitpb.RepoType() plugin was not set correctly")
		log.Warn("gitpb.RepoType() plugin was not set correctly")
		log.Warn("gitpb.RepoType() plugin was not set correctly")
		repo.GoInfo = new(GoInfo)
		repo.setRepoType()
	}

	if repo.GoInfo.GoPlugin {
		return "plugin"
	}
	if repo.GoInfo.GoBinary {
		if repo.Exists(".plugin") {
			log.Warn("gitpb.RepoType() plugin was not set correctly")
			repo.GoInfo.GoPlugin = true
			return "plugin"
		}
		return "binary"
	}
	// binary should always take precidence over libraries that are protobuf's
	if repo.GoInfo.GoProtobuf {
		return "protobuf"
	}
	if repo.GoInfo.GoLibrary {
		return "library"
	}
	// todo: figure out what to do here. for now, binary is easiest
	return "library"
}

func (repo *Repo) setRepoType() {
	if repo == nil {
		return
	}
	if repo.Exists(".plugin") {
		repo.GoInfo.GoPlugin = true
	}
	if ok, _, _ := repo.IsProtobuf(); ok {
		repo.GoInfo.GoProtobuf = true
	}
	switch repo.goListRepoType() {
	case "binary":
		repo.GoInfo.GoBinary = true
		return
	case "library":
		repo.GoInfo.GoLibrary = true
		return
	}
}

func (repo *Repo) goListRepoType() string {
	os.Setenv("GO111MODULE", "off")
	cmd := []string{"go", "list", "-f", "'{{if eq .Name \"main\"}}binary{{else}}library{{end}}'"}
	// cmd := []string{"go", "list", "-f", "'{{.Name}}'"} // probably use this. this just prints out the package name
	// cmd := []string{"go", "list", "-f", "'{{.ImportPath}}'"} // returns go.wit.com/lib/protobuf/gitpb

	result := repo.RunQuiet(cmd)
	if result.Error != nil {
		log.Warn("go list binary detect failed", result.Error)
		return ""
	}
	output := strings.TrimSpace(strings.Join(result.Stdout, "\n"))
	output = strings.Trim(output, "'")
	return output
}