2021-02-06 16:26:23 +00:00
|
|
|
// Package storage defines interfaces to interact with the files persisted such as the list of servers.
|
2020-08-25 19:38:50 -04:00
|
|
|
package storage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-27 19:10:03 +00:00
|
|
|
type Storage struct {
|
|
|
|
|
mergedServers models.AllServers
|
|
|
|
|
hardcodedServers models.AllServers
|
2021-09-23 16:58:21 +00:00
|
|
|
logger InfoErrorer
|
2021-08-27 19:10:03 +00:00
|
|
|
filepath string
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-23 16:58:21 +00:00
|
|
|
type InfoErrorer interface {
|
|
|
|
|
Info(s string)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 19:10:03 +00:00
|
|
|
// 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.
|
2021-09-23 16:58:21 +00:00
|
|
|
func New(logger InfoErrorer, filepath string) (storage *Storage, err error) {
|
2021-08-27 19:10:03 +00:00
|
|
|
// error returned covered by unit test
|
|
|
|
|
harcodedServers, _ := parseHardcodedServers()
|
|
|
|
|
|
|
|
|
|
storage = &Storage{
|
|
|
|
|
hardcodedServers: harcodedServers,
|
|
|
|
|
logger: logger,
|
|
|
|
|
filepath: filepath,
|
|
|
|
|
}
|
2020-08-25 19:38:50 -04:00
|
|
|
|
2021-08-27 19:10:03 +00:00
|
|
|
if err := storage.SyncServers(); err != nil {
|
|
|
|
|
return nil, err
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
2021-08-27 19:10:03 +00:00
|
|
|
|
|
|
|
|
return storage, nil
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|