repostatus/goConfig.go

168 lines
4.4 KiB
Go

package repostatus
// does processing on the go.mod and go.sum files
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 (rs *RepoStatus) isPrimativeGoMod() (bool, error) {
// go mod init & go mod tidy ran without errors
log.Log(REPO, "isPrimativeGoMod()", rs.realPath.String())
tmp := filepath.Join(rs.realPath.String(), "go.mod")
gomod, err := os.Open(tmp)
if err != nil {
log.Log(REPO, "missing go.mod", rs.realPath.String())
rs.goConfig = 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(REPO, " gomod:", parts)
if len(parts) >= 1 {
log.Log(REPO, " gomod: part[0] =", parts[0])
if parts[0] == "require" {
log.Log(REPO, " should return false here")
return false, errors.New("go.mod file is not primative")
}
}
}
return true, nil
}
// readGoMod reads and parses the go.sum file
// saves the config information in *Repo.goConfig
func (rs *RepoStatus) parseGoSum() (bool, error) {
tmp := filepath.Join(rs.realPath.String(), "go.sum")
gosum, err := os.Open(tmp)
if err != nil {
log.Log(REPO, "missing go.sum", rs.realPath.String())
rs.goConfig = nil
return false, err
}
defer gosum.Close()
var deps GoConfig
deps = make(GoConfig)
scanner := bufio.NewScanner(gosum)
log.Log(REPO, "gosum:", tmp)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
parts := strings.Split(line, " ")
if len(parts) == 3 {
godep := strings.TrimSpace(parts[0])
version := strings.TrimSpace(parts[1])
if strings.HasSuffix(version, "/go.mod") {
version = strings.TrimSuffix(version, "/go.mod")
}
currentversion, ok := deps[godep]
if ok {
// only use the first value found in the file?
// this shouldn't have been possible. this function should
// only be called from MakeRedomod()
// todo: make go things a seperate package so this function
// isn't exported?
if version != currentversion {
log.Log(REPOWARN, "\tgo.sum ", godep, "had both", version, currentversion)
}
} else {
deps[godep] = version
log.Log(REPO, "\t", godep, "=", version)
}
} else {
// I've never seen this happen yet
return false, errors.New("go.sum invalid: " + line)
}
}
if err := scanner.Err(); err != nil {
rs.goConfig = nil
return false, err
}
rs.goConfig = deps
return true, nil
}
func (rs *RepoStatus) GoConfig() map[string]string {
return rs.goConfig
}
// poor name perhaps. It's because in most of these
// repos you can also type "make redomod" to do the same thing
// since it's a Makefile task that is also useful to be able to run
// from the command line
func (rs *RepoStatus) MakeRedomod() (bool, error) {
var err error
var output string
if rs.ReadOnly() {
log.Log(REPO, "will not go mod redo read only repos", rs.String())
return false, errors.New(rs.GoPath() + " is read-only ")
}
// unset the go development ENV var to generate release files
os.Unsetenv("GO111MODULE")
err, output = rs.RunCmd([]string{"rm", "-f", "go.mod", "go.sum"})
if err != nil {
log.Log(REPO, "rm failed", err, output)
return false, err
}
err, output = rs.RunCmd([]string{"go", "mod", "init"})
if err != nil {
log.Log(REPO, "go mod init failed", err, output)
return false, err
}
err, output = rs.RunCmd([]string{"go", "mod", "tidy"})
if err != nil {
log.Log(REPO, "go mod tidy failed", err, output)
return false, err
}
log.Log(REPO, "MakeRedomod() worked", rs.GoPath())
if rs.Exists("go.sum") {
// return the attempt to parse go.mod & go.sum
return rs.parseGoSum()
}
rs.goConfig = nil
rs.primitive.SetText("false")
ok, err := rs.isPrimativeGoMod()
if err != nil {
// this means this repo does not depend on any other package
log.Info("PRIMATIVE repo:", rs.String(), "err =", err)
return false, err
}
if ok {
// this means the repo is primitive so there is no go.sum
rs.primitive.SetText("true")
return true, nil
}
// this should never happen
return false, nil
}
func (rs *RepoStatus) IsReleased() bool {
if rs.GetTargetVersion() == rs.GetCurrentVersion() {
return true
}
return false
}