Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
secdb: melange 0.2 removed templating for a different metaprogramming…
Browse files Browse the repository at this point in the history
… approach
  • Loading branch information
kaniini committed Nov 21, 2022
1 parent 16505bc commit 0889590
Showing 1 changed file with 1 addition and 67 deletions.
68 changes: 1 addition & 67 deletions pkg/types/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,12 @@
package types

import (
"bytes"
"fmt"
"os"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
"gopkg.in/yaml.v3"
)

const (
substitutionPackageName = "${{package.name}}"
substitutionPackageVersion = "${{package.version}}"
substitutionPackageEpoch = "${{package.epoch}}"
substitutionTargetsDestdir = "${{targets.destdir}}"
substitutionSubPkgDir = "${{targets.subpkgdir}}"
substitutionHostTripletGnu = "${{host.triplet.gnu}}"
substitutionHostTripletRust = "${{host.triplet.rust}}"
substitutionCrossTripletGnuGlibc = "${{cross.triplet.gnu.glibc}}"
substitutionCrossTripletGnuMusl = "${{cross.triplet.gnu.musl}}"
)

// TODO(kaniini): Harmonize these types with alpine's secdb implementation
// and place the implementation in alpine-go.
type Secfixes map[string][]string
Expand Down Expand Up @@ -82,51 +66,6 @@ func (mp MelangePackage) Entry() PackageEntry {
}
}

func substitutionReplacements() map[string]string {
return map[string]string{
substitutionPackageName: "MELANGE_TEMP_REPLACEMENT_PACAKAGE_NAME",
substitutionPackageVersion: "MELANGE_TEMP_REPLACEMENT_PACAKAGE_VERSION",
substitutionPackageEpoch: "MELANGE_TEMP_REPLACEMENT_PACAKAGE_EPOCH",
substitutionTargetsDestdir: "MELANGE_TEMP_REPLACEMENT_DESTDIR",
substitutionSubPkgDir: "MELANGE_TEMP_REPLACEMENT_SUBPKGDIR",
substitutionHostTripletGnu: "MELANGE_TEMP_REPLACEMENT_HOST_TRIPLET_GNU",
substitutionHostTripletRust: "MELANGE_TEMP_REPLACEMENT_HOST_TRIPLET_RUST",
substitutionCrossTripletGnuGlibc: "MELANGE_TEMP_REPLACEMENT_CROSS_TRIPLET_GNU_GLIBC",
substitutionCrossTripletGnuMusl: "MELANGE_TEMP_REPLACEMENT_CROSS_TRIPLET_GNU_MUSL",
}
}

func applyTemplate(contents []byte) ([]byte, error) {
// First, replace all protected pipeline templated vars temporarily
// So that we can apply the Go template
// We have to do this bc go templates doesn't support ignoring certain fields: https://github.com/golang/go/issues/31147

sr := substitutionReplacements()

protected := string(contents)
for k, v := range sr {
protected = strings.ReplaceAll(protected, k, v)
}

tmpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(protected)
if err != nil {
return nil, err
}
tmpl = tmpl.Option("missingkey=error")
buf := bytes.NewBuffer([]byte{})
if err := tmpl.Execute(buf, nil); err != nil {
return nil, err
}

// Add the pipeline templating back in
templateApplied := buf.String()
for k, v := range sr {
templateApplied = strings.ReplaceAll(templateApplied, v, k)
}

return []byte(templateApplied), nil
}

// LoadMelangePackage loads a Melange source package YAML file
// and extracts the Secfixes data from it.
func LoadMelangePackage(fileName string) (*MelangePackage, error) {
Expand All @@ -135,13 +74,8 @@ func LoadMelangePackage(fileName string) (*MelangePackage, error) {
return nil, err
}

tmplData, err := applyTemplate(fileData)
if err != nil {
return nil, err
}

pkg := &MelangePackage{}
if err := yaml.Unmarshal(tmplData, pkg); err != nil {
if err := yaml.Unmarshal(fileData, pkg); err != nil {
return nil, err
}

Expand Down

0 comments on commit 0889590

Please sign in to comment.