Skip to content

Commit

Permalink
introduce setup command and centralize client cred storage
Browse files Browse the repository at this point in the history
related to #29
  • Loading branch information
flavorjones committed Oct 30, 2018
1 parent 30f27d5 commit 172ea98
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/calendar_assistant/authorizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class UnauthorizedError < CalendarAssistant::BaseException ; end

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = "Flavorjones Calendar Assistant".freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
CREDENTIALS_PATH = File.join ENV["HOME"], ".calendar-assistant.client"
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR

attr_reader :profile_name, :config_token_store
Expand Down Expand Up @@ -77,9 +77,10 @@ def prompt_user_for_authorization
def authorizer
@authorizer ||= begin
if ! File.exists?(CREDENTIALS_PATH)
raise NoCredentials, "No credentials found. Please run `calendar-assistant help authorize` for help"
raise NoCredentials, "No credentials found. Please run `calendar-assistant help setup` for instructions"
end

FileUtils.chmod 0600, CREDENTIALS_PATH
client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
Google::Auth::UserAuthorizer.new(client_id, SCOPE, config_token_store)
end
Expand Down
41 changes: 41 additions & 0 deletions lib/calendar_assistant/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ def config
end


desc "setup",
"Link your local calendar-assistant installation to a Google API Client"
long_desc <<~EOD
This command will walk you through setting up a Google Cloud
Project, enabling the Google Calendar API, and saving the
credentials necessary to access the API on behalf of users.
If you already have downloaded client credentials, you don't
need to run this command. Instead, rename the downloaded JSON
file to `#{CalendarAssistant::Authorizer::CREDENTIALS_PATH}`
EOD
def setup
out = CLIHelpers::Out.new
if File.exist? CalendarAssistant::Authorizer::CREDENTIALS_PATH
out.puts sprintf("Credentials already exist in %s",
CalendarAssistant::Authorizer::CREDENTIALS_PATH)
exit 0
end

out.launch "https://developers.google.com/calendar/quickstart/ruby"
sleep 1
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 = out.prompt "Paste JSON here"
File.open(CalendarAssistant::Authorizer::CREDENTIALS_PATH, "w") do |f|
f.write json
end
FileUtils.chmod 0600, CalendarAssistant::Authorizer::CREDENTIALS_PATH

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


desc "authorize PROFILE_NAME",
"create (or validate) a profile named NAME with calendar access"
long_desc <<~EOD
Expand Down
16 changes: 16 additions & 0 deletions lib/calendar_assistant/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ def puts *args
io.puts(*args)
end

def prompt query, default=nil
loop do
message = query
message += " [#{default}]" if default
message += ": "
print Rainbow(message).bold
answer = STDIN.gets.chomp.strip
if answer.empty?
return default if default
puts Rainbow("Please provide an answer.").red
else
return answer
end
end
end

def print_now! ca, event, printed_now
return true if printed_now
return false if event.start_date != Date.today
Expand Down
4 changes: 4 additions & 0 deletions spec/calendar_assistant/cli_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@
end
end

describe "#prompt" do
it "should have tests"
end

describe "#print_now!" do
freeze_time

Expand Down

0 comments on commit 172ea98

Please sign in to comment.