summaryrefslogtreecommitdiff
path: root/repoType.go
blob: 52846d2b2145e5c5c8159ef3e78ead95926b961b (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

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

import (
	"os"
	"strings"

	"go.wit.com/log"
)

func (repo *Repo) RepoType() string {
	if repo == nil {
		return "nil"
	}
	if repo.GetGoPlugin() {
		return "plugin"
	}
	if repo.GetGoBinary() {
		if repo.Exists(".plugin") {
			return "plugin"
		}
		return "binary"
	}
	if ok, _, _ := repo.IsProtobuf(); ok {
		return "protobuf"
	}
	if repo.GetGoLibrary() {
		return "library"
	}
	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
}