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,22 +94,44 @@ 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'
//
comp, ok := n.(*ast.CompositeLit)
if !ok { if !ok {
return true return true
} }
if ident.Name == "ComponentVersions" { selExpr, ok := comp.Type.(*ast.SelectorExpr)
for _, elt := range x.Elts { if !ok {
// component is one list element return true
component := elt.(*ast.CompositeLit) }
if selExpr.Sel.Name != "Components" {
return true
}
xIdent, ok := selExpr.X.(*ast.Ident)
if !ok {
return true
}
if xIdent.Name != "components" {
return true
}
componentListsCtr++
//
// Iterate over the components
//
for _, componentElt := range comp.Elts {
component := componentElt.(*ast.CompositeLit)
componentCtr++
var url *ast.KeyValueExpr var url *ast.KeyValueExpr
var hash *ast.KeyValueExpr var hash *ast.KeyValueExpr
// Find the URL field
for _, e := range component.Elts { for _, e := range component.Elts {
kv, ok := e.(*ast.KeyValueExpr) kv, ok := e.(*ast.KeyValueExpr)
if !ok { if !ok {
@ -119,33 +141,18 @@ func main() {
if !ok { if !ok {
continue continue
} }
if ident.Name == "URL" { switch ident.Name {
case "URL":
url = kv url = kv
break case "Hash":
}
}
// 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 hash = kv
break
} }
} }
// Generate the hash
fmt.Println("Generating hash for", url.Value.(*ast.BasicLit).Value) fmt.Println("Generating hash for", url.Value.(*ast.BasicLit).Value)
hash.Value.(*ast.BasicLit).Value = mustGetHash(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)
} }