store: use constants for directory names

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-12-07 11:42:49 +01:00
parent 1e72e32ec3
commit 36b5cd18e8
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7

View File

@ -12,11 +12,16 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const (
instanceDir = "instances"
defaultsDir = "defaults"
)
func New(root string) (*Store, error) { func New(root string) (*Store, error) {
if err := os.MkdirAll(filepath.Join(root, "instances"), 0700); err != nil { if err := os.MkdirAll(filepath.Join(root, instanceDir), 0700); err != nil {
return nil, err return nil, err
} }
if err := os.MkdirAll(filepath.Join(root, "defaults"), 0700); err != nil { if err := os.MkdirAll(filepath.Join(root, defaultsDir), 0700); err != nil {
return nil, err return nil, err
} }
return &Store{root: root}, nil return &Store{root: root}, nil
@ -43,7 +48,7 @@ type Txn struct {
} }
func (t *Txn) List() ([]*NodeGroup, error) { func (t *Txn) List() ([]*NodeGroup, error) {
pp := filepath.Join(t.s.root, "instances") pp := filepath.Join(t.s.root, instanceDir)
fis, err := os.ReadDir(pp) fis, err := os.ReadDir(pp)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,7 +78,7 @@ func (t *Txn) NodeGroupByName(name string) (*NodeGroup, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
dt, err := os.ReadFile(filepath.Join(t.s.root, "instances", name)) dt, err := os.ReadFile(filepath.Join(t.s.root, instanceDir, name))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -93,7 +98,7 @@ func (t *Txn) Save(ng *NodeGroup) error {
if err != nil { if err != nil {
return err return err
} }
return ioutils.AtomicWriteFile(filepath.Join(t.s.root, "instances", name), dt, 0600) return ioutils.AtomicWriteFile(filepath.Join(t.s.root, instanceDir, name), dt, 0600)
} }
func (t *Txn) Remove(name string) error { func (t *Txn) Remove(name string) error {
@ -101,7 +106,7 @@ func (t *Txn) Remove(name string) error {
if err != nil { if err != nil {
return err return err
} }
return os.RemoveAll(filepath.Join(t.s.root, "instances", name)) return os.RemoveAll(filepath.Join(t.s.root, instanceDir, name))
} }
func (t *Txn) SetCurrent(key, name string, global, def bool) error { func (t *Txn) SetCurrent(key, name string, global, def bool) error {
@ -121,11 +126,11 @@ func (t *Txn) SetCurrent(key, name string, global, def bool) error {
h := toHash(key) h := toHash(key)
if def { if def {
if err := ioutils.AtomicWriteFile(filepath.Join(t.s.root, "defaults", h), []byte(name), 0600); err != nil { if err := ioutils.AtomicWriteFile(filepath.Join(t.s.root, defaultsDir, h), []byte(name), 0600); err != nil {
return err return err
} }
} else { } else {
os.RemoveAll(filepath.Join(t.s.root, "defaults", h)) // ignore error os.RemoveAll(filepath.Join(t.s.root, defaultsDir, h)) // ignore error
} }
return nil return nil
} }
@ -173,7 +178,7 @@ func (t *Txn) Current(key string) (*NodeGroup, error) {
h := toHash(key) h := toHash(key)
dt, err = os.ReadFile(filepath.Join(t.s.root, "defaults", h)) dt, err = os.ReadFile(filepath.Join(t.s.root, defaultsDir, h))
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
t.reset(key) t.reset(key)