package cmd import ( "fmt" "os" "strconv" "github.com/kb-search/kb/internal/api" "github.com/kb-search/kb/internal/output" "github.com/spf13/cobra" ) var updatenoteCmd = &cobra.Command{ Use: "updatenote ", Short: "Update an existing note's content", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 2 { return fmt.Errorf("requires document ID and text arguments\n\n Usage: kb updatenote 42 \"updated note text\"") } if _, err := strconv.Atoi(args[0]); err != nil { return fmt.Errorf("document ID must be an integer, got %q", args[0]) } return nil }, RunE: runUpdatenote, } func init() { rootCmd.AddCommand(updatenoteCmd) } func runUpdatenote(cmd *cobra.Command, args []string) error { docID := args[0] text := args[1] client := api.NewClient() body := map[string]string{"text": text} resp, err := client.Patch(fmt.Sprintf("/api/v1/notes/%s", docID), body) 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) } var result interface{} if err := api.DecodeJSON(resp, &result); err != nil { return fmt.Errorf("failed to decode response: %w", err) } if output.IsJSON() { output.PrintJSON(result) } else { fmt.Printf("Updated note %s\n", docID) } return nil }