diff --git a/internal/lore/catalog.go b/internal/lore/catalog.go index ddfb13c..b1b7dfb 100644 --- a/internal/lore/catalog.go +++ b/internal/lore/catalog.go @@ -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 { diff --git a/internal/lore/catalog_cmd.go b/internal/lore/catalog_cmd.go index bb72f3b..9dbe38c 100644 --- a/internal/lore/catalog_cmd.go +++ b/internal/lore/catalog_cmd.go @@ -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 diff --git a/internal/lore/catalog_test.go b/internal/lore/catalog_test.go index 02d7a89..3b1e2d7 100644 --- a/internal/lore/catalog_test.go +++ b/internal/lore/catalog_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strings" "testing" ) @@ -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()) + } +}