gomod2nix/fetch/sum.go

42 lines
860 B
Go
Raw Normal View History

2020-07-20 11:38:32 +00:00
package fetch
2020-07-20 10:52:58 +00:00
import (
"bytes"
"fmt"
"io/ioutil"
"regexp"
"strings"
)
func parseGoSum(file string) (map[string]string, error) {
commitShaRev := regexp.MustCompile(`v\d+\.\d+\.\d+-[\d+\.a-zA-Z]*?[0-9]{14}-(.*?)$`)
2020-07-20 10:52:58 +00:00
// Read go.mod
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
pkgs := make(map[string]string) // goPackagepath -> rev
for lineno, line := range bytes.Split(data, []byte("\n")) {
if len(line) == 0 {
continue
}
f := strings.Fields(string(line))
if len(f) != 3 {
return nil, fmt.Errorf("malformed go.sum:\n%s:%d: wrong number of fields %v", file, lineno, len(f))
}
rev := strings.TrimSuffix(strings.TrimSuffix(f[1], "/go.mod"), "+incompatible")
if commitShaRev.MatchString(rev) {
rev = commitShaRev.FindAllStringSubmatch(rev, -1)[0][1]
}
pkgs[f[0]] = rev
}
return pkgs, nil
}