todo editor
This commit is contained in:
+20
-5
@@ -114,16 +114,18 @@ func Read(dir, id string) (Todo, error) {
|
||||
return Parse(id, data)
|
||||
}
|
||||
|
||||
// List reads todos in filename order. Malformed files are reported, not overwritten.
|
||||
func List(dir string) ([]Todo, error) {
|
||||
// Scan reads valid todos in filename order and reports malformed files separately.
|
||||
// A missing directory is empty; other directory errors are fatal.
|
||||
func Scan(dir string) ([]Todo, []error, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return []Todo{}, nil
|
||||
return []Todo{}, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read todo directory: %w", err)
|
||||
return nil, nil, fmt.Errorf("read todo directory: %w", err)
|
||||
}
|
||||
result := make([]Todo, 0)
|
||||
var warnings []error
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".md") {
|
||||
continue
|
||||
@@ -134,10 +136,23 @@ func List(dir string) ([]Todo, error) {
|
||||
}
|
||||
todo, err := Read(dir, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
warnings = append(warnings, err)
|
||||
continue
|
||||
}
|
||||
result = append(result, todo)
|
||||
}
|
||||
return result, warnings, nil
|
||||
}
|
||||
|
||||
// List reports malformed files as errors rather than silently omitting them.
|
||||
func List(dir string) ([]Todo, error) {
|
||||
result, warnings, err := Scan(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(warnings) > 0 {
|
||||
return nil, errors.Join(warnings...)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -126,4 +126,8 @@ func TestMalformedAndUnsafeIDs(t *testing.T) {
|
||||
if _, err := List(dir); err == nil {
|
||||
t.Fatal("malformed file silently ignored")
|
||||
}
|
||||
items, warnings, err := Scan(dir)
|
||||
if err != nil || len(items) != 0 || len(warnings) != 1 {
|
||||
t.Fatalf("scan should report malformed file: %v, %v, %v", items, warnings, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user