diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..981af46 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,20 @@ +--- +engines: + duplication: + enabled: true + config: + languages: + - ruby + + fixme: + enabled: true + + rubocop: + enabled: true + +ratings: + paths: + - "**.rb" + +exclude_paths: +- lib/papertrail/okjson.rb diff --git a/Rakefile b/Rakefile index 345f08f..7df74ef 100644 --- a/Rakefile +++ b/Rakefile @@ -115,7 +115,7 @@ desc "Generate #{gemspec_file}" task :gemspec => :validate do # read spec file and split out manifest section spec = File.read(gemspec_file) - head, manifest, tail = spec.split(" # = MANIFEST =\n") + head, _manifest, tail = spec.split(" # = MANIFEST =\n") # replace name version and date replace_header(head, :name) diff --git a/lib/papertrail/cli.rb b/lib/papertrail/cli.rb index 70530d2..6b08fc4 100644 --- a/lib/papertrail/cli.rb +++ b/lib/papertrail/cli.rb @@ -29,7 +29,7 @@ def initialize end def run - if configfile = find_configfile + if configfile configfile_options = load_configfile(configfile) options.merge!(configfile_options) end @@ -38,11 +38,11 @@ def run opts.banner = "papertrail - command-line tail and search for Papertrail log management service" opts.version = Papertrail::VERSION - opts.on("-h", "--help", "Show usage") do |v| + opts.on("-h", "--help", "Show usage") do puts opts exit end - opts.on("-f", "--follow", "Continue running and printing new events (off)") do |v| + opts.on("-f", "--follow", "Continue running and printing new events (off)") do options[:follow] = true end opts.on("--min-time MIN", "Earliest time to search from") do |v| @@ -66,18 +66,18 @@ def run opts.on("-s", "--system SYSTEM", "System to search") do |v| options[:system] = v end - opts.on("-j", "--json", "Output raw JSON data (off)") do |v| + opts.on("-j", "--json", "Output raw JSON data (off)") do options[:json] = true end - opts.on("--color [program|system|all|off]", - [:program, :system, :all, :off], - "Attribute(s) to colorize based on (program)") do |v| + opts.on("--color [program|system|all|off]", + [:program, :system, :all, :off], "Attribute(s) to colorize based on (program)") do |v| + options[:color] = v end - opts.on("--force-color", "Force use of ANSI color characters even on non-tty outputs (off)") do |v| + opts.on("--force-color", "Force use of ANSI color characters even on non-tty outputs (off)") do options[:force_color] = true end - opts.on("-V", "--version", "Display the version and exit") do |v| + opts.on("-V", "--version", "Display the version and exit") do puts "papertrail version #{Papertrail::VERSION}" exit end diff --git a/lib/papertrail/cli_add_group.rb b/lib/papertrail/cli_add_group.rb index 2db8ad9..da69a8d 100644 --- a/lib/papertrail/cli_add_group.rb +++ b/lib/papertrail/cli_add_group.rb @@ -14,7 +14,7 @@ def run :token => ENV['PAPERTRAIL_API_TOKEN'], } - if configfile = find_configfile + if configfile configfile_options = load_configfile(configfile) options.merge!(configfile_options) end @@ -22,7 +22,7 @@ def run OptionParser.new do |opts| opts.banner = "papertrail-add-group" - opts.on("-h", "--help", "Show usage") do |v| + opts.on("-h", "--help", "Show usage") do puts opts exit end diff --git a/lib/papertrail/cli_add_system.rb b/lib/papertrail/cli_add_system.rb index ae0a9f9..7b541d2 100644 --- a/lib/papertrail/cli_add_system.rb +++ b/lib/papertrail/cli_add_system.rb @@ -16,7 +16,7 @@ def run :token => ENV['PAPERTRAIL_API_TOKEN'], } - if configfile = find_configfile + if configfile configfile_options = load_configfile(configfile) options.merge!(configfile_options) end @@ -59,7 +59,7 @@ def run opts.separator '' opts.separator "Common options:" - opts.on("-h", "--help", "Show usage") do |v| + opts.on("-h", "--help", "Show usage") do puts opts exit end diff --git a/lib/papertrail/cli_helpers.rb b/lib/papertrail/cli_helpers.rb index b346e43..52032ae 100644 --- a/lib/papertrail/cli_helpers.rb +++ b/lib/papertrail/cli_helpers.rb @@ -1,10 +1,14 @@ module Papertrail module CliHelpers + def configfile + @configfile ||= find_configfile + end + def find_configfile - if File.exists?(path = File.expand_path('.papertrail.yml')) + if File.exist?(path = File.expand_path('.papertrail.yml')) return path end - if File.exists?(path = File.expand_path('~/.papertrail.yml')) + if File.exist?(path = File.expand_path('~/.papertrail.yml')) return path end rescue ArgumentError diff --git a/lib/papertrail/cli_join_group.rb b/lib/papertrail/cli_join_group.rb index a413f2a..0a5fbc4 100644 --- a/lib/papertrail/cli_join_group.rb +++ b/lib/papertrail/cli_join_group.rb @@ -14,7 +14,7 @@ def run :token => ENV['PAPERTRAIL_API_TOKEN'], } - if configfile = find_configfile + if configfile configfile_options = load_configfile(configfile) options.merge!(configfile_options) end diff --git a/lib/papertrail/cli_leave_group.rb b/lib/papertrail/cli_leave_group.rb index f78ae9b..e311ea9 100644 --- a/lib/papertrail/cli_leave_group.rb +++ b/lib/papertrail/cli_leave_group.rb @@ -14,7 +14,7 @@ def run :token => ENV['PAPERTRAIL_API_TOKEN'], } - if configfile = find_configfile + if configfile configfile_options = load_configfile(configfile) options.merge!(configfile_options) end diff --git a/lib/papertrail/cli_remove_system.rb b/lib/papertrail/cli_remove_system.rb index e251409..b8e5e57 100644 --- a/lib/papertrail/cli_remove_system.rb +++ b/lib/papertrail/cli_remove_system.rb @@ -14,7 +14,7 @@ def run :token => ENV['PAPERTRAIL_API_TOKEN'], } - if configfile = find_configfile + if configfile configfile_options = load_configfile(configfile) options.merge!(configfile_options) end diff --git a/lib/papertrail/http_client.rb b/lib/papertrail/http_client.rb index 3618ca4..17f89fc 100644 --- a/lib/papertrail/http_client.rb +++ b/lib/papertrail/http_client.rb @@ -42,47 +42,27 @@ def get(path, params = {}) if params.size > 0 path = "#{path}?#{build_nested_query(params)}" end - attempts = 0 - begin + + attempt_http wait: 5.0 do on_complete(https.get(request_uri(path), @headers)) - rescue SystemCallError, Net::HTTPFatalError => e - sleep 5.0 - attempts += 1 - retry if (attempts < 3) - raise e end end def put(path, params) - attempts = 0 - begin + attempt_http do on_complete(https.put(request_uri(path), build_nested_query(params), @headers)) - rescue SystemCallError, Net::HTTPFatalError => e - attempts += 1 - retry if (attempts < 3) - raise e end end def post(path, params) - attempts = 0 - begin + attempt_http do on_complete(https.post(request_uri(path), build_nested_query(params), @headers)) - rescue SystemCallError, Net::HTTPFatalError => e - attempts += 1 - retry if (attempts < 3) - raise e end end def delete(path) - attempts = 0 - begin + attempt_http do on_complete(https.delete(request_uri(path), @headers)) - rescue SystemCallError, Net::HTTPFatalError => e - attempts += 1 - retry if (attempts < 3) - raise e end end @@ -156,6 +136,21 @@ def escape(s) '%' + $&.unpack('H2' * $&.bytesize).join('%').upcase }.tr(' ', '+') end + + def attempt_http(options={}) + wait = options[:wait] + + attempts = 0 + begin + yield + rescue SystemCallError, Net::HTTPFatalError => e + sleep wait if wait + attempts += 1 + retry if (attempts < 3) + raise e + end + end + end end