hidrocor

commit 7547c36768360736dafb7af1b75a832bf3695120

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

wiki: add lookup wiki functionallity

 wiki.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++


diff --git a/wiki.go b/wiki.go
new file mode 100644
index 0000000000000000000000000000000000000000..68d86d9aebe1bfb3e0b081c6085e9ebc5e74246a
--- /dev/null
+++ b/wiki.go
@@ -0,0 +1,55 @@
+// This package holds the logic for reading a dir, finding out the main file,
+// etc. It's a mere abstraction on top of the wiki's filesystem. When fed with
+// a path, it'll return a read file for markdown consumption
+
+package hidrocor
+
+import (
+	"os"
+	"path"
+)
+
+func Lookup(wikiPath string, docPath string) (*file.File, error) {
+	if path == "" {
+		path = "README.md"
+	}
+
+	routePath := path.Join(wikiPath, docPath)
+	fileInfo, err := os.Stat(docPath)
+
+	if err != nil {
+		return nil, err
+	}
+
+	if fileInfo.IsDir() {
+		files, err := os.ReadDir(routePath)
+		if err != nil {
+			return nil, err
+		}
+
+		for _, file := range files {
+			switch file.Name() {
+				case
+				"index.md",
+				"INDEX.md",
+				"readme.md",
+				"README.md":
+				source, err = os.ReadFile(path.Join(routePath, file.Name()))
+
+				if err != nil {
+					return nil, err
+				}
+
+				return source, nil
+			}
+		}
+	} else {
+		source, err = os.ReadFile(routePath)
+
+		if err != nil {
+			return nil, err
+		}
+
+		return source, nil
+	}
+}