Permalink
Browse files

get a list of your projects from command line

  • Loading branch information...
1 parent db61cc8 commit f41ac1d5db489ad05651d9b923c29ce291bbccd8 @shime shime committed Oct 8, 2012
Showing with 135 additions and 21 deletions.
  1. +135 −21 bin/airbrake
View
@@ -1,24 +1,146 @@
#!/usr/bin/env ruby
require "airbrake"
+require "net/http"
+require "uri"
+
+class Project
+ # TODO should probably be moved
+ #
+ attr_writer :name, :id, :api_key
+
+ def to_s
+ "#{@name}".rjust(20) + "(#{@id}):".rjust(10) + " #{@api_key}"
+ end
+
+ def valid?
+ @name && @id && @api_key
+ end
+end
+
+class ProjectFactory
+ # TODO should probably be moved
+ #
+ def initialize
+ @project = Project.new
+ @projects = []
+ end
+
+ def project
+ @project
+ end
+
+ def create_projects_from_xml(xml)
+ xml.split("\n").each do |line|
+ /<name[^>]*>(?<name>.*?)<\/name>/ =~ line
+ project.name = name.capitalize if name
+ /<id[^>]*>(?<id>.*?)<\/id>/ =~ line
+ project.id = id if id
+ /<api-key[^>]*>(?<api_key>.*?)<\/api-key>/ =~ line
+ project.api_key = api_key if api_key
+ check_project
+ end
+ end
+
+ def check_project
+ if @project.valid?
+ projects << @project
+ @project = Project.new
+ end
+ end
+
+ def projects
+ @projects
+ end
+end
def parse_options(array = [])
opts = Hash[*array]
- opts[:error] = opts.delete("-e") || opts.delete("--error") { RuntimeError }
- opts[:message] = opts.delete("-m") || opts.delete("--message") { "I've made a huge mistake" }
- opts[:api_key] = opts.delete("-k") || opts.delete("--api-key")
- opts[:host] = opts.delete("-h") || opts.delete("--host")
- opts[:port] = opts.delete("-p") || opts.delete("--port")
+ opts[:error] = opts.delete("-e") || opts.delete("--error") { RuntimeError }
+ opts[:message] = opts.delete("-m") || opts.delete("--message") { "I've made a huge mistake" }
+ opts[:api_key] = opts.delete("-k") || opts.delete("--api-key") || config_from_file.api_key || ENV["AIRBRAKE_API_KEY"]
+ opts[:host] = opts.delete("-h") || opts.delete("--host") || config_from_file.host
+ opts[:port] = opts.delete("-p") || opts.delete("--port") || config_from_file.port
+ opts[:auth_token] = opts.delete("-t") || opts.delete("--auth-token") || ENV["AIRBRAKE_AUTH_TOKEN"]
+ opts[:account] = opts.delete("-a") || opts.delete("--account") || ENV["AIRBRAKE_ACCOUNT"]
opts
end
+def config_from_file
+ begin
+ load "config/initializers/airbrake.rb"
+ rescue LoadError
+ end
+ Airbrake.configuration
+end
+
+def print_usage
+ puts <<USAGE
+Usage: airbrake [COMMAND] [OPTION]...
+Commands:
+ raise # Raise an exception specified by ERROR and MESSAGE.
+ list # List all the projects for given AUTH_TOKEN and ACCOUNT.
+
+Options:
+ -e, [--error=ERROR] # Error class to raise. Default: RuntimeError
+ -m, [--message=MESSAGE] # Error message. Default: "I've made a huge mistake"
+ -k, [--api-key=API_KEY] # Api key of your Airbrake application.
+ -h, [--host=HOST] # URL of the Airbrake API server. Default: api.airbrake.io
+ -p, [--port=PORT] # Port of the Airbrake API server. Default: 80
+ -t, [--auth-token=AUTH_TOKEN] # The auth token used for API requests.
+ -a, [--account=ACCOUNT] # The account used for API requests.
+ -h, [--help] # Show this usage
+USAGE
+end
+
+def check_api_key
+ abort "You didn't provide API_KEY so nothing was raised."\
+ " Refer to usage for more info (airbrake --help)." unless options[:api_key]
+end
+
+def check_account
+ abort "You didn't provide ACCOUNT "\
+ "so no API request was made." unless options[:account]
+end
+
+def check_auth_token
+ abort "You didn't provide AUTH_TOKEN "\
+ "so no API request was made." unless options[:auth_token]
+end
+
+def fetch_projects
+ uri = URI.parse "http://#{options[:account]}.airbrake.io"\
+ "/data_api/v1/projects.xml?auth_token=#{options[:auth_token]}"
+ http = Net::HTTP.new(uri.host,uri.port)
+ request = Net::HTTP::Get.new(uri.request_uri)
+ response = http.request(request)
+ response.body
+end
+
+def print_projects
+ factory = ProjectFactory.new
+ factory.create_projects_from_xml(fetch_projects)
+ puts "\nProjects\n" + "".rjust(63,"#")
+ factory.projects.each do |project|
+ puts project
+ end
+ puts
+end
+
+def options=(options)
+ @options = options
+end
+
+def options
+ @options
+end
+
args = ARGV.dup
command = args.shift.strip rescue nil
+self.options = parse_options(ARGV[1..-1])
case command
when 'raise'
- options = parse_options(ARGV[1..-1])
- abort "You didn't provide API_KEY so nothing was raised."\
- " Refer to usage for more info (airbrake --help)." unless options[:api_key]
+ check_api_key
Airbrake.configure do |c|
c.api_key = options[:api_key]
c.host = options[:host] if options[:host]
@@ -29,18 +151,10 @@ case command
:cgi_data => ENV)
abort "Error sending exception to Airbrake server. Try again later." unless exception_id
puts "Exception sent successfully: http://airbrake.io/locate/#{exception_id}"
+ when "list"
+ check_auth_token
+ check_account
+ print_projects
else
- puts <<USAGE
-Usage: airbrake [COMMAND] [OPTION]...
-Commands:
- raise # Raise an exception specified by ERROR and MESSAGE.
-
-Options:
- -e, [--error=ERROR] # Error class to raise. Default: RuntimeError
- -m, [--message=MESSAGE] # Error message. Default: "I've made a huge mistake"
- -k, [--api-key=API_KEY] # Api key of your Airbrake application.
- -h, [--host=HOST] # URL of the Airbrake API server. Default: api.airbrake.io
- -p, [--port=PORT] # Port of the Airbrake API server. Default: 80
- -h, [--help] # Show this usage
-USAGE
+ print_usage
end

0 comments on commit f41ac1d

Please sign in to comment.