Class: VagrantPlugins::DNS::Action::RegisterPatterns

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dns/action/register_patterns.rb

Overview

Write dns patterns to ~/.vagrant.d/dns/config file.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RegisterPatterns

Returns a new instance of RegisterPatterns.



6
7
8
# File 'lib/vagrant-dns/action/register_patterns.rb', line 6

def initialize(app, env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-dns/action/register_patterns.rb', line 10

def call(env)
  unless env[:machine].config.dns.enabled
    return @app.call(env)
  end

  config_file = env[:dns].config_file
  registry    = File.exists?(config_file) ? \
                  YAML.load(File.read(config_file)) : {}
  opts        = prepare_opts(env[:machine].config)
  patterns    = opts[:patterns] || default_patterns(opts)
  networks    = opts[:networks].select { |i| i[0] == :private_network }
                               .flatten

  ip = networks.empty? ? '127.0.0.1' : networks[1][:ip]

  patterns.each do |p|
    p = p.source if p.respond_to? :source # Regexp#to_s is unusable
    registry[p] = ip
  end

  env[:dns].ui.info "Registering patterns #{registry}"

  File.open(config_file, "w") { |f| f << YAML.dump(registry) }

  @app.call(env)
end