Skip to content

Commit

Permalink
fixed erroneous matches in tarballs
Browse files Browse the repository at this point in the history
Matcher was searching for files ending with binary listed in distributions

thus, `_rg` would match `rg` specification, and since the former appears after the latter in the tarball, `_ rg` would be installed as the binary (but the file contains the completion).

To solve this, added support for regexps in distribution binaries specification.

Fixes #239
  • Loading branch information
leucos committed Sep 26, 2023
1 parent 07a3a7d commit d134901
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -696,8 +696,19 @@ sources:
`binaries_config`:

```yaml
# Array of binaries names that will be downloaded
- <string>
# Array of binaries names that will be installed.
# The string provided is treated as a regexp.
# This regexp is compared to the filenames found in packages.
# Note that filenames contains their path in the package with the top level
# directory removed, e.g.:
# software-13.0.0-x86_64-unknown-linux-musl/foo/bar/zebinary
# becomes
# foo/bar/zebinary
# Also note that, since all binaries will be installed as the distribution
# entry name, only one (the latest match) will survive for now.
# The list is just here to allow alternate names, not real multiple binaries
# installation.
- <regexp>
```

`supported_platforms`:
Expand Down
2 changes: 1 addition & 1 deletion distributions/distributions.yaml
Expand Up @@ -3248,7 +3248,7 @@ sources:
install:
type: tgz
binaries:
- rg
- "^rg$" # ripgrep binary is named rg

ruflood:
description: Helping Ukraine by flooding russian state-controlled media/bureau/bank websites.
Expand Down
2 changes: 1 addition & 1 deletion internal/app/definitions.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/devops-works/binenv/internal/platform"
)

// Distributions holds the liste of available software sources
// Distributions holds the list of available software sources
type Distributions struct {
Sources map[string]Sources `yaml:"sources"`
}
Expand Down
27 changes: 23 additions & 4 deletions internal/tpl/tpl.go
Expand Up @@ -2,6 +2,7 @@ package tpl

import (
"bytes"
"regexp"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -62,13 +63,21 @@ func (a Args) Interpolate(m map[string]string) {
}

// MatchFilters matches a file against a list of template filters
//
// We use a template to allow interpolation in binaries list (e.g. {{.OS}}-{{.Arch}}-{{.Version}})
// Also, the top level directory, if present, is removed to allow an easier search
// For instance :
// ripgrep-13.0.0-x86_64-unknown-linux-musl/rg
// becomes
// ^rg$
func (a Args) MatchFilters(file string, filters []string) (bool, error) {
var once sync.Once
var (
once sync.Once
onceErr error
)

tpls := []*template.Template{}

var onceErr error

onceBody := func() {
for _, v := range filters {
tpl, err := template.New("matcher").Parse(v)
Expand All @@ -85,6 +94,11 @@ func (a Args) MatchFilters(file string, filters []string) (bool, error) {
return false, onceErr
}

// Remove first directory if present
if strings.Contains(file, "/") {
file = strings.Join(strings.Split(file, "/")[1:], "/")
}

for _, t := range tpls {
buf := bytes.Buffer{}
err := t.Execute(&buf, a)
Expand All @@ -93,7 +107,12 @@ func (a Args) MatchFilters(file string, filters []string) (bool, error) {
}
// fmt.Printf("trying to match %s against %s\n", file, buf.String())

if strings.HasSuffix(file, buf.String()) {
patt, err := regexp.Compile(buf.String())
if err != nil {
return false, err
}

if patt.MatchString(file) {
// fmt.Printf("file %s matches filters\n", file)
return true, nil
}
Expand Down

0 comments on commit d134901

Please sign in to comment.