95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
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 <id>",
|
|
Short: "Show document details",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runInfo,
|
|
}
|
|
|
|
func init() {
|
|
infoCmd.Flags().Bool("no-chunks", false, "return document metadata without chunk details")
|
|
rootCmd.AddCommand(infoCmd)
|
|
}
|
|
|
|
func runInfo(cmd *cobra.Command, args []string) error {
|
|
client := api.NewClient()
|
|
noChunks, _ := cmd.Flags().GetBool("no-chunks")
|
|
path := "/api/v1/documents/" + args[0]
|
|
if noChunks {
|
|
path += "?include_chunks=false"
|
|
}
|
|
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 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"`
|
|
ChunkCount int `json:"chunk_count"`
|
|
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", doc.ChunkCount)},
|
|
}
|
|
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
|
|
}
|