yaml-search-diff 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/ysdiff +15 -0
  3. data/lib/yaml_search_diff.rb +48 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63bb8aee38dd3e2c738cbc209ce8b530a4131ba4b43575d85eaa9fd06abc4201
4
+ data.tar.gz: 03dcf2c69fb51c0c98bd2de1422002020a60def94dd3f6c76b7d125b384b6073
5
+ SHA512:
6
+ metadata.gz: 216bfd791c95eb4073009b7a6554b4525aa75bee9e3d617d1d9e10fc2c46a75a0e3f2129cceb12bc8f301d728e9d594230fe3ed370292faaf03edf34f1a35df1
7
+ data.tar.gz: 3305c740db25e2036ac5ea369993d0fc52dcf2e59c0421d76d35dbdf57ea70ba95858b58421a7f54384f25ad071f1fc2a8ee84326ea1025b7b471ac41782f4ab
data/bin/ysdiff ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'yaml_search_diff'
5
+
6
+ key = ARGV[0]
7
+ file_1 = ARGV[1]
8
+ file_2 = ARGV[2]
9
+
10
+ # TODO: validate arguments and show help
11
+
12
+ yml_1 = open(file_1) {|f| YAML.load(f)}
13
+ yml_2 = open(file_2) {|f| YAML.load(f)}
14
+
15
+ puts YamlSearchDiff.run(key: key, yml_1: yml_1, yml_2: yml_2)
@@ -0,0 +1,48 @@
1
+ require 'yaml'
2
+ require 'diffy'
3
+
4
+ class YamlSearchDiff
5
+ def self.run(key:, yml_1:, yml_2:)
6
+ partial_1 = dfs(yml_1, key)
7
+ partial_2 = dfs(yml_2, key)
8
+
9
+ Diffy::Diff.new(
10
+ YAML.dump(nested_sort_hash(partial_1)),
11
+ YAML.dump(nested_sort_hash(partial_2))
12
+ )
13
+ end
14
+
15
+ private
16
+
17
+ def self.dfs(hash, key)
18
+ keys = hash.keys.sort_by(&:to_s)
19
+ keys.each do |k|
20
+ return hash[k] if k == key
21
+ dfs(hash[k], key) if hash[k].is_a?(Hash)
22
+ end
23
+ nil
24
+ end
25
+
26
+ def self.nested_sort_hash(hash)
27
+ hash.each do |k, v|
28
+ if v.is_a?(Hash)
29
+ hash[k] = nested_sort_hash(v)
30
+ elsif v.is_a?(Array)
31
+ hash[k] = nested_sort_array(v)
32
+ end
33
+ end
34
+ hash.sort.to_h
35
+ end
36
+
37
+ def self.nested_sort_array(array)
38
+ array.each_with_index do |v, i|
39
+ if v.is_a?(Hash)
40
+ array[i] = nested_sort_hash(v)
41
+ elsif v.is_a?(Array)
42
+ array[i] = nested_sort_array(v)
43
+ end
44
+ end
45
+ array.sort_by(&:to_s)
46
+ end
47
+ end
48
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml-search-diff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Natsuki Inoue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A commandline tool to showdiff of a specific key in yaml files.
14
+ email: summertree128@gmail.com
15
+ executables:
16
+ - ysdiff
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/ysdiff
21
+ - lib/yaml_search_diff.rb
22
+ homepage: https://rubygems.org/gems/yaml-search-diff
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.2.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: diff of a specific key in yaml
45
+ test_files: []