Class: Configuratron

Inherits:
Object
  • Object
show all
Defined in:
lib/s3crets_merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Configuratron

Returns a new instance of Configuratron.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/s3crets_merge.rb', line 5

def initialize(opts={})
    if opts[:secrets_file]
        begin
            @secrets = YAML::load_file(opts[:secrets_file])
        rescue Exception => e
            raise RuntimeError, "Yaml config file not found"
        end
    else
        @secrets = find_secrets
    end

    @files_updated = []

    @overwrite = opts.has_key?(:overwrite) ? opts[:overwrite] : false

    replace_config(opts[:json_dir])

end

Instance Attribute Details

#files_updatedObject (readonly)

Returns the value of attribute files_updated.



3
4
5
# File 'lib/s3crets_merge.rb', line 3

def files_updated
  @files_updated
end

#keysObject (readonly)

Returns the value of attribute keys.



3
4
5
# File 'lib/s3crets_merge.rb', line 3

def keys
  @keys
end

#overwriteObject (readonly)

Returns the value of attribute overwrite.



3
4
5
# File 'lib/s3crets_merge.rb', line 3

def overwrite
  @overwrite
end

Instance Method Details

#replace_config(dir) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/s3crets_merge.rb', line 24

def replace_config(dir)
    counter = 0
    search_folder  =  File.expand_path(dir)
    files          =  Dir.glob(search_folder + "/*.json")

    if files.empty?
        puts "Was unable to find any JSON files [#{search_folder}]"
    else

        Dir.glob(File.expand_path(dir) + "/*.json") do |json_file|

            next if json_file =~ /.new./

            begin
                node_data  =  JSON.parse(File.read(json_file)+".")
            rescue JSON::ParserError => e
                raise RuntimeError, "JSON Parse error -> #{json_file}"
            end

            # See if keys have intersection
            unless (@secrets.keys & node_data.keys).empty?
                node_data.merge! @secrets.select { |k| node_data.keys.include? k }
                file_to_write = get_file_name(json_file)
                write_file(file_to_write,node_data)
            end
        end
    end
end