mirror of
https://github.com/Luzifer/go-latestver.git
synced 2024-11-13 01:12:41 +00:00
47 lines
968 B
Go
47 lines
968 B
Go
|
package fetcher
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"github.com/Luzifer/go-latestver/internal/database"
|
||
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
Fetcher interface {
|
||
|
FetchVersion(ctx context.Context, attrs *fieldcollection.FieldCollection) (string, time.Time, error)
|
||
|
Links(attrs *fieldcollection.FieldCollection) []database.CatalogLink
|
||
|
Validate(attrs *fieldcollection.FieldCollection) error
|
||
|
}
|
||
|
FetcherCreate func() Fetcher
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrNoVersionFound = errors.New("no version found")
|
||
|
|
||
|
availableFetchers = map[string]FetcherCreate{}
|
||
|
availableFetchersLock sync.RWMutex
|
||
|
)
|
||
|
|
||
|
func registerFetcher(name string, fn FetcherCreate) {
|
||
|
availableFetchersLock.Lock()
|
||
|
defer availableFetchersLock.Unlock()
|
||
|
|
||
|
availableFetchers[name] = fn
|
||
|
}
|
||
|
|
||
|
func Get(name string) Fetcher {
|
||
|
availableFetchersLock.RLock()
|
||
|
defer availableFetchersLock.RUnlock()
|
||
|
|
||
|
fn, ok := availableFetchers[name]
|
||
|
if !ok {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return fn()
|
||
|
}
|