Class: Tfctl::Config

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tfctl/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_name:, yaml_config:, aws_org_config:, use_cache: false) ⇒ Config



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tfctl/config.rb', line 13

def initialize(config_name:, yaml_config:, aws_org_config:, use_cache: false)
    cache_file  = "#{PROJECT_ROOT}/.tfctl/#{config_name}_cache.yaml"

    # Get configuration.  Either load from cache or process fresh.
    if use_cache
        @config = read_cache(cache_file)
    else
        @config = load_config(config_name, yaml_config, aws_org_config)
        write_cache(cache_file)
    end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/tfctl/config.rb', line 11

def config
  @config
end

Instance Method Details

#[](key) ⇒ Object



25
26
27
# File 'lib/tfctl/config.rb', line 25

def [](key)
    @config[key]
end

#each(&block) ⇒ Object



29
30
31
# File 'lib/tfctl/config.rb', line 29

def each(&block)
    @config.each(&block)
end

#find_accounts(property_name, property_value) ⇒ Object

Filters accounts by account property



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tfctl/config.rb', line 48

def find_accounts(property_name, property_value)
    output =[]
    @config[:accounts].each do ||
        if [property_name] == property_value
            output << 
        end
    end

    if output.empty?
        raise Tfctl::Error, "Account not found with #{property_name}: #{property_value}"
    end

    output
end

#find_accounts_regex(property_name, expr) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tfctl/config.rb', line 63

def find_accounts_regex(property_name, expr)
    output =[]
    @config[:accounts].each do ||
        begin
            if [property_name] =~ /#{expr}/
                output << 
            end
        rescue RegexpError => e
            raise Tfctl::Error, "Regexp: #{e}"
        end
    end

    if output.empty?
        raise Tfctl::Error, "Account not found with #{property_name} matching regex: #{expr}"
    end

    output
end

#key?(key) ⇒ Boolean Also known as: has_key?



33
34
35
# File 'lib/tfctl/config.rb', line 33

def key?(key)
    @config.key?(key)
end

#to_json(*_args) ⇒ Object



43
44
45
# File 'lib/tfctl/config.rb', line 43

def to_json(*_args)
    @config.to_json
end

#to_yamlObject



39
40
41
# File 'lib/tfctl/config.rb', line 39

def to_yaml
    @config.to_yaml
end