46 lines
923 B
Go
46 lines
923 B
Go
package blogposts
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"io/fs"
|
|
"strings"
|
|
)
|
|
|
|
type Post struct {
|
|
Title, Description, Body string
|
|
Tags []string
|
|
}
|
|
|
|
const (
|
|
titleSeparator = "Title: "
|
|
descriptionSeparator = "Description: "
|
|
)
|
|
|
|
func getPost(fileSystem fs.FS, fileName string) (Post, error) {
|
|
postFile, err := fileSystem.Open(fileName)
|
|
if err != nil {
|
|
return Post{}, err
|
|
}
|
|
defer postFile.Close()
|
|
|
|
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 io.Reader) (Post, error) {
|
|
scanner := bufio.NewScanner(postFile)
|
|
|
|
readMetaLine := func(tagName string) string {
|
|
scanner.Scan()
|
|
return strings.TrimPrefix(scanner.Text(), tagName)
|
|
}
|
|
|
|
post := Post{
|
|
Title: readMetaLine(titleSeparator),
|
|
Description: readMetaLine(descriptionSeparator),
|
|
}
|
|
return post, nil
|
|
}
|