Class: Gmail::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/utilities/gmail/cli.rb

Direct Known Subclasses

Mcpeasy::GmailCommands

Instance Method Summary collapse

Instance Method Details

#add_label(email_id, label) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/utilities/gmail/cli.rb', line 203

def add_label(email_id, label)
  result = tool.add_label(email_id, label)

  if result[:success]
    puts "✅ Label '#{label}' added to email"
  else
    puts "❌ Failed to add label to email"
  end
rescue RuntimeError => e
  warn "❌ Failed to add label to email: #{e.message}"
  exit 1
end

#archive(email_id) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/utilities/gmail/cli.rb', line 231

def archive(email_id)
  result = tool.archive_email(email_id)

  if result[:success]
    puts "✅ Email archived"
  else
    puts "❌ Failed to archive email"
  end
rescue RuntimeError => e
  warn "❌ Failed to archive email: #{e.message}"
  exit 1
end

#listObject



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
# File 'lib/utilities/gmail/cli.rb', line 34

def list
  labels = options[:labels]&.split(",")&.map(&:strip)

  result = tool.list_emails(
    start_date: options[:start_date],
    end_date: options[:end_date],
    max_results: options[:max_results],
    sender: options[:sender],
    subject: options[:subject],
    labels: labels,
    read_status: options[:read_status]
  )
  emails = result[:emails]

  if emails.empty?
    puts "📧 No emails found for the specified criteria"
  else
    puts "📧 Found #{result[:count]} email(s):"
    emails.each_with_index do |email, index|
      puts "   #{index + 1}. #{email[:subject] || "No subject"}"
      puts "      From: #{email[:from]}"
      puts "      Date: #{email[:date]}"
      puts "      Snippet: #{email[:snippet]}" if email[:snippet]
      puts "      Labels: #{email[:labels].join(", ")}" if email[:labels]&.any?
      puts "      ID: #{email[:id]}"
      puts
    end
  end
rescue RuntimeError => e
  warn "❌ Failed to list emails: #{e.message}"
  exit 1
end

#mark_read(email_id) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/utilities/gmail/cli.rb', line 175

def mark_read(email_id)
  result = tool.mark_as_read(email_id)

  if result[:success]
    puts "✅ Email marked as read"
  else
    puts "❌ Failed to mark email as read"
  end
rescue RuntimeError => e
  warn "❌ Failed to mark email as read: #{e.message}"
  exit 1
end

#mark_unread(email_id) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/utilities/gmail/cli.rb', line 189

def mark_unread(email_id)
  result = tool.mark_as_unread(email_id)

  if result[:success]
    puts "✅ Email marked as unread"
  else
    puts "❌ Failed to mark email as unread"
  end
rescue RuntimeError => e
  warn "❌ Failed to mark email as unread: #{e.message}"
  exit 1
end

#read(email_id) ⇒ Object



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
# File 'lib/utilities/gmail/cli.rb', line 95

def read(email_id)
  email = tool.get_email_content(email_id)

  puts "📧 Email Details:"
  puts "   ID: #{email[:id]}"
  puts "   Thread ID: #{email[:thread_id]}"
  puts "   Subject: #{email[:subject] || "No subject"}"
  puts "   From: #{email[:from]}"
  puts "   To: #{email[:to]}"
  puts "   CC: #{email[:cc]}" if email[:cc]
  puts "   BCC: #{email[:bcc]}" if email[:bcc]
  puts "   Date: #{email[:date]}"
  puts "   Labels: #{email[:labels].join(", ")}" if email[:labels]&.any?

  if email[:attachments]&.any?
    puts "   Attachments:"
    email[:attachments].each do |attachment|
      puts "      - #{attachment[:filename]} (#{attachment[:mime_type]}, #{attachment[:size]} bytes)"
    end
  end

  puts "\n📄 Body:"
  puts email[:body]
rescue RuntimeError => e
  warn "❌ Failed to read email: #{e.message}"
  exit 1
end

#remove_label(email_id, label) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/utilities/gmail/cli.rb', line 217

def remove_label(email_id, label)
  result = tool.remove_label(email_id, label)

  if result[:success]
    puts "✅ Label '#{label}' removed from email"
  else
    puts "❌ Failed to remove label from email"
  end
rescue RuntimeError => e
  warn "❌ Failed to remove label from email: #{e.message}"
  exit 1
end

#reply(email_id) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/utilities/gmail/cli.rb', line 155

def reply(email_id)
  result = tool.reply_to_email(
    email_id: email_id,
    body: options[:body],
    include_quoted: options[:include_quoted]
  )

  if result[:success]
    puts "✅ Reply sent successfully"
    puts "   Message ID: #{result[:message_id]}"
    puts "   Thread ID: #{result[:thread_id]}"
  else
    puts "❌ Failed to send reply"
  end
rescue RuntimeError => e
  warn "❌ Failed to send reply: #{e.message}"
  exit 1
end

#search(query) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/utilities/gmail/cli.rb', line 69

def search(query)
  result = tool.search_emails(
    query,
    max_results: options[:max_results]
  )
  emails = result[:emails]

  if emails.empty?
    puts "🔍 No emails found matching '#{query}'"
  else
    puts "🔍 Found #{result[:count]} email(s) matching '#{query}':"
    emails.each_with_index do |email, index|
      puts "   #{index + 1}. #{email[:subject] || "No subject"}"
      puts "      From: #{email[:from]}"
      puts "      Date: #{email[:date]}"
      puts "      Snippet: #{email[:snippet]}" if email[:snippet]
      puts "      ID: #{email[:id]}"
      puts
    end
  end
rescue RuntimeError => e
  warn "❌ Failed to search emails: #{e.message}"
  exit 1
end

#sendObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/utilities/gmail/cli.rb', line 130

def send
  result = tool.send_email(
    to: options[:to],
    subject: options[:subject],
    body: options[:body],
    cc: options[:cc],
    bcc: options[:bcc],
    reply_to: options[:reply_to]
  )

  if result[:success]
    puts "✅ Email sent successfully"
    puts "   Message ID: #{result[:message_id]}"
    puts "   Thread ID: #{result[:thread_id]}"
  else
    puts "❌ Failed to send email"
  end
rescue RuntimeError => e
  warn "❌ Failed to send email: #{e.message}"
  exit 1
end

#testObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/utilities/gmail/cli.rb', line 10

def test
  response = tool.test_connection

  if response[:ok]
    puts "✅ Successfully connected to Gmail"
    puts "   Email: #{response[:email]}"
    puts "   Messages: #{response[:messages_total]}"
    puts "   Threads: #{response[:threads_total]}"
  else
    warn "❌ Connection test failed"
  end
rescue RuntimeError => e
  puts "❌ Failed to connect to Gmail: #{e.message}\n\n#{e.backtrace.join("\n")}"
  exit 1
end

#trash(email_id) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/utilities/gmail/cli.rb', line 245

def trash(email_id)
  result = tool.trash_email(email_id)

  if result[:success]
    puts "✅ Email moved to trash"
  else
    puts "❌ Failed to move email to trash"
  end
rescue RuntimeError => e
  warn "❌ Failed to move email to trash: #{e.message}"
  exit 1
end