Class: CalendarAssistant::CLI::Commands

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

Constant Summary collapse

HISTORY_FILE_PATH =
File.join (ENV["CA_HOME"] || ENV["HOME"]), ".calendar-assistant.history"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.has_eventsObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/calendar_assistant/cli/commands.rb', line 19

def self.has_events
  option CalendarAssistant::Config::Keys::Options::MUST_BE,
         type: :string,
         desc: "Event properties that must be true (see README)",
         banner: "PROPERTY1[,PROPERTY2[,...]]",
         aliases: ["-b"]
  option CalendarAssistant::Config::Keys::Options::MUST_NOT_BE,
         type: :string,
         desc: "Event properties that must be false (see README)",
         banner: "PROPERTY1[,PROPERTY2[,...]]",
         aliases: ["-n"]
end

.has_multiple_calendarsObject



32
33
34
35
36
37
38
# File 'lib/calendar_assistant/cli/commands.rb', line 32

def self.has_multiple_calendars
  option CalendarAssistant::Config::Keys::Options::CALENDARS,
         type: :string,
         banner: "CALENDAR1[,CALENDAR2[,...]]",
         desc: "[default 'me'] people (email IDs) to whom this command will be applied",
         aliases: ["-a", "--attendees"]
end

.will_create_a_serviceObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/calendar_assistant/cli/commands.rb', line 6

def self.will_create_a_service
  option CalendarAssistant::Config::Keys::Settings::PROFILE,
         type: :string,
         desc: "the profile you'd like to use (if different from default)",
         aliases: ["-p"]

  option CalendarAssistant::Config::Keys::Options::LOCAL_STORE,
         type: :string,
         banner: "FILENAME",
         desc: "Load events from a local file instead of Google Calendar",
         aliases: ["-l"]
end

Instance Method Details

#authorize(profile_name = nil) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/calendar_assistant/cli/commands.rb', line 129

def authorize(profile_name = nil)
  return if handle_help_args
  return help! if profile_name.nil?

  command_service.authorizer(profile_name: profile_name).authorize

  puts "\nYou're authorized!\n\n"
end

#availability(datespec = "today") ⇒ Object



254
255
256
257
258
259
# File 'lib/calendar_assistant/cli/commands.rb', line 254

def availability(datespec = "today")
  calendar_assistant(datespec) do |ca, date, out|
    event_set = ca.availability date
    out.print_available_blocks ca, event_set
  end
end

#configObject



66
67
68
69
70
71
# File 'lib/calendar_assistant/cli/commands.rb', line 66

def config
  return if handle_help_args
  settings = CalendarAssistant::CLI::Config.new.settings
  filtered_settings = settings.reject { |k, v| v.nil? }
  command_service.out.puts TomlRB.dump({ CalendarAssistant::Config::Keys::SETTINGS => filtered_settings })
end

#interactiveObject



262
263
264
265
266
# File 'lib/calendar_assistant/cli/commands.rb', line 262

def interactive
  return if handle_help_args
  require "thor_repl"
  ThorRepl.start(CalendarAssistant::CLI::Commands, history_file_path: HISTORY_FILE_PATH, prompt: Rainbow("calendar-asssistant> ").bright)
end

#join(timespec = "now") ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/calendar_assistant/cli/commands.rb', line 179

def join(timespec = "now")
  return if handle_help_args
  set_formatting
  ca = CalendarAssistant.new command_service.config, service: command_service.service
  ca.in_env do
    event_set, url = CalendarAssistant::CLI::Helpers.find_av_uri ca, timespec
    if !event_set.empty?
      command_service.out.print_events ca, event_set
      command_service.out.puts url
      command_service.out.launch url if options[CalendarAssistant::Config::Keys::Options::JOIN]
    else
      command_service.out.puts "Could not find a meeting '#{timespec}' with a video call to join."
    end
  end
end

#lint(datespec = "today") ⇒ Object



145
146
147
148
149
150
# File 'lib/calendar_assistant/cli/commands.rb', line 145

def lint(datespec = "today")
  calendar_assistant(datespec) do |ca, date, out|
    event_set = ca.lint_events date
    out.print_events ca, event_set, presenter_class: CalendarAssistant::CLI::LinterEventSetPresenter
  end
end

#location(datespec = "today") ⇒ Object



201
202
203
204
205
206
# File 'lib/calendar_assistant/cli/commands.rb', line 201

def location(datespec = "today")
  calendar_assistant(datespec) do |ca, date, out|
    event_set = ca.find_location_events date
    out.print_events ca, event_set
  end
end

#location_set(location = nil, datespec = "today") ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/calendar_assistant/cli/commands.rb', line 221

def location_set(location = nil, datespec = "today")
  return help! if location.nil?

  calendar_assistant(datespec) do |ca, date, out|
    event_set = ca.create_location_events date, location
    out.print_events ca, event_set
  end
end

#setupObject



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
# File 'lib/calendar_assistant/cli/commands.rb', line 85

def setup
  # TODO ugh see #34 for advice on how to clean this up
  return if handle_help_args
  if File.exist? CalendarAssistant::CLI::Authorizer::CREDENTIALS_PATH
    command_service.out.puts sprintf("Credentials already exist in %s",
                                     CalendarAssistant::CLI::Authorizer::CREDENTIALS_PATH)
    return
  end

  command_service.out.launch "https://developers.google.com/calendar/quickstart/ruby"
  sleep 1
  command_service.out.puts <<~EOT
                             Please click on "ENABLE THE GOOGLE CALENDAR API" and either create a new project or select an existing project.

                             (If you create a new project, name it something like "yourname-calendar-assistant" so you remember why it exists.)

                             Then click "DOWNLOAD CLIENT CONFIGURATION" to download the credentials to local disk.

                             Finally, paste the contents of the downloaded file here (it should be a complete JSON object):
                           EOT

  json = command_service.out.prompt "Paste JSON here"
  File.open(CalendarAssistant::CLI::Authorizer::CREDENTIALS_PATH, "w") do |f|
    f.write json
  end
  FileUtils.chmod 0600, CalendarAssistant::CLI::Authorizer::CREDENTIALS_PATH

  command_service.out.puts "\nOK! Your next step is to run `calendar-assistant authorize`."
end

#show(datespec = "today") ⇒ Object



163
164
165
166
167
168
# File 'lib/calendar_assistant/cli/commands.rb', line 163

def show(datespec = "today")
  calendar_assistant(datespec) do |ca, date, out|
    event_set = ca.find_events date
    out.print_events ca, event_set
  end
end

#versionObject



58
59
60
61
# File 'lib/calendar_assistant/cli/commands.rb', line 58

def version
  return if handle_help_args
  command_service.out.puts CalendarAssistant::VERSION
end