lib/fulmar/infrastructure/service/ssh_config_service.rb in fulmar-2.1.2 vs lib/fulmar/infrastructure/service/ssh_config_service.rb in fulmar-2.2.0
- old
+ new
@@ -6,12 +6,12 @@
module Fulmar
module Infrastructure
module Service
# Adds entries to the ssh config and checks for existing ones
class SSHConfigService
- DEFAULT_CONFIG_FILE = "#{ENV['HOME']}/.ssh/config"
- DEFAULT_KNOWN_HOSTS_FILE = "#{ENV['HOME']}/.ssh/known_hosts"
+ DEFAULT_CONFIG_FILE = "#{ENV['HOME']}/.ssh/config".freeze
+ DEFAULT_KNOWN_HOSTS_FILE = "#{ENV['HOME']}/.ssh/known_hosts".freeze
attr_accessor :config_file, :known_host_file, :quiet
def initialize(config, config_file = DEFAULT_CONFIG_FILE, known_hosts_file = DEFAULT_KNOWN_HOSTS_FILE)
@config = config
@@ -53,11 +53,11 @@
def remove_known_host(hostname)
input_file = File.open(@known_hosts_file, 'r')
output_file = File.open(@known_hosts_file + '.temp', 'w')
while (line = input_file.gets)
- output_file.puts(line) unless /^\[?#{hostname.gsub('.', '\\.')}(?:\]:\d+)?[ ,]/.match(line)
+ output_file.puts(line) unless /^\[?#{hostname.gsub('.', '\\.')}(?:\]:\d+)?[ ,]/ =~ line
end
input_file.close
output_file.close
FileUtils.mv(@known_hosts_file + '.temp', @known_hosts_file)
end
@@ -75,11 +75,11 @@
# Parses the users ssh config for an existing hostname
def host_exists?(hostname)
config_file = File.open(@config_file, 'r')
while (line = config_file.gets)
- if /\s*Host #{hostname.gsub('.', '\\.')}\s*$/.match(line)
+ if /\s*Host #{hostname.gsub('.', '\\.')}\s*$/ =~ line
config_file.close
return true
end
end
config_file.close
@@ -89,11 +89,11 @@
# Adds a host to the ssh config file
def add_host(hostname, ssh_config = {})
puts "Adding host #{hostname}..." if @config[:debug]
config_file = File.open(@config_file, 'a')
- unless ssh_config[:IdentityFile].blank? or ssh_config[:IdentityFile][0, 1] == '/'
+ unless ssh_config[:IdentityFile].blank? || ssh_config[:IdentityFile][0, 1] == '/'
ssh_config[:IdentityFile] = @config.base_path + '/' + ssh_config[:IdentityFile]
end
config_file.puts host_entry(hostname, ssh_config)
@@ -106,11 +106,11 @@
end
protected
def host_entry(hostname, ssh_config = {})
- unless ssh_config[:IdentityFile].blank? or ssh_config[:IdentityFile][0, 1] == '/'
+ unless ssh_config[:IdentityFile].blank? || ssh_config[:IdentityFile][0, 1] == '/'
ssh_config[:IdentityFile] = @config.base_path + '/' + ssh_config[:IdentityFile]
end
entry = [
'', # Add some space between this and the second last entry
@@ -140,26 +140,22 @@
def config_valid?(host_config)
(!host_config[:hostname].blank? && !host_config[:ssh_config].nil?)
end
def remove_trailing_newlines(data)
- while !data.empty? && data.last.strip.empty?
- data.pop
- end
+ data.pop while !data.empty? && data.last.strip.empty?
data
end
def block_before(data, hostname)
cache = []
before = []
data.each do |line|
if line.strip[0] == '#'
cache << line
else
- if /^Host\s#{hostname}$/.match line.strip
- return remove_trailing_newlines(before)
- end
+ return remove_trailing_newlines(before) if /^Host\s#{hostname}$/ =~ line.strip
before += cache
cache = []
before << line
end
end
@@ -176,12 +172,10 @@
write = false
data.each do |line|
if line.strip[0] == '#'
cache << line
else
- if /^Host\s/.match line.strip
- write = true
- end
+ write = true if /^Host\s/ =~ line.strip
if write
after += cache
after << line
end
cache = []