diff --git a/Gemfile b/Gemfile index 40e9aa6..9156c99 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,5 @@ source "https://rubygems.org" -gem "colorize" - group :development, :test do gem "byebug" gem "rspec" diff --git a/lib/hound.rb b/lib/hound.rb index e637d87..fc533a6 100644 --- a/lib/hound.rb +++ b/lib/hound.rb @@ -1,10 +1,10 @@ -require "colorize" require "delegate" require "json" require "yaml" require "hound/version" require "hound/config" +require "hound/format" require "hound/linters/base" require "hound/linters/ruby" require "hound/linters/javascript" diff --git a/lib/hound/format.rb b/lib/hound/format.rb new file mode 100644 index 0000000..d1aef47 --- /dev/null +++ b/lib/hound/format.rb @@ -0,0 +1,7 @@ +module Hound + module Format + def self.error(text) + "\e[31m#{text}\e[0m" + end + end +end diff --git a/lib/hound/linters/base.rb b/lib/hound/linters/base.rb index 72b09c5..5933040 100644 --- a/lib/hound/linters/base.rb +++ b/lib/hound/linters/base.rb @@ -23,14 +23,15 @@ def filepath options["config_file"] end - def handle_config_errors + def parse_config_result &parse_command if filepath - yield + parse_command.call(file_content) + "Using #{filepath}" else "Not provided -- using default" end rescue Errno::ENOENT - "#{filepath} does not exist".colorize(:red) + Format.error("#{filepath} does not exist") end private @@ -41,6 +42,10 @@ def enabled? options.fetch("enabled", true) end + def file_content + File.read(filepath) + end + def options @options || {} end diff --git a/lib/hound/linters/javascript.rb b/lib/hound/linters/javascript.rb index d2b7a79..467d559 100644 --- a/lib/hound/linters/javascript.rb +++ b/lib/hound/linters/javascript.rb @@ -2,18 +2,9 @@ module Hound module Linter class Javascript < SimpleDelegator def config - handle_config_errors do - parse_config - end - end - - private - - def parse_config - JSON.parse(File.read(filepath)) - "Using #{filepath}" + parse_config_result { |file_content| JSON.parse(file_content) } rescue JSON::ParserError - "#{filepath} is invalid JSON".colorize(:red) + Format.error("#{filepath} is invalid JSON") end end end diff --git a/lib/hound/linters/ruby.rb b/lib/hound/linters/ruby.rb index b6ab3ee..0dd4f21 100644 --- a/lib/hound/linters/ruby.rb +++ b/lib/hound/linters/ruby.rb @@ -2,18 +2,9 @@ module Hound module Linter class Ruby < SimpleDelegator def config - handle_config_errors do - parse_config - end - end - - private - - def parse_config - YAML.load(File.read(filepath)) - "Using #{filepath}" + parse_config_result { |file_content| YAML.load(file_content) } rescue Psych::SyntaxError - "#{filepath} is invalid YAML".colorize(:red) + Format.error("#{filepath} is invalid YAML") end end end diff --git a/spec/hound_spec.rb b/spec/hound_spec.rb index eef4737..4553661 100644 --- a/spec/hound_spec.rb +++ b/spec/hound_spec.rb @@ -32,9 +32,11 @@ stub_file ".hound.yml", <<-CONFIG ruby: enabled: true + config_file: .ruby-style.yml go: enabled: false CONFIG + stub_file(".ruby-style.yml", "").and_raise(Errno::ENOENT) result = Hound.config @@ -42,7 +44,7 @@ Ruby --------- Status: Enabled - Config: Not provided -- using default + Config: \e[31m.ruby-style.yml does not exist\e[0m Javascript ---------