2024-09-23 21:09:10 +00:00
|
|
|
package blogposts
|
|
|
|
|
2024-09-24 18:57:28 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
)
|
2024-09-23 21:09:10 +00:00
|
|
|
|
|
|
|
type Post struct {
|
|
|
|
Title, Description, Body string
|
|
|
|
Tags []string
|
|
|
|
}
|
|
|
|
|
2024-09-24 18:57:28 +00:00
|
|
|
func NewPostsFromFS(fileSystem fs.FS) ([]Post, error) {
|
|
|
|
dir, err := fs.ReadDir(fileSystem, ".")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var posts []Post
|
|
|
|
for _, f := range dir {
|
|
|
|
post, err := getPost(fileSystem, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
posts = append(posts, post)
|
|
|
|
}
|
|
|
|
return posts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPost(fileSystem fs.FS, f fs.DirEntry) (Post, error) {
|
|
|
|
postFile, err := fileSystem.Open(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
return Post{}, err
|
|
|
|
}
|
|
|
|
defer postFile.Close()
|
|
|
|
|
2024-09-24 19:03:33 +00:00
|
|
|
return newPost(postFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Does newPost have to be coupled to an fs.File ?
|
|
|
|
// Do we use all the methods and data from this type? What do we really need?
|
|
|
|
func newPost(postFile fs.File) (Post, error) {
|
2024-09-24 18:57:28 +00:00
|
|
|
postData, err := io.ReadAll(postFile)
|
|
|
|
if err != nil {
|
|
|
|
return Post{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
post := Post{Title: string(postData)[7:]}
|
|
|
|
return post, nil
|
2024-09-23 21:09:10 +00:00
|
|
|
}
|