mirror of
https://github.com/Luzifer/go-latestver.git
synced 2024-12-20 18:41:17 +00:00
Knut Ahlers
09e0a66976
Squashed commit of the following: commit 2a83adf6c54d6abcf6762760fd38f2505511f545 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:34:49 2021 +0100 Lint: Fix copylocks errors Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 418f85d504203a6968329e280ecd9cf7d2365373 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:31:38 2021 +0100 Lint: Fix gosec warnings Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 1a977875740be3c40884aa0985578721ceb4ae37 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:28:02 2021 +0100 Lint: Disable gomnd for certain cases Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 5e81cf79ba7256b321442530715a2b53de0a18e1 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:26:01 2021 +0100 Lint: fix ineffassign errors Signed-off-by: Knut Ahlers <knut@ahlers.me> commit cb14fae2dad985368e1f05d62f8e778817d01c6f Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:23:42 2021 +0100 Lint: Fix revive linter errors Signed-off-by: Knut Ahlers <knut@ahlers.me> commit b3390b8dff9b939caa4e3821a48dd848af0bfba4 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:21:35 2021 +0100 Lint: Remove unrequired dereference Signed-off-by: Knut Ahlers <knut@ahlers.me> commit f9052e6aa530c5b5017249fc6c31bdbb94252760 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 03:20:43 2021 +0100 Lint: Remove deadcode Signed-off-by: Knut Ahlers <knut@ahlers.me> commit 72b88adaa25a3bb5a7af21da7ed12f08cae36573 Author: Knut Ahlers <knut@ahlers.me> Date: Wed Dec 1 02:52:27 2021 +0100 Add PR-testing Signed-off-by: Knut Ahlers <knut@ahlers.me> Signed-off-by: Knut Ahlers <knut@ahlers.me>
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/Luzifer/go-latestver/internal/database"
|
|
"github.com/Luzifer/go-latestver/internal/fetcher"
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
if err := f.Validate(ce.FetcherConfig); err != nil {
|
|
return errors.Wrapf(err, "catalog entry %d has invalid fetcher config", i)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|