package cmd import ( "bufio" "fmt" "os" "strings" "github.com/kb-search/kb/internal/api" "github.com/kb-search/kb/internal/output" "github.com/spf13/cobra" ) var removeCmd = &cobra.Command{ Use: "remove ", Short: "Remove a document from the knowledge base", Args: cobra.ExactArgs(1), RunE: runRemove, } func init() { removeCmd.Flags().BoolP("yes", "y", false, "skip confirmation prompt") rootCmd.AddCommand(removeCmd) } func runRemove(cmd *cobra.Command, args []string) error { yes, _ := cmd.Flags().GetBool("yes") if !yes { fmt.Printf("Remove document %s? [y/N] ", args[0]) reader := bufio.NewReader(os.Stdin) answer, _ := reader.ReadString('\n') answer = strings.TrimSpace(strings.ToLower(answer)) if answer != "y" && answer != "yes" { fmt.Println("Cancelled.") return nil } } client := api.NewClient() resp, err := client.Delete("/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 { // Some DELETE endpoints return no body output.PrintJSON(map[string]string{"status": "removed", "id": args[0]}) return nil } output.PrintJSON(raw) } else { fmt.Printf("Removed: %s\n", args[0]) } return nil }