Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/lore/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ func Catalog(ctx context.Context, db *sql.DB, p *CatalogParams) (*CatalogResult,
if strings.TrimSpace(p.Dir) == "" {
return nil, fmt.Errorf("lore: catalog: dir required")
}
if p.Kind != "" {
valid := false
for _, k := range AllKinds() {
if p.Kind == k {
valid = true
break
}
}
if !valid {
return nil, fmt.Errorf("lore: catalog: invalid kind %q: valid kinds are %s", p.Kind, strings.Join(func() []string {
kinds := make([]string, len(AllKinds()))
for i, k := range AllKinds() {
kinds[i] = string(k)
}
return kinds
}(), ", "))
}
}

info, err := os.Stat(p.Dir)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/lore/catalog_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ var CatalogCommand = &command.Command[CatalogInput, CatalogCmdOutput]{
if strings.TrimSpace(in.Dir) == "" {
return CatalogCmdOutput{}, errors.New("dir required")
}
if in.Kind != "" {
valid := false
for _, k := range AllKinds() {
if Kind(in.Kind) == k {
valid = true
break
}
}
if !valid {
return CatalogCmdOutput{}, fmt.Errorf("invalid --kind %q: valid kinds are: %s", in.Kind, strings.Join(func() []string {
kinds := make([]string, len(AllKinds()))
for i, k := range AllKinds() {
kinds[i] = string(k)
}
return kinds
}(), ", "))
}
}
db, err := d.OpenDB(ctx)
if err != nil {
return CatalogCmdOutput{}, err
Expand Down
30 changes: 30 additions & 0 deletions internal/lore/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -180,3 +181,32 @@ func TestCatalog_NonExistentDir(t *testing.T) {
t.Error("Catalog on non-existent dir should return error")
}
}

func TestCatalog_InvalidKind(t *testing.T) {
ctx := context.Background()
db := openTestDB(t, "catalog-kind")

dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.md"), []byte("# Note\n\nContent."), 0o600); err != nil {
t.Fatalf("write note.md: %v", err)
}

_, err := Catalog(ctx, db, &CatalogParams{
Dir: dir,
ProjectID: "catalog-kind",
Kind: "not-a-real-kind",
})
if err == nil {
t.Fatal("Catalog with invalid --kind should return error")
}
// Verify error mentions valid kinds.
for _, k := range AllKinds() {
if !strings.Contains(err.Error(), string(k)) {
t.Errorf("error %q does not contain valid kind %q", err.Error(), k)
}
}
// Verify error mentions the invalid kind.
if !strings.Contains(err.Error(), "not-a-real-kind") {
t.Errorf("error %q does not contain the invalid kind", err.Error())
}
}