versions: repair hash generation

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-01-10 18:07:46 +01:00
parent efd99975a4
commit c081664d03

View File

@ -53,7 +53,7 @@ func mustGetHash(url string) string {
fileHash := sha.Sum(nil) fileHash := sha.Sum(nil)
// Get upstream hash // Get upstream hash
req, err = http.NewRequestWithContext(context.Background(), http.MethodGet, url+".sha256", nil) req, err = http.NewRequestWithContext(context.Background(), http.MethodGet, url+".sha256", http.NoBody)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -94,58 +94,65 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
var componentListsCtr, componentCtr int
newFile := astutil.Apply(file, func(cursor *astutil.Cursor) bool { newFile := astutil.Apply(file, func(cursor *astutil.Cursor) bool {
n := cursor.Node() n := cursor.Node()
if x, ok := n.(*ast.CompositeLit); ok { //
ident, ok := x.Type.(*ast.Ident) // Find CompositeLit of type 'components.Components'
if !ok { //
return true comp, ok := n.(*ast.CompositeLit)
} if !ok {
if ident.Name == "ComponentVersions" { return true
for _, elt := range x.Elts { }
// component is one list element selExpr, ok := comp.Type.(*ast.SelectorExpr)
component := elt.(*ast.CompositeLit) if !ok {
return true
}
if selExpr.Sel.Name != "Components" {
return true
}
xIdent, ok := selExpr.X.(*ast.Ident)
if !ok {
return true
}
if xIdent.Name != "components" {
return true
}
componentListsCtr++
var url *ast.KeyValueExpr //
var hash *ast.KeyValueExpr // Iterate over the components
// Find the URL field //
for _, e := range component.Elts { for _, componentElt := range comp.Elts {
kv, ok := e.(*ast.KeyValueExpr) component := componentElt.(*ast.CompositeLit)
if !ok { componentCtr++
continue
}
ident, ok := kv.Key.(*ast.Ident)
if !ok {
continue
}
if ident.Name == "URL" {
url = kv
break
}
}
// Find the Hash field
for _, e := range component.Elts {
kv, ok := e.(*ast.KeyValueExpr)
if !ok {
continue
}
ident, ok := kv.Key.(*ast.Ident)
if !ok {
continue
}
if ident.Name == "Hash" {
hash = kv
break
}
}
// Generate the hash var url *ast.KeyValueExpr
fmt.Println("Generating hash for", url.Value.(*ast.BasicLit).Value) var hash *ast.KeyValueExpr
hash.Value.(*ast.BasicLit).Value = mustGetHash(url.Value.(*ast.BasicLit).Value)
for _, e := range component.Elts {
kv, ok := e.(*ast.KeyValueExpr)
if !ok {
continue
}
ident, ok := kv.Key.(*ast.Ident)
if !ok {
continue
}
switch ident.Name {
case "URL":
url = kv
case "Hash":
hash = kv
} }
} }
fmt.Println("Generating hash for", url.Value.(*ast.BasicLit).Value)
hash.Value.(*ast.BasicLit).Value = mustGetHash(url.Value.(*ast.BasicLit).Value)
} }
return true return true
}, nil, }, nil,
) )
@ -159,5 +166,9 @@ func main() {
if err := os.WriteFile(filePath, buf.Bytes(), 0o644); err != nil { if err := os.WriteFile(filePath, buf.Bytes(), 0o644); err != nil {
log.Fatalf("error writing file %s: %s", filePath, err) log.Fatalf("error writing file %s: %s", filePath, err)
} }
fmt.Println("Successfully generated hashes.") if componentCtr == 0 {
log.Fatalf("no components lists found")
}
fmt.Printf("Successfully generated hashes for %d components in %d component lists.", componentCtr, componentListsCtr)
} }