kucodiff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da8aef200dbbf8a83f7772181b035549cc929450
4
+ data.tar.gz: 529ad277ad8cc3dcfcdc4c433d62df20faa19c25
5
+ SHA512:
6
+ metadata.gz: 70500ca39f992dfa03d986c9c9aa4ec01b9c3dda9fac3be0284b7c1943f12b966db39a4f19e9cce560c0b1f72e321ae5203304f817cffcc72fc8c07e280111c0
7
+ data.tar.gz: bd7581ffeec22db708ec87e293f8331746b202744f3b27afea5c151977a065b9cf7d570f6a9be10640bdb47090e3c8b6eee66b993e75f36c59eb9d4f027a2218
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2013 Michael Grosser <michael@grosser.it>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ module Kucodiff
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kucodiff.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'yaml'
2
+
3
+ module Kucodiff
4
+ class << self
5
+ def diff(files, ignore: false, expected: {})
6
+ raise ArgumentError, "Need 2+ files" if files.size < 2
7
+
8
+ base = files.shift
9
+ base_template = read(base)
10
+ diff = files.each_with_object({}) do |other, all|
11
+ result = different_keys(base_template, read(other))
12
+ result.reject! { |k| k =~ ignore } if ignore
13
+ all["#{base}-#{other}"] = result.sort
14
+ end
15
+
16
+ expected.each do |k, v|
17
+ result = xor(diff[k] || [], v)
18
+ result.empty? ? diff.delete(k) : diff[k] = result
19
+ end
20
+
21
+ diff
22
+ end
23
+
24
+ private
25
+
26
+ def read(file)
27
+ content = if file.end_with?('.yml', '.yaml')
28
+ YAML.load_stream(File.read(file)) # TODO: test need for stream
29
+ else raise ArgumentError, "unknown file format in #{file}"
30
+ end.first
31
+
32
+ hashify_container_env(content)
33
+ flat_hash(content)
34
+ end
35
+
36
+ # make env compareable
37
+ def hashify_container_env(content)
38
+ containers = content.fetch('spec', {}).fetch('template', {}).fetch('spec', {}).fetch('containers', [])
39
+ containers.each do |container|
40
+ next unless container['env']
41
+ container['env'] = container['env'].each_with_object({}) do |v, h|
42
+ value_key = (v.keys - ['name']).first
43
+ h[v.fetch('name')] = v.fetch(value_key)
44
+ end
45
+ end
46
+ end
47
+
48
+ def different_keys(a, b)
49
+ (a.keys + b.keys).uniq.select { |k| a[k] != b[k] }
50
+ end
51
+
52
+ def xor(a, b)
53
+ a + b - (a & b)
54
+ end
55
+
56
+ # http://stackoverflow.com/questions/9647997/converting-a-nested-hash-into-a-flat-hash
57
+ def flat_hash(input, base = nil, all = {})
58
+ if input.is_a?(Array)
59
+ input = input.each_with_index.to_a.each(&:reverse!)
60
+ end
61
+
62
+ if input.is_a?(Hash) || input.is_a?(Array)
63
+ input.each do |k, v|
64
+ flat_hash(v, base ? "#{base}.#{k}" : k, all)
65
+ end
66
+ else
67
+ all[base] = input
68
+ end
69
+
70
+ all
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kucodiff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: michael@grosser.it
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - MIT-LICENSE
20
+ - lib/kucodiff.rb
21
+ - lib/kucodiff/version.rb
22
+ homepage: https://github.com/grosser/kucodiff
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: 2.1.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Smart diff for kubernetes configs to ensure symmetric configuration
46
+ test_files: []