-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoutput.go
More file actions
62 lines (55 loc) · 1.74 KB
/
Copy pathoutput.go
File metadata and controls
62 lines (55 loc) · 1.74 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
package mail
import (
"context"
"fmt"
"strings"
"github.com/open-cli-collective/google-readonly/internal/gmail"
"github.com/open-cli-collective/google-readonly/internal/output"
)
// ClientFactory is the function used to create Gmail clients.
// Override in tests to inject mocks.
var ClientFactory = func() (gmail.GmailClientInterface, error) {
return gmail.NewClient(context.Background())
}
// newGmailClient creates and returns a new Gmail client
func newGmailClient() (gmail.GmailClientInterface, error) {
return ClientFactory()
}
// printJSON encodes data as indented JSON to stdout
func printJSON(data any) error {
return output.JSONStdout(data)
}
// MessagePrintOptions controls which fields to include in message output
type MessagePrintOptions struct {
IncludeThreadID bool
IncludeTo bool
IncludeSnippet bool
IncludeBody bool
}
// printMessageHeader prints the common header fields of a message
func printMessageHeader(msg *gmail.Message, opts MessagePrintOptions) {
fmt.Printf("ID: %s\n", msg.ID)
if opts.IncludeThreadID {
fmt.Printf("ThreadID: %s\n", msg.ThreadID)
}
// Sanitize user-provided content to prevent terminal injection attacks
fmt.Printf("From: %s\n", SanitizeOutput(msg.From))
if opts.IncludeTo {
fmt.Printf("To: %s\n", SanitizeOutput(msg.To))
}
fmt.Printf("Subject: %s\n", SanitizeOutput(msg.Subject))
fmt.Printf("Date: %s\n", msg.Date)
if len(msg.Labels) > 0 {
fmt.Printf("Labels: %s\n", strings.Join(msg.Labels, ", "))
}
if len(msg.Categories) > 0 {
fmt.Printf("Categories: %s\n", strings.Join(msg.Categories, ", "))
}
if opts.IncludeSnippet {
fmt.Printf("Snippet: %s\n", SanitizeOutput(msg.Snippet))
}
if opts.IncludeBody {
fmt.Print("\n--- Body ---\n\n")
fmt.Println(SanitizeOutput(msg.Body))
}
}