2024-02-20 06:54:11 -06:00
|
|
|
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
|
2024-02-20 18:56:22 -06:00
|
|
|
// it assumes go mod ran init and tidy ran without error
|
2024-02-20 06:54:11 -06:00
|
|
|
func (rs *RepoStatus) isPrimativeGoMod() (bool, error) {
|
|
|
|
// go mod init & go mod tidy ran without errors
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "isPrimativeGoMod()", rs.realPath.String())
|
2024-02-20 06:54:11 -06:00
|
|
|
tmp := filepath.Join(rs.realPath.String(), "go.mod")
|
|
|
|
gomod, err := os.Open(tmp)
|
|
|
|
if err != nil {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "missing go.mod", rs.realPath.String())
|
2024-02-20 06:54:11 -06:00
|
|
|
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, " ")
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, " gomod:", parts)
|
2024-02-20 06:54:11 -06:00
|
|
|
if len(parts) >= 1 {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, " gomod: part[0] =", parts[0])
|
2024-02-20 06:54:11 -06:00
|
|
|
if parts[0] == "require" {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, " should return false here")
|
|
|
|
return false, errors.New("go.mod file is not primative")
|
2024-02-20 06:54:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readGoMod reads and parses the go.sum file
|
2024-02-20 18:56:22 -06:00
|
|
|
// saves the config information in *Repo.goConfig
|
2024-02-20 06:54:11 -06:00
|
|
|
func (rs *RepoStatus) parseGoSum() (bool, error) {
|
|
|
|
tmp := filepath.Join(rs.realPath.String(), "go.sum")
|
|
|
|
gosum, err := os.Open(tmp)
|
|
|
|
if err != nil {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "missing go.sum", rs.realPath.String())
|
2024-02-20 06:54:11 -06:00
|
|
|
rs.goConfig = nil
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer gosum.Close()
|
|
|
|
|
|
|
|
var deps GoConfig
|
|
|
|
deps = make(GoConfig)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(gosum)
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "gosum:", tmp)
|
2024-02-20 06:54:11 -06:00
|
|
|
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 {
|
2024-02-20 18:56:22 -06:00
|
|
|
// 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)
|
2024-02-20 06:54:11 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deps[godep] = version
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "\t", godep, "=", version)
|
2024-02-20 06:54:11 -06:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-20 18:56:22 -06:00
|
|
|
// I've never seen this happen yet
|
|
|
|
return false, errors.New("go.sum invalid: " + line)
|
2024-02-20 06:54:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
rs.goConfig = nil
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.goConfig = deps
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-02-20 14:45:43 -06:00
|
|
|
func (rs *RepoStatus) GoConfig() map[string]string {
|
2024-02-20 06:54:11 -06:00
|
|
|
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() {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "will not go mod redo read only repos", rs.String())
|
2024-02-20 06:54:11 -06:00
|
|
|
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 {
|
2024-11-07 07:04:39 -06:00
|
|
|
log.Log(REPO, "rm go.mod go.sum failed", err, output)
|
2024-02-20 06:54:11 -06:00
|
|
|
return false, err
|
|
|
|
}
|
2024-03-09 18:30:04 -06:00
|
|
|
err, output = rs.RunCmd([]string{"go", "mod", "init", rs.GoPath()})
|
2024-02-20 06:54:11 -06:00
|
|
|
if err != nil {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "go mod init failed", err, output)
|
2024-02-20 06:54:11 -06:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
err, output = rs.RunCmd([]string{"go", "mod", "tidy"})
|
|
|
|
if err != nil {
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "go mod tidy failed", err, output)
|
2024-02-20 06:54:11 -06:00
|
|
|
return false, err
|
|
|
|
}
|
2024-02-20 18:56:22 -06:00
|
|
|
log.Log(REPO, "MakeRedomod() worked", rs.GoPath())
|
2024-02-20 06:54:11 -06:00
|
|
|
|
2024-02-20 18:56:22 -06:00
|
|
|
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
|
2024-02-20 06:54:11 -06:00
|
|
|
}
|
2024-02-20 14:45:43 -06:00
|
|
|
|
|
|
|
func (rs *RepoStatus) IsReleased() bool {
|
|
|
|
if rs.GetTargetVersion() == rs.GetCurrentVersion() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|