summaryrefslogtreecommitdiff
path: root/godep.redoGoMod.go
diff options
context:
space:
mode:
Diffstat (limited to 'godep.redoGoMod.go')
-rw-r--r--godep.redoGoMod.go117
1 files changed, 117 insertions, 0 deletions
diff --git a/godep.redoGoMod.go b/godep.redoGoMod.go
new file mode 100644
index 0000000..2ce7676
--- /dev/null
+++ b/godep.redoGoMod.go
@@ -0,0 +1,117 @@
+package gitpb
+
+// does processing on the go.mod and go.sum files
+
+import (
+ "bufio"
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "go.wit.com/log"
+)
+
+// 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 (repo *Repo) MakeRedomod() (bool, error) {
+ // unset the go development ENV var to generate release files
+ os.Unsetenv("GO111MODULE")
+ if ok, err := repo.strictRun([]string{"rm", "-f", "go.mod", "go.sum"}); !ok {
+ log.Warn("rm go.mod go.sum failed", err)
+ return ok, err
+ }
+ if ok, err := repo.strictRun([]string{"go", "mod", "init", repo.GoPath}); !ok {
+ log.Warn("go mod init failed", err)
+ return ok, err
+ }
+ if ok, err := repo.strictRun([]string{"go", "mod", "tidy"}); !ok {
+ log.Warn("go mod tidy failed", err)
+ return ok, err
+ }
+ log.Info("MakeRedomod() worked", repo.GoPath)
+
+ if repo.Exists("go.sum") {
+ // return the attempt to parse go.mod & go.sum
+ return repo.parseGoSum()
+ }
+ repo.GoDeps = nil
+ repo.GoPrimitive = false
+
+ ok, err := repo.isPrimativeGoMod()
+ if err != nil {
+ // this means this repo does not depend on any other package
+ log.Info("PRIMATIVE repo error:", repo.GoPath, "err =", err)
+ return false, err
+ }
+ if ok {
+ // this means the repo is primitive so there is no go.sum
+ repo.GoPrimitive = true
+ return true, nil
+ }
+ // this should never happen
+ return false, errors.New("MakeRedomod() logic failed")
+}
+
+// reads and parses the go.sum file
+func (repo *Repo) parseGoSum() (bool, error) {
+ // empty out what was there before
+ repo.GoDeps = nil
+ tmp := filepath.Join(repo.FullPath, "go.sum")
+ gosum, err := os.Open(tmp)
+ if err != nil {
+ log.Warn("missing go.sum", repo.FullPath)
+ return false, err
+ }
+ defer gosum.Close()
+
+ scanner := bufio.NewScanner(gosum)
+ log.Info("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")
+ }
+ new1 := GoDep{
+ GoPath: godep,
+ Version: version,
+ }
+ repo.AppendGoDep(&new1)
+ /*
+ found := repo.FindGoDepByPath(godep)
+ if found == nil {
+ 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.Warn("\tgo.sum ", godep, "had both", version, currentversion)
+ }
+ } else {
+ deps[godep] = version
+ log.Info("\t", godep, "=", version)
+ }
+ */
+ } else {
+ // I've never seen this happen yet
+ panic(errors.New("go.sum invalid: " + line))
+ // return false, errors.New("go.sum invalid: " + line)
+ }
+ }
+
+ if err := scanner.Err(); err != nil {
+ repo.GoDeps = nil
+ return false, err
+ }
+ return true, nil
+}