gomod2nix/fetch/sum.go
adisbladis 19394d538e
Fix commitShaRev regex for some more complicated packages
Example "version" string this fixes: v0.5.0-alpha.5.0.20200819165624-17cef6e3e9d5
2020-10-29 13:26:16 +01:00

41 lines
860 B
Go

package fetch
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}-(.*?)$`)
// 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
}