2021-11-22 02:39:25 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v3"
|
2021-11-22 16:31:05 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/go-latestver/internal/database"
|
|
|
|
"github.com/Luzifer/go-latestver/internal/fetcher"
|
2021-11-22 02:39:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrCatalogEntryNotFound = errors.New("catalog entry not found")
|
|
|
|
|
|
|
|
type (
|
|
|
|
File struct {
|
|
|
|
Catalog []database.CatalogEntry `yaml:"catalog"`
|
|
|
|
CheckInterval time.Duration `yaml:"check_interval"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func New() *File {
|
|
|
|
return &File{
|
|
|
|
CheckInterval: time.Hour,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) CatalogEntryByTag(name, tag string) (database.CatalogEntry, error) {
|
|
|
|
for i := range f.Catalog {
|
|
|
|
ce := f.Catalog[i]
|
|
|
|
if ce.Name == name && ce.Tag == tag {
|
|
|
|
return ce, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return database.CatalogEntry{}, ErrCatalogEntryNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Load(filepath string) error {
|
|
|
|
fh, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "opening config file")
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
|
|
|
dec := yaml.NewDecoder(fh)
|
|
|
|
dec.KnownFields(true)
|
|
|
|
|
|
|
|
return errors.Wrap(dec.Decode(f), "decoding config")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) ValidateCatalog() error {
|
|
|
|
for i, ce := range f.Catalog {
|
|
|
|
f := fetcher.Get(ce.Fetcher)
|
|
|
|
if f == nil {
|
|
|
|
return errors.Errorf("catalog entry %d has unknown fetcher", i)
|
|
|
|
}
|
|
|
|
|
2021-12-01 02:38:52 +00:00
|
|
|
if err := f.Validate(ce.FetcherConfig); err != nil {
|
2021-11-22 02:39:25 +00:00
|
|
|
return errors.Wrapf(err, "catalog entry %d has invalid fetcher config", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|