notea-de-me/spike/review/reinvent/ezmded/server/tree.go

131 lines
2.5 KiB
Go

package main
import (
"io/ioutil"
"os"
"path"
yaml "gopkg.in/yaml.v2"
)
type Branch struct {
Leaf Leaf
Branches map[string]Branch
}
func (branch Branch) IsZero() bool {
return branch.Leaf == (Leaf{}) && len(branch.Branches) == 0
}
type Leaf struct {
Title string
Deleted bool
Content string
}
func (base Leaf) Merge(updated Leaf) Leaf {
if updated.Title != "" {
base.Title = updated.Title
}
base.Deleted = updated.Deleted
base.Content = updated.Content
return base
}
type Tree struct {
root string
cachedRoot Branch
}
func NewTree(root string) Tree {
return Tree{root: root}
}
func (tree Tree) WithRoot(root string) Tree {
tree.root = root
return tree
}
func (tree Tree) GetRoot() (Branch, error) {
if !tree.cachedRoot.IsZero() {
return tree.cachedRoot, nil
}
got, err := tree.getRoot()
if err == nil {
tree.cachedRoot = got
}
return got, err
}
func (tree Tree) getRoot() (Branch, error) {
m := Branch{Branches: map[string]Branch{}}
entries, err := os.ReadDir(tree.root)
if os.IsNotExist(err) {
return m, nil
}
if err != nil {
return Branch{}, err
}
for _, entry := range entries {
if entry.Name() == "data.yaml" {
if b, err := ioutil.ReadFile(path.Join(tree.root, entry.Name())); err != nil {
return Branch{}, err
} else if err := yaml.Unmarshal(b, &m.Leaf); err != nil {
return Branch{}, err
}
} else if entry.IsDir() {
if branch, err := tree.WithRoot(path.Join(tree.root, entry.Name())).getRoot(); err != nil {
return Branch{}, err
} else {
m.Branches[entry.Name()] = branch
}
}
}
return m, nil
}
func (tree Tree) toDir(id []string) string {
return path.Dir(tree.toData(id))
}
func (tree Tree) toData(id []string) string {
return path.Join(tree.root, path.Join(id...), "data.yaml")
}
func (tree Tree) Put(id []string, input Leaf) error {
if _, err := os.Stat(tree.toData(id)); os.IsNotExist(err) {
b, err := yaml.Marshal(Leaf{})
if err != nil {
return err
}
if err := ensureAndWrite(tree.toData(id), b); err != nil {
return err
}
}
old, err := tree.Get(id)
if err != nil {
return err
}
b, err := yaml.Marshal(old.Merge(input))
if err != nil {
return err
}
return ensureAndWrite(tree.toData(id), b)
}
func (tree Tree) Del(id []string) error {
os.RemoveAll(tree.toDir(id))
return nil
}
func (tree Tree) Get(id []string) (Leaf, error) {
f, err := os.Open(tree.toData(id))
if err != nil {
return Leaf{}, err
}
defer f.Close()
var got Leaf
err = yaml.NewDecoder(f).Decode(&got)
return got, err
}