gitpb/reloadRepoType.go

82 lines
1.8 KiB
Go
Raw Permalink Normal View History

package gitpb
// does processing on the go.mod and go.sum files
import (
"os"
"strings"
"go.wit.com/log"
)
2024-12-17 06:37:14 -06:00
func (repo *Repo) GetRepoType() string {
if repo == nil {
return "nil"
}
2024-12-17 06:37:14 -06:00
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"
}
2024-12-17 06:37:14 -06:00
if repo.GoInfo.GoProtobuf {
return "protobuf"
}
if repo.GoInfo.GoBinary {
if repo.Exists(".plugin") {
2024-12-17 06:37:14 -06:00
log.Warn("gitpb.RepoType() plugin was not set correctly")
repo.GoInfo.GoPlugin = true
return "plugin"
}
return "binary"
}
2024-12-17 06:37:14 -06:00
if repo.GoInfo.GoLibrary {
return "library"
}
2024-12-17 23:01:13 -06:00
// todo: figure out what to do here. for now, binary is easiest
2024-12-17 23:27:17 -06:00
return "library"
2024-12-17 06:37:14 -06:00
}
func (repo *Repo) setRepoType() {
if repo == nil {
return
}
if repo.Exists(".plugin") {
repo.GoInfo.GoPlugin = true
return
}
if ok, _, _ := repo.IsProtobuf(); ok {
2024-12-17 06:37:14 -06:00
repo.GoInfo.GoProtobuf = true
return
}
2024-12-17 06:37:14 -06:00
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
}