utils.go
846 bytes
1package backend
2
3import (
4 "path/filepath"
5
6 "github.com/charmbracelet/soft-serve/git"
7 "github.com/charmbracelet/soft-serve/pkg/proto"
8)
9
10// LatestFile returns the contents of the latest file at the specified path in
11// the repository and its file path.
12func LatestFile(r proto.Repository, ref *git.Reference, pattern string) (string, string, error) {
13 repo, err := r.Open()
14 if err != nil {
15 return "", "", err
16 }
17 return git.LatestFile(repo, ref, pattern)
18}
19
20// Readme returns the repository's README.
21// It searches in priority order: root, .github, docs.
22func Readme(r proto.Repository, ref *git.Reference) (readme string, path string, err error) {
23 for _, dir := range []string{".", ".github", "docs"} {
24 readme, path, err = LatestFile(r, ref, filepath.Join(dir, "[rR][eE][aA][dD][mM][eE]*"))
25 if err == nil {
26 return
27 }
28 }
29 return
30}