introduce the api concern for commands + create a new site (if none) when pushing (wip)
did
committed Apr 12, 2015
commit 438adff2e865720a1322f970a5bc4a377602af2f
Showing 5
changed files with
86 additions
and 31 deletions
locomotive/wagon/commands/authenticate_command.rb b/lib/locomotive/wagon/commands/authenticate_command.rb
+14
-11
| @@ | @@ -1,10 +1,13 @@ |
| - | require 'locomotive/coal' |
| require 'netrc' | |
| + | require_relative 'concerns/api_concern' |
| + | |
| module Locomotive::Wagon | |
| class AuthenticateCommand < Struct.new(:platform_url, :email, :password, :shell) | |
| + | include ApiConcern |
| + | |
| def self.authenticate(platform_url, email, password, shell) | |
| self.new(platform_url, email, password, shell).authenticate | |
| end | |
| @@ | @@ -38,7 +41,7 @@ module Locomotive::Wagon |
| name = shell.ask 'What is your name?' | |
| begin | |
| - | account = client.my_account.create(name: name, email: email, password: password) |
| + | account = api_client.my_account.create(name: name, email: email, password: password) |
| shell.say "Your account has been successfully created.", :green | |
| account.api_key | |
| rescue Locomotive::Coal::Error => e | |
| @@ | @@ -60,21 +63,21 @@ module Locomotive::Wagon |
| def my_account | |
| begin | |
| - | client.my_account.get |
| + | api_client.my_account.get |
| rescue Locomotive::Coal::UnauthorizedError | |
| nil | |
| end | |
| end | |
| - | def client |
| - | @client ||= Locomotive::Coal::Client.new(api_url, email: email, password: password) |
| - | end |
| + | # def client |
| + | # @client ||= Locomotive::Coal::Client.new(api_url, email: email, password: password) |
| + | # end |
| - | def api_url |
| - | uri = URI(platform_url) |
| - | uri.merge!('/locomotive/api/v3') if uri.path == '/' || uri.path == '' |
| - | uri.to_s |
| - | end |
| + | # def api_url |
| + | # uri = URI(platform_url) |
| + | # uri.merge!('/locomotive/api/v3') if uri.path == '/' || uri.path == '' |
| + | # uri.to_s |
| + | # end |
| end | |
locomotive/wagon/commands/concerns/api_concern.rb b/lib/locomotive/wagon/commands/concerns/api_concern.rb
+29
-0
| @@ | @@ -0,0 +1,29 @@ |
| + | require 'locomotive/coal' |
| + | |
| + | module Locomotive::Wagon |
| + | |
| + | module ApiConcern |
| + | |
| + | private |
| + | |
| + | def api_client |
| + | @api_client ||= Locomotive::Coal::Client.new(api_url, api_credentials) |
| + | end |
| + | |
| + | def api_credentials |
| + | if respond_to?(:email) |
| + | { email: email, password: password } |
| + | elsif respond_to?(:credentials) |
| + | credentials |
| + | end |
| + | end |
| + | |
| + | def api_url |
| + | uri = URI(platform_url) |
| + | uri.merge!('/locomotive/api/v3') if uri.path == '/' || uri.path == '' |
| + | uri.to_s |
| + | end |
| + | |
| + | end |
| + | |
| + | end |
locomotive/wagon/commands/push_command.rb b/lib/locomotive/wagon/commands/push_command.rb
+33
-9
| @@ | @@ -1,11 +1,20 @@ |
| - | require 'locomotive/coal' |
| require 'locomotive/steam' | |
| require 'netrc' | |
| + | require_relative 'concerns/api_concern' |
| + | |
| module Locomotive::Wagon | |
| class PushCommand < Struct.new(:env, :path, :options) | |
| + | include ApiConcern |
| + | |
| + | attr_accessor :platform_url, :credentials |
| + | |
| + | def self.push(env, path, options) |
| + | self.new(env, path, options).push |
| + | end |
| + | |
| def push | |
| puts connection_information.inspect | |
| true | |
| @@ | @@ -17,22 +26,24 @@ module Locomotive::Wagon |
| information | |
| else | |
| # 1. ask for the platform URL (or LOCOMOTIVE_PLATFORM_URL env variable) [DONE] | |
| - | platform_url = ask_for_platform_url |
| + | self.platform_url = ask_for_platform_url |
| # 2. retrieve email + api_key. If no entry present in the .netrc, raise an error [DONE] | |
| - | credentials = read_from_netrc(platform_url) |
| + | self.credentials = read_from_netrc(self.platform_url) |
| - | raise 'You need to run wagon authenticate before going further' if credentials.nil? |
| + | raise 'You need to run wagon authenticate before going further' if self.credentials.nil? |
| # 3. get an instance of the Steam services in order to load the information about the site (SiteRepository) | |
| site = steam_services.current_site | |
| # 4. ask for a handle if not found (blank: random one) | |
| - | handle = site[:handle] || ask_for_the_site_handle |
| + | site[:handle] ||= ask_for_the_site_handle |
| + | |
| + | # 5. create the site |
| + | create_remote_site(site) |
| - | puts handle |
| + | 'TODO' |
| - | # 5. create the site [] |
| # 6. update the deploy.yml | |
| end | |
| @@ | @@ -43,16 +54,25 @@ module Locomotive::Wagon |
| private | |
| + | def create_remote_site(site) |
| + | api_client.sites.create(site.attributes).tap do |_site| |
| + | puts _site.inspect |
| + | end |
| + | end |
| + | |
| def ask_for_platform_url | |
| default = ENV['LOCOMOTIVE_PLATFORM_URL'] || DEFAULT_PLATFORM_URL | |
| - | url = shell.ask "Enter the URL of your platform (default: #{default})" |
| + | url = shell.ask "What is the URL of your platform? (default: #{default})" |
| url.blank? ? default : url | |
| end | |
| def ask_for_the_site_handle | |
| - | shell.ask "Enter the handle of your site (default: random one)" |
| + | handle = nil |
| + | while handle.nil? |
| + | handle = shell.ask "What is the handle of your site?" |
| + | end |
| end | |
| def read_from_yaml_file | |
| @@ | @@ -91,6 +111,10 @@ module Locomotive::Wagon |
| end | |
| end | |
| + | def client |
| + | @client ||= Locomotive::Coal::Client.new(api_url, credentials) |
| + | end |
| + | |
| def deploy_file | |
| File.join(path, 'config', 'deploy.yml') | |
| end | |
spec/integration/commands/push_command_spec.rb
+1
-2
| @@ | @@ -9,7 +9,6 @@ describe Locomotive::Wagon::PushCommand do |
| # before { VCR.insert_cassette 'push', record: :new_episodes, match_requests_on: [:method, :query, :body] } | |
| # after { VCR.eject_cassette } | |
| - | # let(:platform_url) { TEST_PLATFORM_URL } |
| let(:env) { 'production' } | |
| let(:path) { default_site_path } | |
| let(:shell) { Thor::Shell::Color.new } | |
| @@ | @@ -27,7 +26,7 @@ describe Locomotive::Wagon::PushCommand do |
| before do | |
| allow(Netrc).to receive(:read).and_return(TEST_PLATFORM_ALT_URL => credentials) | |
| - | allow(Thor::LineEditor).to receive(:readline).and_return(TEST_PLATFORM_URL.dup) |
| + | allow(Thor::LineEditor).to receive(:readline).and_return(TEST_PLATFORM_URL.dup, 'acme') |
| end | |
| it { is_expected.to eq true } | |
spec/support/vcr.rb
+9
-9
| @@ | @@ -1,10 +1,10 @@ |
| - | # require 'webmock/rspec' |
| - | # require 'vcr' |
| + | require 'webmock/rspec' |
| + | require 'vcr' |
| - | # # VCR config |
| - | # VCR.configure do |c| |
| - | # c.cassette_library_dir = 'spec/fixtures/cassettes' |
| - | # c.hook_into :webmock |
| - | # c.ignore_hosts 'codeclimate.com' |
| - | # c.configure_rspec_metadata! |
| - | # end |
| + | # VCR config |
| + | VCR.configure do |c| |
| + | c.cassette_library_dir = 'spec/fixtures/cassettes' |
| + | c.hook_into :webmock |
| + | c.ignore_hosts 'codeclimate.com' |
| + | c.configure_rspec_metadata! |
| + | end |