1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-09 09:35:03 +09:00
any-sync/nodeconf/nodeconfstore/nodeconfstore.go
Sergey Cherepanov 745f3d56fb
nodeconf store
2023-04-12 14:50:35 +02:00

62 lines
1.2 KiB
Go

package nodeconfstore
import (
"context"
"github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/nodeconf"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"sync"
)
func New() NodeConfStore {
return new(nodeConfStore)
}
type NodeConfStore interface {
app.Component
nodeconf.Store
}
type nodeConfStore struct {
path string
mu sync.Mutex
}
type configGetter interface {
NodeConfStorePath() string
}
func (n *nodeConfStore) Init(a *app.App) (err error) {
n.path = a.MustComponent("config").(configGetter).NodeConfStorePath()
return
}
func (n *nodeConfStore) Name() (name string) {
return nodeconf.CNameStore
}
func (n *nodeConfStore) GetLast(ctx context.Context, netId string) (c nodeconf.Configuration, err error) {
n.mu.Lock()
defer n.mu.Unlock()
path := filepath.Join(n.path, netId+".yml")
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
err = nodeconf.ErrConfigurationNotFound
return
}
err = yaml.Unmarshal(data, &c)
return
}
func (n *nodeConfStore) SaveLast(ctx context.Context, c nodeconf.Configuration) (err error) {
n.mu.Lock()
defer n.mu.Unlock()
path := filepath.Join(n.path, c.NetworkId+".yml")
data, err := yaml.Marshal(c)
if err != nil {
return
}
return os.WriteFile(path, data, 0755)
}