1
1
require 'json'
2
2
3
3
class FactDif
4
- def initialize ( old_output , new_output , exclude_list = [ ] )
4
+ def initialize ( old_output , new_output , exclude_list , save_structured )
5
5
@c_facter = JSON . parse ( old_output )
6
6
@next_facter = JSON . parse ( new_output )
7
7
@exclude_list = exclude_list
8
+ @save_structured = save_structured
9
+ @flat_diff = [ ]
8
10
@diff = { }
9
11
end
10
12
11
13
def difs
12
- search_hash ( @c_facter , [ ] )
14
+ search_hash ( ( ( @c_facter . to_a - @next_facter . to_a ) | ( @next_facter . to_a - @c_facter . to_a ) ) . to_h )
15
+
16
+ @flat_diff . sort_by { |a | a [ 0 ] } . each do |pair |
17
+ fact_path = pair [ 0 ]
18
+ value = pair [ 1 ]
19
+ compare ( fact_path , value , @c_facter )
20
+ compare ( fact_path , value , @next_facter )
21
+ end
13
22
14
23
@diff
15
24
end
@@ -28,35 +37,45 @@ def search_hash(sh, path = [])
28
37
path . pop
29
38
end
30
39
else
31
- compare ( path , sh )
40
+ @flat_diff . push ( [ path . dup , sh ] )
32
41
end
33
42
end
34
43
35
- def compare ( fact_path , old_value )
36
- new_value = @next_facter . dig ( *fact_path )
37
- if different? ( new_value , old_value ) && !excluded? ( fact_path . join ( '.' ) )
38
- @diff [ fact_path . join ( '.' ) ] = { new_value : new_value , old_value : old_value }
44
+ def compare ( fact_path , given_value , compared_hash )
45
+ compared_value = compared_hash . dig ( *fact_path )
46
+ if different? ( compared_value , given_value ) && !excluded? ( fact_path . join ( '.' ) )
47
+ fact_path = fact_path . map { |f | f . to_s . include? ( '.' ) ? "\" #{ f } \" " : f } . join ( '.' ) unless @save_structured
48
+ if compared_hash == @c_facter
49
+ bury ( *fact_path , { :new_value => given_value , :old_value => compared_value } , @diff )
50
+ else
51
+ bury ( *fact_path , { :new_value => compared_value , :old_value => given_value } , @diff )
52
+ end
53
+ end
54
+ end
55
+
56
+ def bury ( *paths , value , hash )
57
+ if paths . count > 1
58
+ path = paths . shift
59
+ hash [ path ] = Hash . new unless hash . key? ( path )
60
+ bury ( *paths , value , hash [ path ] )
61
+ else
62
+ hash [ *paths ] = value
39
63
end
40
64
end
41
65
42
66
def different? ( new , old )
43
- if old . is_a? ( String ) && new . is_a? ( String )
67
+ if old . is_a? ( String ) && new . is_a? ( String ) && ( old . include? ( ',' ) || new . include? ( ',' ) )
44
68
old_values = old . split ( ',' )
45
69
new_values = new . split ( ',' )
46
70
47
- diff = old_values - new_values
48
- # also add new entries only available in Facter 4
49
- diff . concat ( new_values - old_values )
50
-
51
- return true if diff . any?
52
-
53
- return false
71
+ diff = ( old_values - new_values ) | ( new_values - old_values )
72
+ return diff . size . positive?
54
73
end
55
74
56
75
old != new
57
76
end
58
77
59
78
def excluded? ( fact_name )
60
- @exclude_list . any? { |excluded_fact | fact_name =~ /^ #{ excluded_fact } $ / }
79
+ @exclude_list . any? { |excluded_fact | fact_name =~ /#{ excluded_fact } / }
61
80
end
62
81
end
0 commit comments