-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_mismatch.go
49 lines (42 loc) · 1.22 KB
/
hash_mismatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"errors"
"regexp"
"strings"
)
type HashMismatchResult struct {
Specified, Got string
}
var (
gotRe = regexp.MustCompile("^.*got:\\s*(.*)\\s*$")
specifiedRe = regexp.MustCompile("^.*specified:\\s*(.*)\\s*$")
)
func FindHashMismatch(buildOutput string) (HashMismatchResult, error) {
lines := strings.Split(buildOutput, "\n")
if len(lines) < 3 {
return HashMismatchResult{}, errors.New("output too small")
}
mismatchLine := -1
for i, line := range lines {
if strings.Contains(line, "hash mismatch in fixed-output derivation") {
mismatchLine = i
break
}
}
if mismatchLine == -1 {
return HashMismatchResult{}, errors.New("no hash mismatch message found")
}
gotLine := lines[mismatchLine+2]
specifiedLine := lines[mismatchLine+1]
gotMatches := gotRe.FindStringSubmatch(gotLine)
if len(gotMatches) != 2 {
return HashMismatchResult{}, errors.New("hash mismatch parse error")
}
gotHash := gotMatches[1]
specifiedMatches := specifiedRe.FindStringSubmatch(specifiedLine)
if len(specifiedMatches) != 2 {
return HashMismatchResult{}, errors.New("hash mismatch parse error")
}
specifiedHash := specifiedMatches[1]
return HashMismatchResult{Got: gotHash, Specified: specifiedHash}, nil
}