Class: VagrantDockerHostsManager::Action::Apply

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-docker-hosts-manager/actions/apply.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ Apply

Returns a new instance of Apply.



6
# File 'lib/vagrant-docker-hosts-manager/actions/apply.rb', line 6

def initialize(app, _env) = (@app = app)

Instance Method Details

#call(env) ⇒ Object



8
9
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-docker-hosts-manager/actions/apply.rb', line 8

def call(env)
  Util::I18n.setup!(env)
  cfg = env[:machine].config.docker_hosts
  ENV['VDHM_VERBOSE'] = '1' if cfg.respond_to?(:verbose) && cfg.verbose
  mid    = env[:machine].id || 'unknown'
  dry    = Util::I18n.env_flag('VDHM_DRY_RUN')
  ui     = env[:ui]
  hoster = Util::HostsFile.new(env, owner_id: mid)

  entries = compute_entries(env, cfg, ui)
  if entries.empty?
    UiHelpers.say(ui, ::I18n.t('vdhm.messages.no_entries'))
    return
  end

  if dry
    Util::Json.emit(action: 'apply', status: 'dry-run', data: { owner: mid, entries: entries })
    return
  end

  hoster.apply(entries)
  Util::Json.emit(action: 'apply', status: 'success', data: { owner: mid, entries: entries })
rescue StandardError => e
  Util::Json.emit(action: 'apply', status: 'error', error: e.message, backtrace: e.backtrace&.first(3))
  UiHelpers.error(ui, "VDHM: #{e.message}")
ensure
  @app.call(env)
end