Class: FlycalCli::Cli

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/flycal_cli/cli.rb', line 17

def self.exit_on_failure?
  true
end

Instance Method Details

#calendarsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flycal_cli/cli.rb', line 64

def calendars
  apply_locale_override
  unless Auth.logged_in?
    puts Locale.t("errors.not_connected")
    exit 1
  end

  calendars = load_calendars
  return if calendars.nil?

  calendars.each do |cal|
    summary = cal.summary || cal.id
    puts "#{summary} #{cal.id}"
  end
end

#configObject



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
# File 'lib/flycal_cli/cli.rb', line 81

def config
  apply_locale_override
  unless Auth.logged_in?
    puts Locale.t("errors.not_connected")
    exit 1
  end

  with_config_interrupt_handling do
    prompt = TTY::Prompt.new
    choice = prompt.select(
      Locale.t("config.prompt"),
      {
        Locale.t("config.options.calendar_default") => :calendar_default,
        Locale.t("config.options.exclude_calendars") => :exclude_calendars,
        Locale.t("config.options.edit_config") => :edit_config
      },
      per_page: 10
    )

    case choice
    when :calendar_default
      config_calendar_default
    when :exclude_calendars
      config_exclude_calendars
    when :edit_config
      config_edit
    end
  end
end

#loginObject



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
# File 'lib/flycal_cli/cli.rb', line 22

def 
  apply_locale_override
  if Auth.logged_in?
    puts "āœ“ You are already connected to your Google account."
    puts "\nRun 'flycal config' to set the default calendar."
    return
  end

  unless Config.credentials_exist?
    puts "Error: Credentials file not found."
    puts "\nTo configure flycal:"
    puts "1. Go to https://console.cloud.google.com/apis/credentials"
    puts "2. Create 'Desktop app' credentials"
    puts "3. Download the JSON and save it as: #{Config.credentials_path}"
    puts "\nAlso add this URI as an authorized redirect:"
    puts "  http://127.0.0.1:9292/oauth2callback"
    return
  end

  begin
    Auth.
    puts "\nāœ“ Authentication completed successfully!"
    puts "\nRun 'flycal config' to set the default calendar."
  rescue FlycalCli::Error => e
    puts "Error: #{e.message}"
    exit 1
  end
end

#logoutObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/flycal_cli/cli.rb', line 52

def logout
  apply_locale_override
  unless Auth.logged_in?
    puts "You are not connected to any Google account."
    return
  end

  Auth.logout
  puts "āœ“ Disconnected successfully."
end

#searchObject



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
173
174
175
# File 'lib/flycal_cli/cli.rb', line 133

def search
  apply_locale_override
  unless Auth.logged_in?
    puts "You are not connected. Run 'flycal login' first."
    exit 1
  end

  time_min = options[:from].to_s.empty? ? Date.today.to_time : DateTimeParser.parse(options[:from])
  begin
    time_max = if options[:in].to_s.empty?
                 options[:to].to_s.empty? ? (Date.today + 30).to_time + 86400 - 1 : DateTimeParser.parse(options[:to], end_of_day: true)
               else
                 DurationParser.add_to_time(options[:in], time_min)
               end
  rescue FlycalCli::Error => e
    puts "Error: #{e.message}"
    exit 1
  end

  if time_min > time_max
    puts "Error: 'from' date must be before 'to' date."
    exit 1
  end

  creds = Auth.credentials
  service = CalendarService.new(creds)

  calendar_ids = resolve_calendar_ids(service, options[:calendar])
  if calendar_ids.empty?
    puts "No calendars found."
    exit 1
  end

  events = service.list_all_events(
    calendar_ids,
    time_min: time_min,
    time_max: time_max,
    query: options[:description]
  )

  print_events(service, events)
  print_search_summary(events, time_min: time_min, time_max: time_max)
end

#slotsObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/flycal_cli/cli.rb', line 202

def slots
  apply_locale_override
  unless Auth.logged_in?
    puts Locale.t("errors.not_connected")
    exit 1
  end

  slot_cfg = Config.slots_config
  defaults = slot_cfg.fetch("defaults", {})
  in_value = options[:in].to_s.strip
  in_value = "1 week" if in_value.empty?

  duration_value = options[:duration].to_s.strip
  duration_value = defaults["default_duration"].to_s.strip if duration_value.empty?
  duration_value = "45min" if duration_value.empty?

  from_value = options[:from].to_s.strip
  from_value = defaults["from"].to_s.strip if from_value.empty?
  from_value = "now" if from_value.empty?

  begin
    template_name, template = resolve_slots_template(slot_cfg, options[:template])
    slot_duration = DurationParser.to_seconds(duration_value)
    free_before = DurationParser.to_seconds(slot_cfg["free_before"] || "0m")
    free_after = DurationParser.to_seconds(slot_cfg["free_after"] || "0m")
    time_min = parse_slots_from(from_value)
    time_max = DurationParser.add_to_time(in_value, time_min)
    hours = parse_workhours(template["hours"])
    days = parse_template_days(template["days"])
  rescue FlycalCli::Error => e
    puts "Error: #{e.message}"
    exit 1
  end

  if time_min >= time_max
    puts Locale.t("errors.duration_positive")
    exit 1
  end

  creds = Auth.credentials
  service = CalendarService.new(creds)

  exclude_calendar_ids = resolve_slots_exclude_calendar_ids(service, slot_cfg, options[:calendar])
  if exclude_calendar_ids.empty?
    puts Locale.t("slots.no_calendar")
    exit 1
  end

  calendars = service.list_calendars
  calendar_meta = exclude_calendar_ids.filter_map do |id|
    cal = calendars.find { |c| c.id == id }
    name = cal&.summary || id
    { id: id, name: name }
  end

  fetch_from = time_min - free_before
  events = exclude_calendar_ids.flat_map do |calendar_id|
    service.list_events(
      calendar_id,
      time_min: fetch_from,
      time_max: time_max
    )
  rescue Google::Apis::Errors::Error => e
    warn Locale.t("errors.calendar_fetch", calendar: calendar_id, message: e.message)
    []
  end

  finder = SlotFinder.new(
    events: events,
    time_min: time_min,
    time_max: time_max,
    slot_duration_seconds: slot_duration,
    hours: hours,
    days: days,
    free_before_seconds: free_before,
    free_after_seconds: free_after
  )

  slots_by_day = finder.slots_by_day
  slot_count = slots_by_day.values.sum(&:size)
  puts SlotFormatter.format_header(
    from: time_min,
    to: time_max,
    duration: duration_value,
    calendars: calendar_meta,
    count: slot_count,
    template: template_name
  )
  puts ""
  output = SlotFormatter.format_output(slots_by_day)
  if output.empty?
    puts Locale.t("slots.no_available")
  else
    puts output
    copy_slots_to_clipboard(output)
  end
end

#updateObject



301
302
303
304
305
306
307
308
309
# File 'lib/flycal_cli/cli.rb', line 301

def update
  apply_locale_override
  puts "Updating flycal-cli..."
  success = system("gem", "update", "flycal-cli")
  return if success

  puts "Error: failed to update flycal-cli."
  exit 1
end

#versionObject



312
313
314
315
# File 'lib/flycal_cli/cli.rb', line 312

def version
  apply_locale_override
  puts "flycal #{FlycalCli::VERSION}"
end