Class: Codebase::Command
- Inherits:
-
Object
- Object
- Codebase::Command
- Defined in:
- lib/codebase/command.rb
Instance Method Summary collapse
- #api(path, data = nil) ⇒ Object
- #api_request(url, username, password, data = nil) ⇒ Object
- #api_username ⇒ Object
- #apikey ⇒ Object
- #call(options, *args) ⇒ Object
- #configured? ⇒ Boolean
- #domain ⇒ Object
- #execute_commands(array) ⇒ Object
- #git_config_variable(name) ⇒ Object
- #in_repository? ⇒ Boolean
-
#initialize(block) ⇒ Command
constructor
A new instance of Command.
- #new_username? ⇒ Boolean
- #options ⇒ Object
- #repository_properties ⇒ Object
- #repository_status ⇒ Object
- #use_hirb ⇒ Object
- #username ⇒ Object
Constructor Details
#initialize(block) ⇒ Command
4 5 6 |
# File 'lib/codebase/command.rb', line 4 def initialize(block) (class << self;self end).send :define_method, :command, &block end |
Instance Method Details
#api(path, data = nil) ⇒ Object
134 135 136 |
# File 'lib/codebase/command.rb', line 134 def api(path, data = nil) api_request("http://api3.codebasehq.com/#{path}", api_username, apikey, data) end |
#api_request(url, username, password, data = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/codebase/command.rb', line 96 def api_request(url, username, password, data = nil) require 'uri' require 'net/http' require 'net/https' uri = URI.parse(url) if data req = Net::HTTP::Post.new(uri.path) else req = Net::HTTP::Get.new(uri.path) end req.basic_auth(username, password) req.add_field("Accept", "application/json") req.add_field("Content-type", "application/json") res = Net::HTTP.new(uri.host, uri.port) if url.include?('https://') res.use_ssl = true res.verify_mode = OpenSSL::SSL::VERIFY_NONE end res = res.request(req, data) case res when Net::HTTPSuccess return res.body when Net::HTTPFound if res.body =~ /http:\/\/api3.codebasehq.com/ && url.include?('https://') puts "Falling back to non SSL" api_request(url.gsub("https://", "http://"), username, password) end when Net::HTTPServiceUnavailable puts "The API is currently unavailable. Please check your codebase account has been enabled for API access." Process.exit(1) when Net::HTTPForbidden, Net:: puts "Access Denied. Ensure you have correctly configured your local Gem installation using the 'cb setup' command." Process.exit(1) else return false end end |
#api_username ⇒ Object
45 46 47 |
# File 'lib/codebase/command.rb', line 45 def api_username @api_username = "#{domain.split('.').first}/#{username}" end |
#apikey ⇒ Object
49 50 51 |
# File 'lib/codebase/command.rb', line 49 def apikey @apikey ||= git_config_variable(:apikey) end |
#call(options, *args) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/codebase/command.rb', line 8 def call(, *args) = arity = method(:command).arity args << nil while args.size < arity send :command, *args end |
#configured? ⇒ Boolean
29 30 31 |
# File 'lib/codebase/command.rb', line 29 def configured? git_config_variable(:domain) && git_config_variable(:username) && apikey end |
#domain ⇒ Object
37 38 39 |
# File 'lib/codebase/command.rb', line 37 def domain @domain ||= new_username? ? "#{new_username?[1]}.codebasehq.com" : git_config_variable(:domain) end |
#execute_commands(array) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/codebase/command.rb', line 72 def execute_commands(array) for command in array puts "\e[44;33m" + command + "\e[0m" exit_code = 0 IO.popen(command) do |f| output = f.read exit_code = Process.waitpid2(f.pid)[1] end if exit_code != 0 $stderr.puts "An error occured running: #{command}" Process.exit(1) end end end |
#git_config_variable(name) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/codebase/command.rb', line 87 def git_config_variable(name) if name.is_a?(Symbol) r = `git config codebase.#{name.to_s}`.chomp else r = `git config #{name.to_s}`.chomp end r.empty? ? nil : r end |
#in_repository? ⇒ Boolean
53 54 55 |
# File 'lib/codebase/command.rb', line 53 def in_repository? repository_status != :false end |
#new_username? ⇒ Boolean
33 34 35 |
# File 'lib/codebase/command.rb', line 33 def new_username? git_config_variable(:username).match(/(.+)\/(.+)/) end |
#options ⇒ Object
25 26 27 |
# File 'lib/codebase/command.rb', line 25 def || Hash.new end |
#repository_properties ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/codebase/command.rb', line 57 def repository_properties return false unless in_repository? origin_name = (git_config_variable(:remote) || 'origin') remote_url = git_config_variable("remote.#{origin_name}.url") if remote_url =~ /git\@(gitbase|codebasehq|cbhqdev)\.com:(.*)\/(.*)\/(.*)\.git/ {:domain => $1, :account => $2, :project => $3, :repository => $4} else raise Codebase::Error, "Invalid Codebase repository (#{remote_url})" end end |
#repository_status ⇒ Object
68 69 70 |
# File 'lib/codebase/command.rb', line 68 def repository_status @in_repository ||= (`git branch 2> /dev/null` && $?.success? ? true : :false) end |
#use_hirb ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/codebase/command.rb', line 15 def use_hirb begin require 'hirb' extend Hirb::Console rescue LoadError puts "Hirb is not installed. Install hirb using '[sudo] gem install hirb' to get cool ASCII tables" Process.exit(1) end end |
#username ⇒ Object
41 42 43 |
# File 'lib/codebase/command.rb', line 41 def username @username ||= new_username? ? new_username?[2] : git_config_variable(:username) end |