Skip to content

Commit

Permalink
implement global config ignore_patterns regex ignoreable patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed May 14, 2024
1 parent 8d9e415 commit 2fe6bfd
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 26 deletions.
21 changes: 19 additions & 2 deletions app/app_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (app *Application) Run() *ReturnStatus {
}

assetWrapper := NewAssetWrapper(findResult.Assets)
detector, err := detectors.DetermineCorrectDetector(&app.Opts, nil)
detector, err := detectors.DetermineCorrectDetector(&app.Opts, app.Config.Global.IgnorePatterns, nil)
if err != nil {
return NewReturnStatus(FatalError, err, fmt.Sprintf("error: %v", err))
}
Expand All @@ -74,6 +74,24 @@ func (app *Application) Run() *ReturnStatus {
return NewReturnStatus(FatalError, err, fmt.Sprintf("error: %v", err))
}

filterDetector, _ := detectors.GetPatternDetectors(app.Config.Global.IgnorePatterns, nil)
filteredDetected, err := filterDetector.DetectWithoutSystem(findResult.Assets)
if err != nil {
return NewReturnStatus(FatalError, err, fmt.Sprintf("error: %v", err))
}

if filteredDetected != nil {
//remove filteredDetected.Candidates from detected.Candidates
detected.Candidates = FilterArr(detected.Candidates, func(a assets.Asset) bool {
return IsInArr(filteredDetected.Candidates, a, func(a1 assets.Asset, a2 assets.Asset) bool { return a1.Name == a2.Name })
})

if len(detected.Candidates) == 1 {
detected.Asset = detected.Candidates[0]
detected.Candidates = []assets.Asset{}
}
}

asset := detected.Asset

if len(detected.Candidates) != 0 {
Expand Down Expand Up @@ -103,7 +121,6 @@ func (app *Application) Run() *ReturnStatus {

tagDownloaded := utilities.SetIf(app.Opts.Tag != "", "latest", app.Opts.Tag)
app.Cache.Data.GetRepositoryEntryByKey(app.Target, &app.Cache).UpdateDownloadedAt(tagDownloaded)
// app.Cache.Data.GetRepositoryEntryByKey(app.Target, &app.Cache).UpdateReleaseDate(asset.ReleaseDate)

extractor, err := app.getExtractor(assetWrapper.Asset, finder.Tool)
if err != nil {
Expand Down
39 changes: 21 additions & 18 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import (
)

type ConfigGlobal struct {
All bool `toml:"all"`
DownloadOnly bool `toml:"download_only"`
File string `toml:"file"`
GithubToken string `toml:"github_token"`
Quiet bool `toml:"quiet"`
ShowHash bool `toml:"show_hash"`
Source bool `toml:"download_source"`
System string `toml:"system"`
Target string `toml:"target"`
UpgradeOnly bool `toml:"upgrade_only"`
RemoveExisting bool `toml:"remove_existing"`
All bool `toml:"all"`
DownloadOnly bool `toml:"download_only"`
File string `toml:"file"`
GithubToken string `toml:"github_token"`
Quiet bool `toml:"quiet"`
ShowHash bool `toml:"show_hash"`
Source bool `toml:"download_source"`
System string `toml:"system"`
Target string `toml:"target"`
UpgradeOnly bool `toml:"upgrade_only"`
RemoveExisting bool `toml:"remove_existing"`
IgnorePatterns []string `toml:"ignore_patterns"`
}

type ConfigRepository struct {
Expand Down Expand Up @@ -167,13 +168,14 @@ func (app *Application) initializeConfig() {
if err != nil {
app.Config = &Config{
Global: ConfigGlobal{
All: false,
DownloadOnly: false,
GithubToken: "",
Quiet: false,
ShowHash: false,
Source: false,
UpgradeOnly: false,
All: false,
DownloadOnly: false,
GithubToken: "",
Quiet: false,
ShowHash: false,
Source: false,
UpgradeOnly: false,
IgnorePatterns: []string{},
},
Repositories: make(map[string]ConfigRepository, 0),
}
Expand All @@ -192,6 +194,7 @@ func (app *Application) initializeConfig() {
config.Global.UpgradeOnly = utilities.SetIf(!config.Meta.MetaData.IsDefined("global", "upgrade_only"), config.Global.UpgradeOnly, false)
config.Global.Target = utilities.SetIf(!config.Meta.MetaData.IsDefined("global", "target"), config.Global.Target, utilities.GetCurrentDirectory())
config.Global.RemoveExisting = utilities.SetIf(!config.Meta.MetaData.IsDefined("global", "remove_existing"), config.Global.RemoveExisting, false)
config.Global.IgnorePatterns = utilities.SetIf(!config.Meta.MetaData.IsDefined("global", "ignore_patterns"), config.Global.IgnorePatterns, []string{})

// ensure "~" in the target directory is expanded
config.Global.Target, _ = home.Expand(config.Global.Target)
Expand Down
32 changes: 31 additions & 1 deletion lib/detectors/detect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package detectors

import (
"regexp"
"runtime"
"strings"

Expand All @@ -20,7 +21,7 @@ type Detector interface {
// AllDetector, which will just return all assets. Otherwise we use the
// --system pair provided by the user, or the runtime.GOOS/runtime.GOARCH
// pair by default (the host system OS/Arch pair).
func DetermineCorrectDetector(opts *appflags.Flags, system *SystemDetector) (detector Detector, err error) {
func DetermineCorrectDetector(opts *appflags.Flags, ignoredPatterns []string, system *SystemDetector) (detector Detector, err error) {
if system == nil {
system, _ = NewSystemDetector(runtime.GOOS, runtime.GOARCH)
}
Expand Down Expand Up @@ -55,6 +56,35 @@ func DetermineCorrectDetector(opts *appflags.Flags, system *SystemDetector) (det
}
}

for _, p := range ignoredPatterns {
detectors = append(detectors, &SingleAssetDetector{
Asset: p,
Anti: true,
IsPattern: true,
Compiled: regexp.MustCompile(p),
})
}

detector = &DetectorChain{
Detectors: detectors,
System: system,
}

return detector, err
}

func GetPatternDetectors(ignoredPatterns []string, system *SystemDetector) (detector *DetectorChain, err error) {
detectors := make([]Detector, 0)

for _, p := range ignoredPatterns {
detectors = append(detectors, &SingleAssetDetector{
Asset: p,
Anti: true,
IsPattern: true,
Compiled: regexp.MustCompile(p),
})
}

detector = &DetectorChain{
Detectors: detectors,
System: system,
Expand Down
2 changes: 1 addition & 1 deletion lib/detectors/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestDetermineCorrectDetector(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
system, _ := NewSystemDetector(runtime.GOOS, runtime.GOARCH)
got, err := DetermineCorrectDetector(&tt.flags, system)
got, err := DetermineCorrectDetector(&tt.flags, []string{}, system)
if (err != nil) != tt.wantErr {
t.Errorf("DetermineCorrectDetector() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
28 changes: 28 additions & 0 deletions lib/detectors/detector_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,32 @@ type DetectorChain struct {
System Detector
}

func (dc *DetectorChain) DetectWithoutSystem(assets []Asset) (*DetectionResult, error) {
for _, d := range dc.Detectors {
if d == nil {
continue
}

detected, err := d.Detect(assets)
if len(detected.Candidates) == 0 && err != nil {
return nil, err
}
if len(detected.Candidates) == 0 {
return &detected, nil
}
assets = detected.Candidates
}

ptr := NewDetectionResult(nil, assets)
return &ptr, nil
}

func (dc *DetectorChain) Detect(assets []Asset) (DetectionResult, error) {
for _, d := range dc.Detectors {
if d == nil {
continue
}

detected, err := d.Detect(assets)
if len(detected.Candidates) == 0 && err != nil {
return DetectionResult{}, err
Expand All @@ -23,6 +47,10 @@ func (dc *DetectorChain) Detect(assets []Asset) (DetectionResult, error) {
assets = detected.Candidates
}

if dc.System == nil {
return DetectionResult{}, nil
}

detected, err := dc.System.Detect(assets)
if len(detected.Candidates) == 0 && err != nil {
return DetectionResult{}, err
Expand Down
20 changes: 16 additions & 4 deletions lib/detectors/single_asset_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package detectors
import (
"fmt"
"path"
"regexp"
"strings"

. "github.com/permafrost-dev/zeget/lib/assets"
Expand All @@ -11,23 +12,34 @@ import (
// SingleAssetDetector finds a single named asset. If Anti is true it finds all
// assets that don't contain Asset.
type SingleAssetDetector struct {
Asset string
Anti bool
Asset string
Anti bool
IsPattern bool
Compiled *regexp.Regexp
}

func (s *SingleAssetDetector) Detect(assets []Asset) (DetectionResult, error) {
var candidates []Asset

for _, a := range assets {

// handle regex patterns
if s.Anti && s.IsPattern {
if !s.Compiled.MatchString(a.Name) && !s.Compiled.MatchString(path.Base(a.Name)) {
candidates = append(candidates, a)
}
}

if !s.Anti && path.Base(a.Name) == s.Asset {
return NewDetectionResult(&a, nil), nil
}
if !s.Anti && strings.Contains(path.Base(a.Name), s.Asset) {
candidates = append(candidates, a)
}
if s.Anti && path.Base(a.Name) != s.Asset && len(assets) == 2 {
if !s.IsPattern && s.Anti && path.Base(a.Name) != s.Asset && len(assets) == 2 {
return NewDetectionResult(&a, nil), nil
}
if s.Anti && !strings.Contains(path.Base(a.Name), s.Asset) {
if !s.IsPattern && s.Anti && !strings.Contains(path.Base(a.Name), s.Asset) {
candidates = append(candidates, a)
}
}
Expand Down
19 changes: 19 additions & 0 deletions lib/utilities/arrays.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utilities

func FilterArr[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}

func IsInArr[T any](arr []T, val T, test func(T, T) bool) bool {
for _, a := range arr {
if test(a, val) {
return true
}
}
return false
}

0 comments on commit 2fe6bfd

Please sign in to comment.