This commit is contained in:
Arnas Udovicius 2022-05-08 23:19:55 +03:00
parent fdbbc02d0a
commit cbee75e3c3
2 changed files with 20 additions and 20 deletions

34
tree.go
View file

@ -33,12 +33,13 @@ func BuildTree(dirPath string, meta []string) (Tree, error) {
}
func PopulateTree(sourcePath string, destPath string, meta []string) error {
err = fixFormat(sourcePath)
err := fixFormat(sourcePath)
if err != nil {
return err
}
id, err := getMaxId(sourcePath)
var id int64 = 0
id, err = getMaxId(sourcePath)
if err != nil {
return err
}
@ -68,7 +69,7 @@ func fixFormat(dir string) error {
if info.Mode().IsRegular() {
b, err := ioutil.ReadFile(path) // just pass the file name
if err != nil {
return err
return err
}
osf, err := os.Open(path)
@ -87,9 +88,9 @@ func fixFormat(dir string) error {
format = false
}
if (format) {
if format {
line = strings.Trim(line, " ")
if (line != "") {
if line != "" {
content = content + "\n" + line
}
} else {
@ -151,7 +152,7 @@ func addMissingMeta(dir string, meta []string) error {
return nil
})
return err
return err
}
func addMissingId(dir string, id int64) error {
@ -189,20 +190,20 @@ func addMissingId(dir string, id int64) error {
return err
}
return nil
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
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
return err
}
func getMaxId(dir string) (int64, error) {
@ -314,20 +315,20 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
}
for _, option := range meta {
if strings.HasPrefix(line, option) {
line = strings.TrimPrefix(line, option + ":")
line = strings.TrimPrefix(line, option+":")
f.Meta[option] = strings.Trim(line, " ")
}
}
}
osf.Close()
_ = osf.Close()
return f, nil
}
func ReadFileContent(file File) (string, error) {
osf, err := os.Open(path)
osf, err := os.Open(file.FullPath)
if err != nil {
return nil, err
return "", err
}
content := ""
@ -342,13 +343,13 @@ func ReadFileContent(file File) (string, error) {
continue
}
if (isMetaPart) {
if isMetaPart {
continue
}
if (removeEmptyLine) {
if removeEmptyLine {
line = strings.Trim(line, " ")
if (line == "") {
if line == "" {
continue
} else {
removeEmptyLine = false
@ -357,6 +358,7 @@ func ReadFileContent(file File) (string, error) {
content = content + "\n" + line
}
_ = osf.Close()
return content, nil
}

View file

@ -24,7 +24,7 @@ func TestSunny(t *testing.T) {
t.Fatal("Couldn't prepare data for testing")
}
tree, err := BuildTree("./testdata/sunny1", []string{})
if err != nil {
if err != nil {
t.Errorf("Got error: %v", err)
}
@ -75,8 +75,6 @@ func TestFixFormat(t *testing.T) {
if !strings.Contains(str, "\n\n---\n") {
t.Fatal("Split line has not been formatted in file 'file1.md'.")
}
t.Log(tree)
}
func TestMeta(t *testing.T) {
@ -90,7 +88,7 @@ func TestMeta(t *testing.T) {
meta := []string{"option1", "option2"}
tree, err := BuildTree("./testdata/sunny1", meta)
if err != nil {
if err != nil {
t.Errorf("Got error: %v", err)
}