populate id, add missing options

This commit is contained in:
Arnas Udovicius 2021-05-12 01:10:18 +03:00
parent 1710a3cf21
commit 129221c000
6 changed files with 204 additions and 4 deletions

3
.gitignore vendored
View file

@ -16,5 +16,6 @@
# vendor/ # vendor/
/tags /tags
/testdata/sunny1/
/.idea/ /.idea/
/testdata/sunny1/
/testdata/sunny2/

5
go.mod
View file

@ -2,4 +2,7 @@ module g.arns.lt/zordsdavini/zord-tree
go 1.16 go 1.16
require github.com/otiai10/copy v1.5.1 require (
g.arns.lt/zordsdavini/abcex v1.0.0
github.com/otiai10/copy v1.5.1
)

2
go.sum
View file

@ -1,3 +1,5 @@
g.arns.lt/zordsdavini/abcex v1.0.0 h1:qQqlZ4DMfethCGK4I6yGaLqMrTzKNIshqpINd1l3t0E=
g.arns.lt/zordsdavini/abcex v1.0.0/go.mod h1:YRcJgts3XZwI+LEkngpfUab3DkUAW387Irpr43hIym8=
github.com/otiai10/copy v1.5.1 h1:a/cs2E1/1V0az8K5nblbl+ymEa4E11AfaOLMar8V34w= github.com/otiai10/copy v1.5.1 h1:a/cs2E1/1V0az8K5nblbl+ymEa4E11AfaOLMar8V34w=
github.com/otiai10/copy v1.5.1/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E= github.com/otiai10/copy v1.5.1/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=

View file

@ -1,5 +1,6 @@
tags: t1 tags: t1
description: Če test 3 description: Če test 3
option2: it exists
--- ---

165
tree.go
View file

@ -2,10 +2,14 @@ package zord_tree
import ( import (
"bufio" "bufio"
"fmt"
"g.arns.lt/zordsdavini/abcex"
cp "github.com/otiai10/copy"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
) )
@ -28,7 +32,166 @@ func BuildTree(dirPath string, meta []string) (Tree, error) {
return readPath(dirPath, []string{}, meta) return readPath(dirPath, []string{}, meta)
} }
func readPath( dirPath string, category []string, meta []string) (Tree, error) { 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) {
tree := Tree{} tree := Tree{}
tree.path = dirPath tree.path = dirPath

View file

@ -68,3 +68,33 @@ func TestMeta(t *testing.T) {
t.Error("File 'file1.md' should have 'option1' with value 'test option'") t.Error("File 'file1.md' should have 'option1' with value 'test option'")
} }
} }
func TestId(t *testing.T) {
err := os.Remove("./testdata/sunny1")
if err != nil {
}
err = os.Remove("./testdata/sunny2")
if err != nil {
}
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
if err != nil {
t.Fatal("Couldn't prepare data for testing")
}
PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{})
}
func TestMissingOptions(t *testing.T) {
err := os.Remove("./testdata/sunny1")
if err != nil {
}
err = os.Remove("./testdata/sunny2")
if err != nil {
}
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
if err != nil {
t.Fatal("Couldn't prepare data for testing")
}
PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{"option1", "option2"})
}