2023-03-18 16:38:05 +00:00
|
|
|
// Package fetcher contains the implementations of fetchers to retrieve
|
|
|
|
// current versions for the catalog entries
|
2021-11-22 02:39:25 +00:00
|
|
|
package fetcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-11-22 16:31:05 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-11-22 02:39:25 +00:00
|
|
|
"github.com/Luzifer/go-latestver/internal/database"
|
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-03-18 16:38:05 +00:00
|
|
|
// Fetcher defines the interface all fetchers have to follow
|
2021-11-22 02:39:25 +00:00
|
|
|
Fetcher interface {
|
2023-03-18 16:38:05 +00:00
|
|
|
// FetchVersion retrieves the latest version for the catalog entry
|
2021-11-22 02:39:25 +00:00
|
|
|
FetchVersion(ctx context.Context, attrs *fieldcollection.FieldCollection) (string, time.Time, error)
|
2023-03-18 16:38:05 +00:00
|
|
|
// Links retrieves a collection of links for the fetcher
|
2021-11-22 02:39:25 +00:00
|
|
|
Links(attrs *fieldcollection.FieldCollection) []database.CatalogLink
|
2023-03-18 16:38:05 +00:00
|
|
|
// Validate validates the configuration given to the fetcher
|
2021-11-22 02:39:25 +00:00
|
|
|
Validate(attrs *fieldcollection.FieldCollection) error
|
|
|
|
}
|
2023-03-18 16:38:05 +00:00
|
|
|
// Create represents a function to instantiate a Fetcher
|
2021-12-01 02:38:52 +00:00
|
|
|
Create func() Fetcher
|
2021-11-22 02:39:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-03-18 16:38:05 +00:00
|
|
|
// ErrNoVersionFound signalizes the fetcher was not able to retrieve a version
|
2021-11-22 02:39:25 +00:00
|
|
|
ErrNoVersionFound = errors.New("no version found")
|
|
|
|
|
2021-12-01 02:38:52 +00:00
|
|
|
availableFetchers = map[string]Create{}
|
2021-11-22 02:39:25 +00:00
|
|
|
availableFetchersLock sync.RWMutex
|
|
|
|
)
|
|
|
|
|
2021-12-01 02:38:52 +00:00
|
|
|
func registerFetcher(name string, fn Create) {
|
2021-11-22 02:39:25 +00:00
|
|
|
availableFetchersLock.Lock()
|
|
|
|
defer availableFetchersLock.Unlock()
|
|
|
|
|
|
|
|
availableFetchers[name] = fn
|
|
|
|
}
|
|
|
|
|
2023-03-18 16:38:05 +00:00
|
|
|
// Get retrieves an creation function for the given fetcher name
|
2021-11-22 02:39:25 +00:00
|
|
|
func Get(name string) Fetcher {
|
|
|
|
availableFetchersLock.RLock()
|
|
|
|
defer availableFetchersLock.RUnlock()
|
|
|
|
|
|
|
|
fn, ok := availableFetchers[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn()
|
|
|
|
}
|