-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.go
More file actions
94 lines (84 loc) · 1.98 KB
/
Copy pathread.go
File metadata and controls
94 lines (84 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gonotes
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// noteFile is a parsed note together with its original filename on disk.
type noteFile struct {
Note
Filename string
}
// readNoteFile reads a single note file by name from dir.
// On error it appends to errs and returns nil.
func readNoteFile(dir, name string, errs *[]ScanError) *noteFile {
id, _ := IDFromFilename(name)
path := filepath.Join(dir, name)
f, err := os.Open(path)
if err != nil {
*errs = append(*errs, ScanError{
Filename: name,
Message: fmt.Sprintf("open: %v", err),
})
return nil
}
defer f.Close()
note, err := ReadNote(id, f)
if err != nil {
*errs = append(*errs, ScanError{
Filename: name,
Message: fmt.Sprintf("read note: %v", err),
})
return nil
}
return ¬eFile{Note: *note, Filename: name}
}
// readNoteFiles reads all .md files from dir and parses them.
// It returns the parsed noteFiles and any per-file errors.
func readNoteFiles(dir string) ([]noteFile, []ScanError, error) {
f, err := os.Open(dir)
if err != nil {
return nil, nil, fmt.Errorf("open notes dir: %w", err)
}
defer f.Close()
var files []noteFile
var errs []ScanError
for {
entries, err := f.ReadDir(readDirBatch)
for _, e := range entries {
if e.IsDir() {
continue
}
name := e.Name()
if !strings.HasSuffix(name, ".md") {
continue
}
nf := readNoteFile(dir, name, &errs)
if nf != nil {
files = append(files, *nf)
}
}
if err != nil {
if err == io.EOF {
break
}
return nil, nil, fmt.Errorf("read dir: %w", err)
}
}
return files, errs, nil
}
// readNotesFromDir reads all .md files from dir and returns just the Notes.
// Used by callers that don't need the original filenames.
func readNotesFromDir(dir string) ([]Note, []ScanError, error) {
files, errs, err := readNoteFiles(dir)
if err != nil {
return nil, nil, err
}
notes := make([]Note, len(files))
for i := range files {
notes[i] = files[i].Note
}
return notes, errs, nil
}