package cmd import ( "fmt" "os" "github.com/kb-search/kb/internal/api" "github.com/kb-search/kb/internal/output" "github.com/spf13/cobra" ) var infoCmd = &cobra.Command{ Use: "info ", Short: "Show document details", Args: cobra.ExactArgs(1), RunE: runInfo, } func init() { rootCmd.AddCommand(infoCmd) } func runInfo(cmd *cobra.Command, args []string) error { client := api.NewClient() resp, err := client.Get("/api/v1/documents/" + args[0]) 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 doc struct { ID int `json:"id"` Title string `json:"title"` Type string `json:"doc_type"` Tags []string `json:"tags"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` Chunks []struct { ID int `json:"id"` Page interface{} `json:"page"` Section string `json:"section"` } `json:"chunks"` } if err := api.DecodeJSON(resp, &doc); err != nil { return fmt.Errorf("failed to decode response: %w", err) } pairs := [][]string{ {"ID", fmt.Sprintf("%d", doc.ID)}, {"Title", doc.Title}, {"Type", doc.Type}, {"Tags", joinStrings(doc.Tags)}, {"Created", doc.CreatedAt}, {"Updated", doc.UpdatedAt}, {"Chunks", fmt.Sprintf("%d", len(doc.Chunks))}, } output.PrintKeyValue(pairs) if len(doc.Chunks) > 0 { fmt.Println() headers := []string{"CHUNK ID", "PAGE", "SECTION"} var rows [][]string for _, c := range doc.Chunks { page := "" if c.Page != nil { page = fmt.Sprintf("%v", c.Page) } rows = append(rows, []string{fmt.Sprintf("%d", c.ID), page, c.Section}) } output.PrintTable(headers, rows) } return nil }