lib/aocli/cli.rb in aocli-1.4.5 vs lib/aocli/cli.rb in aocli-1.5.0
- old
+ new
@@ -1,55 +1,64 @@
+require "byebug"
-require"byebug"
module Aocli
class Cli
def initialize
- @options = {cookie: Aocli::CookieRetriever.call}
+ @options = Aocli::Config.load_config
@prompt = TTY::Prompt.new
end
def run
prompt_for_command
- get_all_command_options
+ prompt_command_options
# Aocli::Commands::StartDay
# Aocli::Commands::SaveCookie
+ # Aocli::Commands::RemoveCookie
+ # Aocli::Commands::SetTemplatePath
+ # Aocli::Commands::RemoveTemplatePath
options[:command].constantize.run!(options)
end
private
+
attr_accessor :options, :prompt
attr_reader :cookie
def prompt_for_command
options[:command] = prompt.select("What would you like to do?", command_options)
end
- def get_all_command_options
+ def prompt_command_options
case options[:command]
when "Aocli::Commands::StartDay"
set_start_day_options
when "Aocli::Commands::SaveCookie"
set_save_cookie_options
+ when "Aocli::Commands::SetTemplatePath"
+ set_template_path_options
+ when "Aocli::Commands::RemoveCookie", "Aocli::Commands::RemoveTemplatePath"
+ nil
when "Exit"
exit
else
raise NotImplementedError, "Can't handle that command: #{command_klass}"
end
end
def command_options
@command_options ||= [
+ requires_config(:cookie, {name: "Start day", value: "Aocli::Commands::StartDay"}),
{name: "Save Cookie", value: "Aocli::Commands::SaveCookie"},
- requires_cookie({name: "Start day", value: "Aocli::Commands::StartDay"}),
+ requires_config(:cookie, {name: "Unset cookie", value: "Aocli::Commands::RemoveCookie"}),
+ {name: set_template_prompt, value: "Aocli::Commands::SetTemplatePath"},
+ requires_config(:template_path, {name: "Unset template file", value: "Aocli::Commands::RemoveTemplatePath"}),
{name: "Exit", value: "Exit"},
]
- # TODO:
- # requires_cookie({name: "Remove cookie file", value: "Aocli::Commands::RemoveCookieFile"}),
end
- def requires_cookie(hash)
- hash.merge(options[:cookie] ? {} : {disabled: "(No cookie has been set yet)"})
+ def requires_config(required_config, hash)
+ hash.merge(options[required_config] ? {} : {disabled: "(No #{required_config} has been set yet)"})
end
def set_start_day_options
year = prompt.select("Which Year?", Aocli::Values::START_DAY_YEARS.reverse)
day = prompt.select("Which Day?", Aocli::Values::START_DAY_DAYS)
@@ -57,8 +66,20 @@
options[:output_destination] = "./"
end
def set_save_cookie_options
options[:cookie] = prompt.ask("Paste your cookie and it will be saved:")
+ end
+
+ def set_template_path_options
+ options[:template_path] = prompt.ask("Paste the full file path to your template file:")
+ end
+
+ def set_template_prompt
+ base_prompt = "Set Template File"
+ if options[:template_path]
+ base_prompt += " -> Currently set to '#{options[:template_path]}'"
+ end
+ base_prompt
end
end
end