s3crets 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module S3crets
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/s3crets.rb CHANGED
@@ -9,7 +9,6 @@ module S3crets
9
9
  require "fileutils"
10
10
  require "pathname"
11
11
  require "pp"
12
- require "s3crets_dig"
13
12
  require "s3crets_merge"
14
13
  require "rainbow"
15
14
  end
data/lib/s3crets_merge.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  class Configuratron
2
2
 
3
- attr_reader :overwrite, :files_updated
3
+ attr_reader :keys, :overwrite, :files_updated
4
4
 
5
5
  def initialize(opts={})
6
6
  if opts[:secrets_file]
@@ -22,41 +22,55 @@ class Configuratron
22
22
  end
23
23
 
24
24
  def replace_config(dir)
25
- search_folder = File.expand_path(dir)
26
- files = Dir.glob(search_folder + "/*.json")
25
+
26
+ search_folder = File.expand_path(dir)
27
+ files = Dir.glob(search_folder + "/*.json")
27
28
 
28
29
  if files.empty?
29
30
  puts "Was unable to find any JSON files [#{search_folder}]"
30
31
  else
31
-
32
32
  Dir.glob(File.expand_path(dir) + "/*.json") do |json_file|
33
33
 
34
34
  next if json_file =~ /.new./
35
35
 
36
- begin
37
- node_data = JSON.parse(File.read(json_file))
38
- rescue JSON::ParserError => e
39
- raise RuntimeError, "JSON Parse error -> #{json_file}"
40
- end
36
+ node_data = JSON.parse(File.read(json_file))
41
37
 
42
- # See if keys have intersection
43
- unless (@secrets.keys & node_data.keys).empty?
44
- node_data.merge! @secrets.select { |k| node_data.keys.include? k }
45
- file_to_write = get_file_name(json_file)
46
- write_file(file_to_write,node_data)
38
+ merge_hashes(@secrets, node_data)
39
+
40
+ file_to_write = get_file_name(json_file)
41
+ files_updated << file_to_write
42
+ File.open(file_to_write, 'w') do |fh|
43
+ fh.puts JSON.pretty_generate(node_data)
44
+ fh.close
47
45
  end
46
+
48
47
  end
49
48
  end
50
49
  end
51
50
 
52
51
  private
53
52
 
54
- def write_file(file,data)
55
- @files_updated << file
56
- File.open(file, 'w') do |fh|
57
- fh.puts JSON.pretty_generate(data)
58
- fh.close
53
+ # Merge source into destination in-place
54
+ # Only where they have keys in common
55
+ # Taking care not to lose keys in destination but not source
56
+ def merge_hashes(source, destination)
57
+ source.each_key do |key|
58
+
59
+ if destination.has_key?(key)
60
+
61
+ case source[key]
62
+ when Hash
63
+ merge_hashes( source[key], destination[key] )
64
+ when String
65
+ destination[key] = source[key]
66
+ else
67
+ puts "Searching through source hash for a string but found #{source[key].class}"
68
+ exit 1
69
+ end
70
+
59
71
  end
72
+
73
+ end
60
74
  end
61
75
 
62
76
  def get_file_name(json_file)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3crets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-09 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,7 +75,6 @@ files:
75
75
  - bin/s3crets
76
76
  - lib/s3crets.rb
77
77
  - lib/s3crets/version.rb
78
- - lib/s3crets_dig.rb
79
78
  - lib/s3crets_merge.rb
80
79
  - s3crets.gemspec
81
80
  homepage: ''
@@ -93,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
92
  version: '0'
94
93
  segments:
95
94
  - 0
96
- hash: -385047288439108485
95
+ hash: -2921035769767889780
97
96
  required_rubygems_version: !ruby/object:Gem::Requirement
98
97
  none: false
99
98
  requirements:
@@ -102,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
101
  version: '0'
103
102
  segments:
104
103
  - 0
105
- hash: -385047288439108485
104
+ hash: -2921035769767889780
106
105
  requirements: []
107
106
  rubyforge_project:
108
107
  rubygems_version: 1.8.23
data/lib/s3crets_dig.rb DELETED
@@ -1,7 +0,0 @@
1
- class Hash
2
- def dig(*path)
3
- path.inject(self) do |location, key|
4
- location.respond_to?(:keys) ? location[key] : nil
5
- end
6
- end
7
- end