diff --git a/README.md b/README.md index c919d3d..a248ae8 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,37 @@ # Yamldiff [![Build Status](https://travis-ci.org/wallace/yamldiff.png)](https://travis-ci.org/wallace/yamldiff) -Given two yaml files, Yamldiff tells you which keys present in the first file -are not present in the second. +Given two yaml files, Yamldiff tells you which keys present in the first file are not present in the second. ## Installation -Add this line to your application's Gemfile: +From release: +```bash +$ sudo gem install yamldiff +``` - gem 'yamldiff' - -And then execute: - - $ bundle - -Or install it yourself as: - - $ gem install yamldiff +From source code: +```bash +$ bundle +$ gem build yamldiff.gemspec +$ sudo gem install yamldiff-VERSION.gem +``` ## Usage - require 'yamldiff' - result = Yamldiff.diff_yaml('path_to_file_1', 'path_to_file_2') - result.inspect - # { 'path_to_file_2' => [] } +Of the program: +```console +$ yamldiff +USAGE: yamldiff [-i] file1 file2 +``` + +Of the lib: +```ruby +require 'yamldiff' +result = Yamldiff.diff_yaml('path_to_file_1', 'path_to_file_2') +result.inspect +# { 'path_to_file_2' => [] } +``` ## Contributing diff --git a/bin/yamldiff b/bin/yamldiff index 5ac0745..b2ce833 100755 --- a/bin/yamldiff +++ b/bin/yamldiff @@ -2,11 +2,22 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) require 'yamldiff' +require 'set' -if ARGV.size != 2 - $stderr.puts "USAGE: yamldiff file1 file2" +if ARGV.size != 2 && !(ARGV.size == 3 && ARGV[0] == "-i") + $stderr.puts "USAGE: yamldiff [-i] file1 file2" exit 1 end -errors = Yamldiff.diff_yaml(ARGV[0], ARGV[1]) -puts errors[ARGV[1]] +if ARGV.size == 3 + begin + ignore = Set.new File.readlines(ARGV[2] + ".ignore").map(&:chomp) + common = Set.new Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) + puts (common - ignore).to_a() + rescue Errno::ENOENT + puts Yamlcomm.comm_yaml(ARGV[1], ARGV[2]) + end +else + errors = Yamldiff.diff_yaml(ARGV[0], ARGV[1]) + puts errors[ARGV[1]] +end diff --git a/lib/yamldiff/version.rb b/lib/yamldiff/version.rb index 8b285b4..16be204 100644 --- a/lib/yamldiff/version.rb +++ b/lib/yamldiff/version.rb @@ -1,3 +1,3 @@ class Yamldiff - VERSION = "0.0.9" + VERSION = "0.0.10" end diff --git a/lib/yamldiff/yamldiff.rb b/lib/yamldiff/yamldiff.rb index 6979f7e..370d6a1 100644 --- a/lib/yamldiff/yamldiff.rb +++ b/lib/yamldiff/yamldiff.rb @@ -44,3 +44,46 @@ def compare_hashes(first, second, context = []) end end end + +class Yamlcomm + class << self + # Compare the two yaml files + def comm_yaml(first, second, common = []) + primary = YAML.load(ERB.new(File.read(first)).result) + secondary = YAML.load(ERB.new(File.read(second)).result) + common = compare_hashes(primary, secondary) + common + end + + # Adapted from yamldiff.rb + def compare_hashes(first, second, context = []) + common = [] + + first.each do |key, value| + + unless second + next + end + + if value.is_a?(Hash) + common << compare_hashes(value, second[key], context + [key]) + next + end + + if second.key?(key) + value2 = second[key] + if value.class == value2.class + if value == value2 + s = "" + context.each { |p| s += p + "." } + common << s + key + end + end + end + + end + + common.flatten + end + end +end