From cbee75e3c328d97637f9c40a5b7e34b0f0319eff Mon Sep 17 00:00:00 2001
From: Arnas Udovicius <zordsdavini@gmail.com>
Date: Sun, 8 May 2022 23:19:55 +0300
Subject: [PATCH] code fix

---
 tree.go      | 34 ++++++++++++++++++----------------
 tree_test.go |  6 ++----
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/tree.go b/tree.go
index 3d181af..d766a63 100644
--- a/tree.go
+++ b/tree.go
@@ -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
 }
diff --git a/tree_test.go b/tree_test.go
index 8efa48e..0567d69 100644
--- a/tree_test.go
+++ b/tree_test.go
@@ -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)
 	}