Skip to content

Commit

Permalink
prefactoring cli.rb
Browse files Browse the repository at this point in the history
notably making the :profile option an instance option (was previously
a class option) and providing a helper method to apply it.

this is because the auth and config commands shouldn't document the :profile
option, and we're about to add a second auth command.
  • Loading branch information
flavorjones committed Oct 30, 2018
1 parent ee5bbcf commit 30f27d5
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions lib/calendar_assistant/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@

class CalendarAssistant
class CLI < Thor
def self.supports_profile_option
option :profile,
type: :string,
desc: "the profile you'd like to use (if different from default)",
aliases: ["-p"]
end

default_config = CalendarAssistant::Config.new options: options # used in option descriptions

# it's unfortunate that thor does not support this usage of help args
class_option :help,
type: :boolean,
aliases: ["-h", "-?"]

# note that these options are passed straight through to CLIHelpers.print_events
class_option :profile,
type: :string,
desc: "the profile you'd like to use (if different from default)",
aliases: ["-p"]
class_option :debug,
type: :boolean,
desc: "how dare you suggest there are bugs"


desc "config",
"Dump your configuration parameters (merge of defaults and overrides from #{CalendarAssistant::Config::CONFIG_FILE_PATH})"
def config
return if handle_help_args
config = CalendarAssistant::Config.new
settings = {}
setting_names = CalendarAssistant::Config::Keys::Settings.constants.map { |k| CalendarAssistant::Config::Keys::Settings.const_get k }
setting_names.each do |key|
settings[key] = config.setting key
end
puts TOML::Generator.new({CalendarAssistant::Config::Keys::SETTINGS => settings}).body
end


desc "authorize PROFILE_NAME",
"create (or validate) a profile named NAME with calendar access"
long_desc <<~EOD
Expand Down Expand Up @@ -57,6 +71,7 @@ def authorize profile_name
type: :boolean,
desc: "only show events that you've accepted with another person",
aliases: ["-c"]
supports_profile_option
def show datespec="today"
return if handle_help_args
config = CalendarAssistant::Config.new options: options
Expand All @@ -71,6 +86,7 @@ def show datespec="today"
option :join,
type: :boolean, default: true,
desc: "launch a browser to join the video call URL"
supports_profile_option
def join timespec="now"
return if handle_help_args
config = CalendarAssistant::Config.new options: options
Expand All @@ -90,6 +106,7 @@ def join timespec="now"

desc "location [DATE | DATERANGE]",
"Show your location for a date or range of dates (default 'today')"
supports_profile_option
def location datespec="today"
return if handle_help_args
config = CalendarAssistant::Config.new options: options
Expand All @@ -101,6 +118,7 @@ def location datespec="today"

desc "location-set LOCATION [DATE | DATERANGE]",
"Set your location to LOCATION for a date or range of dates (default 'today')"
supports_profile_option
def location_set location, datespec="today"
return if handle_help_args
config = CalendarAssistant::Config.new options: options
Expand All @@ -109,6 +127,7 @@ def location_set location, datespec="today"
CLIHelpers::Out.new.print_events ca, events, options
end


desc "availability [DATE | DATERANGE | TIMERANGE]",
"Show your availability for a date or range of dates (default 'today')"
option CalendarAssistant::Config::Keys::Settings::MEETING_LENGTH,
Expand All @@ -129,6 +148,7 @@ def location_set location, datespec="today"
desc: sprintf("[default %s] find chunks of available time before TIME (which is a Chronic string like '9am' or '14:30')",
default_config.setting(CalendarAssistant::Config::Keys::Settings::END_OF_DAY)),
aliases: ["-e"]
supports_profile_option
def availability datespec="today"
return if handle_help_args
config = CalendarAssistant::Config.new options: options
Expand All @@ -137,19 +157,6 @@ def availability datespec="today"
CLIHelpers::Out.new.print_available_blocks ca, events, options
end

desc "config",
"Dump your configuration parameters (merge of defaults and overrides from #{CalendarAssistant::Config::CONFIG_FILE_PATH})"
def config
return if handle_help_args
config = CalendarAssistant::Config.new
settings = {}
setting_names = CalendarAssistant::Config::Keys::Settings.constants.map { |k| CalendarAssistant::Config::Keys::Settings.const_get k }
setting_names.each do |key|
settings[key] = config.setting key
end
puts TOML::Generator.new({CalendarAssistant::Config::Keys::SETTINGS => settings}).body
end

private

def handle_help_args
Expand Down

0 comments on commit 30f27d5

Please sign in to comment.