This repository has been archived on 2025-09-28. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
plaintext-encyclopedia/database.go

33 lines
782 B
Go
Raw Normal View History

package main
import (
"io/fs"
"log"
"os"
"strings"
)
type Database struct {
Keys []string
Entries map[string]string
}
func BuildDB(directory string) Database {
logger := log.Default()
logger.Println("Building database")
// keys, entries
var keys []string
entries := map[string]string{}
// get files in directory and read them
directory = strings.TrimRight(directory, "/") // we don't need that last / -> if '/' is used as directory, you are dumb.
entriesDirFs := os.DirFS(directory)
keys, err := fs.Glob(entriesDirFs, "*")
if err != nil { logger.Panicln(err) }
for _, k := range keys {
contentB, err := os.ReadFile(directory + "/" + k)
if err != nil { logger.Panicln(err) }
entries[k] = string(contentB)
}
return Database{Keys: keys, Entries: entries}
}