Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Nov 23, 2024
1 parent ff21a38 commit 129ab4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
22 changes: 7 additions & 15 deletions app/app_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

. "github.com/permafrost-dev/zeget/lib/assets"
"github.com/permafrost-dev/zeget/lib/detectors"
"github.com/permafrost-dev/zeget/lib/reporters"
"github.com/permafrost-dev/zeget/lib/utilities"
. "github.com/permafrost-dev/zeget/lib/utilities"
Expand Down Expand Up @@ -35,32 +34,25 @@ func (app *Application) Run() *ReturnStatus {
return NewReturnStatus(FatalError, nil, fmt.Sprintf("error: %v", err))
}

finder := app.getFinder()
findResult := app.getFindResult(finder)
finder, findResult := app.Find()

if result := app.ProcessFilters(&finder, &findResult); result != nil {
if result := app.ProcessFilters(finder, findResult); result != nil {
return result
}

app.cacheTarget(&finder, &findResult)
cacheItem = app.cacheTarget(finder, findResult)

if shouldReturn, returnStatus := app.shouldReturn(findResult.Error); shouldReturn {
return returnStatus
}

assetWrapper := NewAssetWrapper(findResult.Assets)
detector, err := detectors.DetermineCorrectDetector(&app.Opts, app.Config.Global.IgnorePatterns, nil)
detected, err := app.DetectAssets(assetWrapper)
if err != nil {
return NewReturnStatus(FatalError, err, fmt.Sprintf("error: %v", err))
}

// get the url and candidates from the detector
detected, err := detector.Detect(assetWrapper.Assets)
if err != nil {
return NewReturnStatus(FatalError, err, fmt.Sprintf("error: %v", err))
}

if result := app.FilterDetectedAssets(&detected, &findResult); result != nil {
if result := app.FilterDetectedAssets(detected, findResult); result != nil {
return result
}

Expand All @@ -73,12 +65,12 @@ func (app *Application) Run() *ReturnStatus {
}
}

body, result := app.DownloadAndVerify(assetWrapper, &findResult)
body, result := app.DownloadAndVerify(assetWrapper, findResult)
if result != nil {
return result
}

extractedCount, result := app.ExtractDownloadedAsset(assetWrapper, body, &finder)
extractedCount, result := app.ExtractDownloadedAsset(assetWrapper, body, finder)
if result != nil {
return result
}
Expand Down
28 changes: 26 additions & 2 deletions app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,23 @@ func (app *Application) getFindResult(finder finders.ValidFinder) finders.FindRe
return *finder.Finder.Find(app.DownloadClient())
}

func (app *Application) cacheTarget(finding *finders.ValidFinder, findResult *finders.FindResult) {
app.Cache.AddRepository(
func (app *Application) Find() (*finders.ValidFinder, *finders.FindResult) {
finder := app.getFinder()
findResult := app.getFindResult(finder)

return &finder, &findResult
}

func (app *Application) cacheTarget(finding *finders.ValidFinder, findResult *finders.FindResult) *data.RepositoryCacheEntry {
item, _ := app.Cache.AddRepository(
app.Target,
finding.Tool,
app.Opts.Asset,
findResult,
time.Now().Add(time.Hour*24*7),
)

return item
}

func (app *Application) targetToProject(target string) error {
Expand Down Expand Up @@ -717,3 +726,18 @@ func (app *Application) FilterDetectedAssets(detected *detectors.DetectionResult

return nil
}

func (app *Application) DetectAssets(assetWrapper *AssetWrapper) (*detectors.DetectionResult, error) {
detector, err := detectors.DetermineCorrectDetector(&app.Opts, app.Config.Global.IgnorePatterns, nil)
if err != nil {
return nil, err
}

// get the url and candidates from the detector
detected, err := detector.Detect(assetWrapper.Assets)
if err != nil {
return nil, err
}

return &detected, nil
}

0 comments on commit 129ab4d

Please sign in to comment.