class Avmtrf1::Tools::Runner::Openvpn

def ad_auth?

def ad_auth?
  !parsed.no_ad_auth?
end

def command_uncached

def command_uncached
  if parsed.no_sudo?
    ::Avmtrf1::Executables.env.command(*openvpn_args)
  else
    ::Avmtrf1::Executables.sudo.command(*openvpn_args)
  end
end

def config_builder_uncached

def config_builder_uncached
  r = ::Avmtrf1::Openvpn::Config::Builder.new(parsed.ovpn_path)
  r.simple_add_or_update('auth-user-pass', [credentials_path.to_path]) if ad_auth?
  r
end

def credentials_content

def credentials_content
  [::Avmtrf1.ad_user.username, ::Avmtrf1.ad_user.password]
    .map { |v| "#{v}\n" }.join
end

def on_config_file(&block)

def on_config_file(&block)
  on_temp_file(:ovpn_config_path, -> { config_builder.data }, &block)
end

def on_credentials_file(&block)

def on_credentials_file(&block)
  if ad_auth?
    on_temp_file(:credentials_path, -> { credentials_content }, &block)
  else
    block.call
  end
end

def on_temp_file(attr, content_proc)

def on_temp_file(attr, content_proc)
  ::EacRubyUtils::Fs::Temp.on_file do |file|
    file.write(content_proc.call)
    send("#{attr}=", file)
    yield
    send("#{attr}=", nil)
  end
end

def on_temp_files(&block)

def on_temp_files(&block)
  on_credentials_file do
    on_config_file(&block)
  end
end

def openvpn_args

def openvpn_args
  ['openvpn', '--config', ovpn_config_path]
end

def run

def run
  on_temp_files do
    start_banner
    infov 'Resultado', command.system
  end
end

def start_banner

def start_banner
  infov 'Arquivo de configuração', parsed.ovpn_path
  infov 'Linha de comando', ::Shellwords.join(command.args)
end