yaml-search-diff 0.2.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ysdiff +10 -1
- data/lib/yaml_search_diff.rb +62 -33
- metadata +19 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43c8d977451539a04e29673ecd6c5fc7b14f2523ddacba5b818dfaaf6d37ab0d
|
4
|
+
data.tar.gz: b534be13e379921eee993728ad8018567736542ff31904ebe5ec41c0079b2e41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7cf20666840ea29c9427b308c527c8f7b9632539f3b6c9c29d5996cb88e4b591a2f180927dc62c1317433ba6e16c8074fc5c44e776e21357ac33a1452368757
|
7
|
+
data.tar.gz: 4254b55411edb43987487a5c9d3b5028f9806fbc7640db7873ce1ab66042692ba7ee866b4174e332a66b428b3f2659c6412568d692ef3fee25df8b02e351a8c2
|
data/bin/ysdiff
CHANGED
@@ -12,4 +12,13 @@ file_2 = ARGV[2]
|
|
12
12
|
yml_1 = open(file_1) {|f| YAML.load(f)}
|
13
13
|
yml_2 = open(file_2) {|f| YAML.load(f)}
|
14
14
|
|
15
|
-
|
15
|
+
invalid_files = []
|
16
|
+
invalid_files << file_1 unless yml_1.is_a?(Hash)
|
17
|
+
invalid_files << file_2 unless yml_2.is_a?(Hash)
|
18
|
+
|
19
|
+
if invalid_files.empty?
|
20
|
+
puts YamlSearchDiff.run(key: key, yml_1: yml_1, yml_2: yml_2)
|
21
|
+
else
|
22
|
+
STDERR.puts "Couldn't parse following files as Hash: #{invalid_files.join(', ')}"
|
23
|
+
exit 1
|
24
|
+
end
|
data/lib/yaml_search_diff.rb
CHANGED
@@ -2,47 +2,76 @@ require 'yaml'
|
|
2
2
|
require 'diffy'
|
3
3
|
|
4
4
|
class YamlSearchDiff
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
Diffy::Diff.new(
|
10
|
-
YAML.dump(nested_sort_hash(partial_1)),
|
11
|
-
YAML.dump(nested_sort_hash(partial_2))
|
12
|
-
)
|
13
|
-
end
|
5
|
+
class << self
|
6
|
+
def run(key:, yml_1:, yml_2:)
|
7
|
+
return "" unless yml_1.is_a?(Hash) && yml_2.is_a?(Hash)
|
14
8
|
|
15
|
-
|
9
|
+
searched_1 = key.include?(':') ? search_dig(key, yml_1) : search(key, yml_1)
|
10
|
+
searched_2 = key.include?(':') ? search_dig(key, yml_2) : search(key, yml_2)
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
dfs(hash[k], key) if hash[k].is_a?(Hash)
|
22
|
-
end
|
23
|
-
nil
|
12
|
+
Diffy::Diff.new(
|
13
|
+
YAML.dump(searched_1),
|
14
|
+
YAML.dump(searched_2)
|
15
|
+
)
|
24
16
|
end
|
25
17
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
18
|
+
private
|
19
|
+
|
20
|
+
def search(key, yml)
|
21
|
+
searched = catch(:has_key) { dfs(yml, key) }
|
22
|
+
sort_yml(searched)
|
23
|
+
end
|
24
|
+
|
25
|
+
def search_dig(key, yml)
|
26
|
+
splitted_keys = key.split(':')
|
27
|
+
first_key = splitted_keys.first
|
28
|
+
|
29
|
+
searched = catch(:has_key) { dfs(yml, first_key) }
|
30
|
+
digged = searched.dig(*splitted_keys[1..])
|
31
|
+
|
32
|
+
sort_yml(digged)
|
33
|
+
end
|
34
|
+
|
35
|
+
def dfs(hash, key)
|
36
|
+
keys = hash.keys.sort_by(&:to_s)
|
37
|
+
keys.each do |k|
|
38
|
+
throw :has_key, hash[k] if k == key
|
39
|
+
dfs(hash[k], key) if hash[k].is_a?(Hash)
|
32
40
|
end
|
41
|
+
{}
|
33
42
|
end
|
34
|
-
hash.sort.to_h
|
35
|
-
end
|
36
43
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
44
|
+
def sort_yml(yml)
|
45
|
+
if yml.is_a?(Hash)
|
46
|
+
nested_sort_hash(yml)
|
47
|
+
elsif yml.is_a?(Array)
|
48
|
+
nested_sort_array(yml)
|
49
|
+
else
|
50
|
+
yml
|
43
51
|
end
|
44
52
|
end
|
45
|
-
|
46
|
-
|
53
|
+
|
54
|
+
def nested_sort_hash(hash)
|
55
|
+
hash.each do |k, v|
|
56
|
+
if v.is_a?(Hash)
|
57
|
+
hash[k] = nested_sort_hash(v)
|
58
|
+
elsif v.is_a?(Array)
|
59
|
+
hash[k] = nested_sort_array(v)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
hash.sort_by { |k, v| k.to_s }.to_h
|
63
|
+
end
|
64
|
+
|
65
|
+
def nested_sort_array(array)
|
66
|
+
array.each_with_index do |v, i|
|
67
|
+
if v.is_a?(Hash)
|
68
|
+
array[i] = nested_sort_hash(v)
|
69
|
+
elsif v.is_a?(Array)
|
70
|
+
array[i] = nested_sort_array(v)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
array.sort_by(&:to_s)
|
74
|
+
end
|
75
|
+
end
|
47
76
|
end
|
48
77
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml-search-diff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Natsuki Inoue
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: diffy
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
13
27
|
description: A commandline tool to showdiff of a specific key in yaml files.
|
14
28
|
email: summertree128@gmail.com
|
15
29
|
executables:
|
@@ -22,7 +36,8 @@ files:
|
|
22
36
|
homepage: https://rubygems.org/gems/yaml-search-diff
|
23
37
|
licenses:
|
24
38
|
- MIT
|
25
|
-
metadata:
|
39
|
+
metadata:
|
40
|
+
documentation_uri: https://github.com/summertree128/yaml-search-diff
|
26
41
|
post_install_message:
|
27
42
|
rdoc_options: []
|
28
43
|
require_paths:
|