lib/sconb.rb in sconb-0.0.4 vs lib/sconb.rb in sconb-0.0.5
- old
+ new
@@ -11,11 +11,14 @@
desc "dump > dump.json", "Dump .ssh/config to JSON"
def dump()
path = options[:config]
file = File.expand_path(path)
configs = {}
- return configs unless File.readable?(file)
+ unless File.readable?(file)
+ puts configs
+ return
+ end
allconfig = config_load(path, '*')
configs['*'] = allconfig unless allconfig.size == 1
IO.foreach(file) do |line|
next if line =~ /^\s*(?:#.*)?$/
@@ -42,18 +45,15 @@
puts JSON.pretty_generate configs
end
desc "restore < dump.json > .ssh/config", "Restore .ssh/config from JSON"
def restore()
- ssh_config = ''
- json = ''
- while str = $stdin.gets
- json << str
- end
+ ssh_configs = []
+ json = stdin_read
configs = JSON.parse(json)
configs.each do |host, config|
- ssh_config << "\n"
+ ssh_config = ''
ssh_config << 'Host ' + host + "\n"
config.each do |key, value|
next if key.downcase == 'host' || key.downcase == 'identityfilecontent'
if key.downcase == 'identityfile'
value.each_with_index do |keyfile,i|
@@ -61,30 +61,28 @@
end
else
ssh_config << ' ' + key + ' ' + value + "\n"
end
end
+ ssh_configs.push ssh_config
end
- puts ssh_config
+ puts ssh_configs.join("\n")
end
method_option :force, :type => :boolean, :aliases => '-f', :default => false, :banner => 'force generate'
desc "keyregen < dump.json", "Regenerate private keys from JSON"
def keyregen()
- json = ''
- while str = $stdin.gets
- json << str
- end
+ json = $stdin.read
configs = JSON.parse(json)
configs.each do |host, config|
config.each do |key, value|
next unless key.downcase == 'identityfilecontent'
identity_files = config['IdentityFile']
value.each_with_index do |keycontent,i|
identity_file = File.expand_path(identity_files[i])
if File.exists?(identity_file) and !options[:force]
- raise Thor::Error, "Error: key exists. If you want to overwrite, use --force option."
+ raise Thor::Error, "Error: " + identity_files[i] + " is exists. If you want to overwrite, use --force option."
end
puts 'Regenerate ' + identity_files[i] + ' ...'
File.open(identity_file, 'w') do |file|
file.write keycontent
end
@@ -92,12 +90,17 @@
end
end
end
end
- # Original code Net::SSH::Config.load
private
+ def stdin_read()
+ return $stdin.read
+ end
+
+ # Original code is Net::SSH::Config.load (https://github.com/net-ssh/net-ssh/blob/master/LICENSE.txt)
+ private
def config_load(path, host)
settings = {}
file = File.expand_path(path)
return settings unless File.readable?(file)
@@ -167,10 +170,10 @@
return settings
end
private
- # Original code Net::SSH::Config.pattern2regex
+ # Original code is Net::SSH::Config.pattern2regex (https://github.com/net-ssh/net-ssh/blob/master/LICENSE.txt)
def pattern2regex(pattern)
pattern = "^" + pattern.to_s.gsub(/\./, "\\.").
gsub(/\?/, '.').
gsub(/([+\/])/, '\\\\\\0').
gsub(/\*/, '.*') + "$"