Skip to content

Commit f26e1ab

Browse files
committed
internal/fetch: split extractReadme from extractReadmes
In CL 557818 we're going to process readmes a single unit at a time, so split a function that extracts a single readme at a time out of extractReadmes so we can use that in the cl. Change-Id: I5c39a95869de12e6f4aeebd2f42d87f24e147786 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/559615 LUCI-TryBot-Result: Go LUCI <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent c7eaeb8 commit f26e1ab

File tree

1 file changed

+72
-30
lines changed

1 file changed

+72
-30
lines changed

internal/fetch/readme.go

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io/fs"
12+
"os"
1213
"path"
1314
"strings"
1415

@@ -21,60 +22,101 @@ import (
2122
func extractReadmes(modulePath, resolvedVersion string, contentDir fs.FS) (_ []*internal.Readme, err error) {
2223
defer derrors.Wrap(&err, "extractReadmes(ctx, %q, %q, r)", modulePath, resolvedVersion)
2324

24-
// The key is the README directory. Since we only store one README file per
25-
// directory, we use this below to prioritize READMEs in markdown.
26-
readmes := map[string]*internal.Readme{}
27-
var skipPaths = []string{"_"}
25+
var readmes []*internal.Readme
2826
err = fs.WalkDir(contentDir, ".", func(pathname string, d fs.DirEntry, err error) error {
2927
if err != nil {
3028
return err
3129
}
32-
for _, sp := range skipPaths {
33-
// if the name of the folder has a prefix listed in skipPaths
34-
// then we should skip the directory.
35-
// e.g. _foo
36-
if strings.HasPrefix(pathname, sp) {
37-
return fs.SkipDir
38-
}
30+
31+
if !d.IsDir() {
32+
return nil
33+
}
34+
35+
readme, err := extractReadme(modulePath, path.Join(modulePath, pathname), resolvedVersion, contentDir)
36+
if err != nil {
37+
return err
3938
}
39+
if readme == nil {
40+
// no readme for the directory
41+
return nil
42+
}
43+
readmes = append(readmes, readme)
44+
return nil
45+
})
46+
if err != nil && !errors.Is(err, fs.ErrNotExist) { // we can get NotExist on an empty FS {
47+
return nil, err
48+
}
49+
return readmes, nil
50+
}
51+
52+
// rel returns the relative path from the modulePath to the pkgPath
53+
// returning "." if they're the same.
54+
func rel(pkgPath, modulePath string) string {
55+
suff := internal.Suffix(pkgPath, modulePath)
56+
if suff == "" {
57+
return "."
58+
}
59+
return suff
60+
}
61+
62+
// extractReadme returns the file path and contents the unit's README,
63+
// if there is one. dir is the directory path prefixed with the modulePath.
64+
func extractReadme(modulePath, dir, resolvedVersion string, contentDir fs.FS) (_ *internal.Readme, err error) {
65+
defer derrors.Wrap(&err, "extractReadme(ctx, %q, %q %q, r)", modulePath, dir, resolvedVersion)
66+
67+
innerPath := rel(dir, modulePath)
68+
if strings.HasPrefix(innerPath, "_") {
69+
// TODO(matloob): do we want to check each element of the path?
70+
// The original code didn't.
71+
return nil, nil
72+
}
4073

41-
if !d.IsDir() && isReadme(pathname) {
42-
info, err := d.Info()
74+
f, err := contentDir.Open(innerPath)
75+
if err != nil {
76+
if os.IsNotExist(err) {
77+
return nil, nil
78+
}
79+
return nil, err
80+
}
81+
rdf, ok := f.(fs.ReadDirFile)
82+
if !ok {
83+
return nil, fmt.Errorf("could not open directory for %v", dir)
84+
}
85+
entries, err := rdf.ReadDir(0)
86+
if err != nil {
87+
return nil, err
88+
}
89+
var readme *internal.Readme
90+
for _, e := range entries {
91+
pathname := path.Join(innerPath, e.Name())
92+
if !e.IsDir() && isReadme(pathname) {
93+
info, err := e.Info()
4394
if err != nil {
44-
return err
95+
return nil, err
4596
}
4697
if info.Size() > MaxFileSize {
47-
return fmt.Errorf("file size %d exceeds max limit %d: %w", info.Size(), MaxFileSize, derrors.ModuleTooLarge)
98+
return nil, fmt.Errorf("file size %d exceeds max limit %d: %w", info.Size(), MaxFileSize, derrors.ModuleTooLarge)
4899
}
49100
c, err := readFSFile(contentDir, pathname, MaxFileSize)
50101
if err != nil {
51-
return err
102+
return nil, err
52103
}
53104

54-
key := path.Dir(pathname)
55-
if r, ok := readmes[key]; ok {
105+
if readme != nil {
56106
// Prefer READMEs written in markdown, since we style these on
57107
// the frontend.
58-
ext := path.Ext(r.Filepath)
108+
ext := path.Ext(readme.Filepath)
59109
if ext == ".md" || ext == ".markdown" {
60-
return nil
110+
continue
61111
}
62112
}
63-
readmes[key] = &internal.Readme{
113+
readme = &internal.Readme{
64114
Filepath: pathname,
65115
Contents: string(c),
66116
}
67117
}
68-
return nil
69-
})
70-
if err != nil && !errors.Is(err, fs.ErrNotExist) { // we can get NotExist on an empty FS {
71-
return nil, err
72-
}
73-
var rs []*internal.Readme
74-
for _, r := range readmes {
75-
rs = append(rs, r)
76118
}
77-
return rs, nil
119+
return readme, nil
78120
}
79121

80122
var excludedReadmeExts = map[string]bool{".go": true, ".vendor": true}

0 commit comments

Comments
 (0)