Maint: internal/storage rework

- No more global variables
- Inject merged servers to configuration package
- Fix #566: configuration parsing to use persisted servers.json
- Move server data files from `internal/constants` to `internal/storage`
This commit is contained in:
Quentin McGaw (desktop)
2021-08-27 19:10:03 +00:00
parent b1cfc03fc5
commit 3863cc439e
59 changed files with 850 additions and 490 deletions

View File

@@ -6,20 +6,29 @@ import (
"github.com/qdm12/golibs/logging"
)
type Storage interface {
// Passing an empty filepath disables writing to a file
SyncServers(hardcodedServers models.AllServers) (allServers models.AllServers, err error)
FlushToFile(servers models.AllServers) error
type Storage struct {
mergedServers models.AllServers
hardcodedServers models.AllServers
logger logging.Logger
filepath string
}
type storage struct {
logger logging.Logger
filepath string
}
// New creates a new storage and reads the servers from the
// embedded servers file and the file on disk.
// Passing an empty filepath disables writing servers to a file.
func New(logger logging.Logger, filepath string) (storage *Storage, err error) {
// error returned covered by unit test
harcodedServers, _ := parseHardcodedServers()
func New(logger logging.Logger, filepath string) Storage {
return &storage{
logger: logger,
filepath: filepath,
storage = &Storage{
hardcodedServers: harcodedServers,
logger: logger,
filepath: filepath,
}
if err := storage.SyncServers(); err != nil {
return nil, err
}
return storage, nil
}