diff options
| author | Jeff Carr <[email protected]> | 2024-11-29 18:15:25 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-11-29 18:15:25 -0600 |
| commit | 4a79cccffd6cd20e42271eada567c4b8c0edc2f4 (patch) | |
| tree | 4f1bf43db50e09956abe22d9fb3cb9d9b8ca4717 /goDep.redoGoMod.go | |
| parent | 7615317ca72347429f665dc7db9a0fe49d86cc55 (diff) | |
attempt to use go.wit.com/apps/autogenpb for sort
Diffstat (limited to 'goDep.redoGoMod.go')
| -rw-r--r-- | goDep.redoGoMod.go | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/goDep.redoGoMod.go b/goDep.redoGoMod.go new file mode 100644 index 0000000..6b6f222 --- /dev/null +++ b/goDep.redoGoMod.go @@ -0,0 +1,132 @@ +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.GoDeps.AppendUniqueGoPath(&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 +} +func (repo *Repo) RepoType() 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 +} |
