2021-05-04 20:26:41 +00:00
|
|
|
package zord_tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-05-11 22:10:18 +00:00
|
|
|
"fmt"
|
|
|
|
"g.arns.lt/zordsdavini/abcex"
|
|
|
|
cp "github.com/otiai10/copy"
|
2021-05-04 20:26:41 +00:00
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-05-11 22:10:18 +00:00
|
|
|
"path/filepath"
|
2021-05-04 20:26:41 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
fullPath string
|
|
|
|
category []string
|
|
|
|
tags []string
|
|
|
|
meta map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tree struct {
|
|
|
|
path string
|
|
|
|
dirs []Tree
|
|
|
|
files []File
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:17:07 +00:00
|
|
|
func BuildTree(dirPath string, meta []string) (Tree, error) {
|
|
|
|
return readPath(dirPath, []string{}, meta)
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 22:10:18 +00:00
|
|
|
func PopulateTree(sourcePath string, destPath string, meta []string) error {
|
|
|
|
id, err := getMaxId(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id++
|
|
|
|
err = addMissingId(sourcePath, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = addMissingMeta(sourcePath, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cp.Copy(sourcePath, destPath)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMissingMeta(dir string, meta []string) error {
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
check := map[string]bool{}
|
|
|
|
for _, option := range meta {
|
|
|
|
check[option] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
osf, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
for _, option := range meta {
|
|
|
|
if strings.HasPrefix(line, option+":") {
|
|
|
|
check[option] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if line == "---" {
|
|
|
|
for option, process := range check {
|
|
|
|
if !process {
|
|
|
|
err = addMeta(path, option, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMissingId(dir string, id int64) error {
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
osf, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "id:") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if line == "---" {
|
|
|
|
err = addMeta(path, "id", abcex.Encode(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMeta(path string, option string, value string) error {
|
|
|
|
b, err := ioutil.ReadFile(path) // just pass the file name
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
str := string(b)
|
|
|
|
str = strings.Replace(str, "\n\n---\n", fmt.Sprintf("\n%s: %s\n\n---\n", option, value), 1)
|
|
|
|
err = ioutil.WriteFile(path, []byte(str), 0644)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMaxId(dir string) (int64, error) {
|
|
|
|
var max int64 = 0
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
osf, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == "---" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "id:") {
|
|
|
|
line = strings.TrimPrefix(line, "id:")
|
|
|
|
i := abcex.Decode(strings.Trim(line, " "))
|
|
|
|
if i > max {
|
|
|
|
max = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return max, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return max, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPath(dirPath string, category []string, meta []string) (Tree, error) {
|
2021-05-04 20:26:41 +00:00
|
|
|
tree := Tree{}
|
|
|
|
|
|
|
|
tree.path = dirPath
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
fullPath := path.Join(dirPath, file.Name())
|
|
|
|
if file.IsDir() {
|
2021-05-09 18:17:07 +00:00
|
|
|
nextDir, err := readPath(fullPath, append(category, file.Name()), meta)
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
|
|
|
tree.dirs = append(tree.dirs, nextDir)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ioutil.ReadFile(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
2021-05-09 18:17:07 +00:00
|
|
|
nextFile, err := readFile(file, fullPath, category, meta)
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
|
|
|
tree.files = append(tree.files, nextFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
2021-05-09 18:17:07 +00:00
|
|
|
func readFile(file fs.FileInfo, fullPath string, category []string, meta []string) (File, error) {
|
2021-05-04 20:26:41 +00:00
|
|
|
f := File{
|
|
|
|
name: file.Name(),
|
|
|
|
fullPath: fullPath,
|
|
|
|
category: category,
|
2021-05-09 18:17:07 +00:00
|
|
|
meta: map[string]string{},
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
osf, err := os.Open(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return File{}, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == "---" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "tags:") {
|
|
|
|
line = strings.TrimPrefix(line, "tags:")
|
|
|
|
t := strings.Split(line, ",")
|
|
|
|
tags := []string{}
|
|
|
|
for _, tag := range t {
|
|
|
|
tags = append(tags, strings.Trim(tag, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
f.tags = tags
|
|
|
|
}
|
2021-05-09 18:17:07 +00:00
|
|
|
for _, option := range meta {
|
|
|
|
if strings.HasPrefix(line, option) {
|
|
|
|
line = strings.TrimPrefix(line, option + ":")
|
|
|
|
f.meta[option] = strings.Trim(line, " ")
|
|
|
|
}
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
osf.Close()
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|