Class: Ruby::Nginx::Commands::AddHostMapping

Inherits:
TerminalCommand show all
Defined in:
lib/ruby/nginx/commands/add_host_mapping.rb

Instance Attribute Summary

Attributes inherited from TerminalCommand

#cmd, #error_type, #printer, #result, #user

Instance Method Summary collapse

Methods included from Helpers::SudoHelper

#sudoify

Methods included from Helpers::PromptHelper

#yes?

Constructor Details

#initialize(host, ip, sudo: false) ⇒ AddHostMapping

Returns a new instance of AddHostMapping.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby/nginx/commands/add_host_mapping.rb', line 9

def initialize(host, ip, sudo: false)
  @host = host
  @ip = ip
  @sudo = sudo
  sudo_reason = "Allow sudo elevation to add \"#{host}\" to /etc/hosts?"

  super(
    cmd: "echo \"#{host_config}\" | #{sudoify("tee -a /etc/hosts", sudo, sudo_reason)}",
    raise: Ruby::Nginx::ConfigError
  )
end

Instance Method Details

#runObject



21
22
23
24
25
26
27
28
# File 'lib/ruby/nginx/commands/add_host_mapping.rb', line 21

def run
  super unless added?
rescue Ruby::Nginx::ConfigError
  raise if @sudo

  # Elevate to sudo and try again.
  self.class.new(@host, @ip, sudo: true).run
end