tree is now filetree based

This commit is contained in:
Bel LaPointe
2022-02-09 08:49:08 -07:00
parent fc263d6c2d
commit 9ad262e607
5 changed files with 122 additions and 51 deletions

View File

@@ -4,13 +4,17 @@ import (
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"
yaml "gopkg.in/yaml.v2"
)
var errChainNotFound = errors.New("pid chain not found")
type Tree struct {
path string
root string
}
type Branch struct {
@@ -25,9 +29,9 @@ type Pretty struct {
Title string
}
func NewTree(path string) *Tree {
func NewTree(root string) *Tree {
return &Tree{
path: path,
root: root,
}
}
@@ -74,53 +78,108 @@ func (tree *Tree) GetPretty() (map[string]*Pretty, error) {
}
func (tree *Tree) Get() (map[string]Branch, error) {
b, err := ioutil.ReadFile(tree.path)
m := map[string]Branch{}
err := filepath.Walk(tree.root, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if info.Name() != "meta.yaml" {
return nil
}
b, err := ioutil.ReadFile(p)
if err != nil {
return err
}
var branch Branch
if err := yaml.Unmarshal(b, &branch); err != nil {
return err
}
id := path.Base(path.Dir(p))
pidFullPath := path.Dir(path.Dir(p))
if pidFullPath != tree.root {
branch.PID = path.Base(pidFullPath)
} else {
branch.PID = ""
}
m[id] = branch
return nil
})
if os.IsNotExist(err) {
return map[string]Branch{}, nil
return m, nil
}
if err != nil {
return nil, err
}
var m map[string]Branch
err = yaml.Unmarshal(b, &m)
return m, err
}
func (tree *Tree) Set(m map[string]Branch) error {
b, err := yaml.Marshal(m)
if err != nil {
return err
}
return ioutil.WriteFile(tree.path, b, os.ModePerm)
}
func (tree *Tree) Del(id string) error {
func (tree *Tree) fullIdAndMeta(id string) (string, Branch, error) {
m, err := tree.Get()
if err != nil {
return err
return "", Branch{}, err
}
branch, ok := m[id]
if !ok {
return nil
return "", Branch{}, errChainNotFound
}
branch.Updated = time.Now().UTC()
branch.Deleted = true
m[id] = branch
return tree.Set(m)
p := id
for branch.PID != "" {
p = path.Join(branch.PID, p)
branch, ok = m[branch.PID]
if !ok {
return "", Branch{}, errChainNotFound
}
}
return path.Join(tree.root, p), branch, nil
}
func (tree *Tree) Put(id string, branch Branch) error {
branch.Updated = time.Now().UTC()
if branch.Title == "" {
branch.Title = "Untitled (" + time.Now().UTC().String() + ")"
}
m, err := tree.Get()
func (tree *Tree) putMeta(fullId string, branch Branch) error {
b, err := yaml.Marshal(branch)
if err != nil {
return err
}
if _, ok := m[branch.PID]; !ok && branch.PID != "" {
return errors.New("PID does not exist")
}
m[id] = branch
return tree.Set(m)
return ensureAndWrite(path.Join(fullId, "meta.yaml"), b)
}
func (tree *Tree) Del(id string) error {
fullId, meta, err := tree.fullIdAndMeta(id)
if err != nil {
if err == errChainNotFound {
return nil
}
return err
}
meta.Updated = time.Now().UTC()
meta.Deleted = true
return tree.putMeta(fullId, meta)
}
func (tree *Tree) Put(id string, branch Branch) error {
fullId, meta, err := tree.fullIdAndMeta(id)
if err == errChainNotFound {
if branch.PID != "" {
fullId, _, err = tree.fullIdAndMeta(branch.PID)
fullId = path.Join(fullId, id)
} else {
err = nil
fullId = path.Join(tree.root, fullId, id)
}
}
if err != nil {
return err
}
meta.Updated = time.Now().UTC()
if meta.Title == "" {
meta.Title = "Untitled (" + time.Now().UTC().String() + ")"
}
if branch.Title != "" {
meta.Title = branch.Title
}
meta.PID = branch.PID
meta.Deleted = branch.Deleted
return tree.putMeta(fullId, meta)
}