-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcompose.go
More file actions
172 lines (156 loc) · 4.79 KB
/
compose.go
File metadata and controls
172 lines (156 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package cmd
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/basecamp/hey-cli/internal/editor"
"github.com/basecamp/hey-cli/internal/output"
)
type composeCommand struct {
cmd *cobra.Command
to string
cc string
bcc string
subject string
message string
threadID string
from string
}
func newComposeCommand() *composeCommand {
composeCommand := &composeCommand{}
composeCommand.cmd = &cobra.Command{
Use: "compose",
Short: "Compose a new message",
Annotations: map[string]string{
"agent_notes": "Creates a new email. Requires --subject. Use --to (optionally with --cc/--bcc) for new threads or --thread-id for existing ones.",
},
Example: ` hey compose --to alice@example.com --subject "Hello" -m "Hi there"
hey compose --to alice@example.com --cc bob@example.com --bcc carol@example.org --subject "Hello" -m "Hi"
hey compose --subject "Update" --thread-id 12345 -m "Thread reply"
echo "Long message" | hey compose --to bob@example.com --subject "Report"`,
RunE: composeCommand.run,
}
composeCommand.cmd.Flags().StringVar(&composeCommand.to, "to", "", "Recipient email address(es)")
composeCommand.cmd.Flags().StringVar(&composeCommand.cc, "cc", "", "CC recipient email address(es)")
composeCommand.cmd.Flags().StringVar(&composeCommand.bcc, "bcc", "", "BCC recipient email address(es)")
composeCommand.cmd.Flags().StringVar(&composeCommand.subject, "subject", "", "Message subject (required)")
composeCommand.cmd.Flags().StringVarP(&composeCommand.message, "message", "m", "", "Message body (or opens $EDITOR)")
composeCommand.cmd.Flags().StringVar(&composeCommand.threadID, "thread-id", "", "Thread ID to post message to")
composeCommand.cmd.Flags().StringVar(&composeCommand.from, "from", "", "Sender email address (overrides default)")
return composeCommand
}
func (c *composeCommand) run(cmd *cobra.Command, args []string) error {
if err := requireAuth(); err != nil {
return err
}
if c.subject == "" {
return output.ErrUsageHint("--subject is required", "hey compose --to <email> --subject <subject> -m <message>")
}
message := c.message
if message == "" {
if !stdinIsTerminal() {
var err error
message, err = readStdin()
if err != nil {
return err
}
if message == "" {
return output.ErrUsage("no message provided (use -m or --message to provide inline, or pipe to stdin)")
}
} else {
var err error
message, err = editor.Open("")
if err != nil {
return output.ErrAPI(0, fmt.Sprintf("could not open editor: %v", err))
}
if message == "" {
return output.ErrUsage("empty message, aborting")
}
}
}
ctx := cmd.Context()
// When --from or default_sender is set, we bypass the SDK's service methods
// and call PostMutation directly so we can control the acting_sender_id.
hasSenderOverride := c.from != "" || cfg.DefaultSender != ""
if c.threadID != "" {
topicID, err := strconv.ParseInt(c.threadID, 10, 64)
if err != nil {
return output.ErrUsage(fmt.Sprintf("invalid thread ID: %s", c.threadID))
}
if hasSenderOverride {
senderID, err := effectiveSenderID(ctx, c.from)
if err != nil {
return err
}
body := map[string]any{
"acting_sender_id": senderID,
"message": map[string]any{
"content": message,
},
}
if _, err := sdk.PostMutation(ctx, fmt.Sprintf("/topics/%d/entries.json", topicID), body); err != nil {
return convertSDKError(err)
}
} else {
if err := sdk.Messages().CreateTopicMessage(ctx, topicID, message); err != nil {
return convertSDKError(err)
}
}
} else {
to := parseAddresses(c.to)
cc := parseAddresses(c.cc)
bcc := parseAddresses(c.bcc)
if hasSenderOverride {
senderID, err := effectiveSenderID(ctx, c.from)
if err != nil {
return err
}
addressed := map[string]any{}
if len(to) > 0 {
addressed["directly"] = to
}
if len(cc) > 0 {
addressed["copied"] = cc
}
if len(bcc) > 0 {
addressed["blindcopied"] = bcc
}
body := map[string]any{
"acting_sender_id": senderID,
"message": map[string]any{
"subject": c.subject,
"content": message,
},
"entry": map[string]any{
"addressed": addressed,
},
}
if _, err := sdk.PostMutation(ctx, "/messages.json", body); err != nil {
return convertSDKError(err)
}
} else {
if err := sdk.Messages().Create(ctx, c.subject, message, to, cc, bcc); err != nil {
return convertSDKError(err)
}
}
}
if writer.IsStyled() {
fmt.Fprintln(cmd.OutOrStdout(), "Message sent.")
return nil
}
return writeOK(nil, output.WithSummary("Message sent"))
}
func parseAddresses(s string) []string {
if s == "" {
return nil
}
var addrs []string
for _, addr := range strings.Split(s, ",") {
addr = strings.TrimSpace(addr)
if addr != "" {
addrs = append(addrs, addr)
}
}
return addrs
}