package cmd import ( "fmt" "net/url" "os" "github.com/kb-search/kb/internal/api" "github.com/kb-search/kb/internal/output" "github.com/spf13/cobra" ) var listCmd = &cobra.Command{ Use: "list", Short: "List documents in the knowledge base", RunE: runList, } func init() { listCmd.Flags().String("type", "", "filter by document type") listCmd.Flags().String("tags", "", "filter by tags (comma-separated)") rootCmd.AddCommand(listCmd) } func runList(cmd *cobra.Command, args []string) error { docType, _ := cmd.Flags().GetString("type") tags, _ := cmd.Flags().GetString("tags") params := url.Values{} if docType != "" { params.Set("type", docType) } if tags != "" { params.Set("tags", tags) } path := "/api/v1/documents" if len(params) > 0 { path += "?" + params.Encode() } client := api.NewClient() resp, err := client.Get(path) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if err := api.CheckError(resp); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if output.IsJSON() { var raw interface{} if err := api.DecodeJSON(resp, &raw); err != nil { return fmt.Errorf("failed to decode response: %w", err) } output.PrintJSON(raw) return nil } var docs []struct { ID int `json:"id"` Title string `json:"title"` Type string `json:"doc_type"` Tags []string `json:"tags"` } if err := api.DecodeJSON(resp, &docs); err != nil { return fmt.Errorf("failed to decode response: %w", err) } if len(docs) == 0 { fmt.Println("No documents found.") return nil } headers := []string{"ID", "TITLE", "TYPE", "TAGS"} var rows [][]string for _, d := range docs { rows = append(rows, []string{fmt.Sprintf("%d", d.ID), d.Title, d.Type, joinStrings(d.Tags)}) } output.PrintTable(headers, rows) return nil }