(cmd *cobra.Command, args []string)
| 36 | } |
| 37 | |
| 38 | func (c *replyCommand) run(cmd *cobra.Command, args []string) error { |
| 39 | if err := requireAuth(); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | threadID, err := strconv.ParseInt(args[0], 10, 64) |
| 44 | if err != nil { |
| 45 | return output.ErrUsage(fmt.Sprintf("invalid thread ID: %s", args[0])) |
| 46 | } |
| 47 | |
| 48 | ctx := cmd.Context() |
| 49 | |
| 50 | // Fetch topic page to extract recipients (To/CC/BCC). |
| 51 | topicResp, err := sdk.GetHTML(ctx, fmt.Sprintf("/topics/%d", threadID)) |
| 52 | if err != nil { |
| 53 | return convertSDKError(err) |
| 54 | } |
| 55 | addressed := htmlutil.ParseTopicAddressed(string(topicResp.Data)) |
| 56 | if len(addressed.To) == 0 && len(addressed.CC) == 0 && len(addressed.BCC) == 0 { |
| 57 | return output.ErrUsage("could not determine thread recipients") |
| 58 | } |
| 59 | |
| 60 | // Fetch entries to find the latest entry ID for the reply. |
| 61 | entriesResp, err := sdk.GetHTML(ctx, fmt.Sprintf("/topics/%d/entries", threadID)) |
| 62 | if err != nil { |
| 63 | return convertSDKError(err) |
| 64 | } |
| 65 | entries := htmlutil.ParseTopicEntriesHTML(string(entriesResp.Data)) |
| 66 | if len(entries) == 0 { |
| 67 | return output.ErrNotFound("entries for thread", args[0]) |
| 68 | } |
| 69 | |
| 70 | latestEntryID := entries[len(entries)-1].ID |
| 71 | |
| 72 | message := c.message |
| 73 | if message == "" { |
| 74 | if !stdinIsTerminal() { |
| 75 | message, err = readStdin() |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | if message == "" { |
| 80 | return output.ErrUsage("no message provided (use -m or --message to provide inline, or pipe to stdin)") |
| 81 | } |
| 82 | } else { |
| 83 | message, err = editor.Open("") |
| 84 | if err != nil { |
| 85 | return output.ErrAPI(0, fmt.Sprintf("could not open editor: %v", err)) |
| 86 | } |
| 87 | if message == "" { |
| 88 | return output.ErrUsage("empty message, aborting") |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if err = sdk.Entries().CreateReply(ctx, latestEntryID, message, addressed.To, addressed.CC, addressed.BCC); err != nil { |
| 94 | return convertSDKError(err) |
| 95 | } |
nothing calls this directly
no test coverage detected