go primative logic is correct now

This commit is contained in:
Jeff Carr 2024-02-20 18:56:22 -06:00
parent 2a19cd5eef
commit 70e8c98b1c
2 changed files with 54 additions and 44 deletions

View File

@ -12,6 +12,9 @@ var INFO *log.LogFlag
var WARN *log.LogFlag
var CHANGE *log.LogFlag
var REPO *log.LogFlag
var REPOWARN *log.LogFlag
func init() {
full := "go.wit.com/lib/gui/repostatus"
short := "repostatus"
@ -19,4 +22,10 @@ func init() {
INFO = log.NewFlag("INFO", false, full, short, "general repo things")
WARN = log.NewFlag("WARN", true, full, short, "bad things")
CHANGE = log.NewFlag("CHANGE", true, full, short, "when repo changes")
full = "go.wit.com/lib/gui/repo"
short = "repo"
REPO = log.NewFlag("REPO", false, full, short, "general repo things")
REPOWARN = log.NewFlag("REPOWARN", true, full, short, "repo warnings")
}

View File

@ -15,13 +15,14 @@ import (
// 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(WARN, "isPrimativeGoMod()", rs.realPath.String())
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(WARN, "missing go.mod", rs.realPath.String())
log.Log(REPO, "missing go.mod", rs.realPath.String())
rs.goConfig = nil
return false, err
}
@ -32,12 +33,12 @@ func (rs *RepoStatus) isPrimativeGoMod() (bool, error) {
line := strings.TrimSpace(scanner.Text())
parts := strings.Split(line, " ")
log.Log(INFO, " gomod:", parts)
log.Log(REPO, " gomod:", parts)
if len(parts) >= 1 {
log.Log(INFO, " gomod: part[0] =", parts[0])
log.Log(REPO, " gomod: part[0] =", parts[0])
if parts[0] == "require" {
log.Log(INFO, " should return false here")
return false, errors.New("bad go.mod file" + rs.GoPath())
log.Log(REPO, " should return false here")
return false, errors.New("go.mod file is not primative")
}
}
@ -46,28 +47,12 @@ func (rs *RepoStatus) isPrimativeGoMod() (bool, error) {
}
// readGoMod reads and parses the go.sum file
// saves the config information in repo.goConfig
// saves the config information in *Repo.goConfig
func (rs *RepoStatus) parseGoSum() (bool, error) {
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)
rs.goConfig = nil
rs.primitive.SetText("false")
return false, err
}
if ok {
// this means the repo is primitive so there is no go.sum
rs.goConfig = nil
rs.primitive.SetText("true")
return true, nil
}
rs.primitive.SetText("false")
tmp := filepath.Join(rs.realPath.String(), "go.sum")
gosum, err := os.Open(tmp)
if err != nil {
log.Log(WARN, "missing go.sum", rs.realPath.String())
log.Log(REPO, "missing go.sum", rs.realPath.String())
rs.goConfig = nil
return false, err
}
@ -77,7 +62,7 @@ func (rs *RepoStatus) parseGoSum() (bool, error) {
deps = make(GoConfig)
scanner := bufio.NewScanner(gosum)
log.Log(INFO, "gosum:", tmp)
log.Log(REPO, "gosum:", tmp)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
@ -90,23 +75,21 @@ func (rs *RepoStatus) parseGoSum() (bool, error) {
}
currentversion, ok := deps[godep]
if ok {
if currentversion != version {
// ignore these warnings for now
depname := rs.String()
if strings.HasPrefix(depname, "go.wit.com") {
log.Log(INFO, "REPO:", rs.realPath.String())
log.Log(INFO, " version mismatch:", godep, version, currentversion)
} else {
log.Log(INFO, "REPO:", rs.realPath.String())
log.Log(INFO, " version mismatch:", godep, version, currentversion)
}
// 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(INFO, "\t", godep, "=", version)
log.Log(REPO, "\t", godep, "=", version)
}
} else {
log.Log(WARN, "\t INVALID:", parts)
// I've never seen this happen yet
return false, errors.New("go.sum invalid: " + line)
}
}
@ -131,7 +114,7 @@ func (rs *RepoStatus) MakeRedomod() (bool, error) {
var err error
var output string
if rs.ReadOnly() {
log.Log(WARN, "will not go mod redo read only repos", rs.String())
log.Log(REPO, "will not go mod redo read only repos", rs.String())
return false, errors.New(rs.GoPath() + " is read-only ")
}
@ -139,23 +122,41 @@ func (rs *RepoStatus) MakeRedomod() (bool, error) {
os.Unsetenv("GO111MODULE")
err, output = rs.RunCmd([]string{"rm", "-f", "go.mod", "go.sum"})
if err != nil {
log.Log(WARN, "rm failed", err, output)
log.Log(REPO, "rm failed", err, output)
return false, err
}
err, output = rs.RunCmd([]string{"go", "mod", "init"})
if err != nil {
log.Log(WARN, "go mod init failed", err, output)
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(WARN, "go mod tidy failed", err, output)
log.Log(REPO, "go mod tidy failed", err, output)
return false, err
}
log.Log(INFO, "MakeRedomod() worked", rs.GoPath())
log.Log(REPO, "MakeRedomod() worked", rs.GoPath())
// return the attempt to parse go.mod & go.sum
return rs.parseGoSum()
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 {