class Gaptool::SSHConfigCommand

Public Instance Methods

execute() click to toggle source
# File lib/gaptool_client/commands.rb, line 139
def execute
  config, content = Gaptool::SSH.config

  if remove?
    data = ''
  else
    puts Rainbow('Getting nodes from API').green
    data = [Gaptool::SSH::START_MARKER, '# to remove this block, run gt ssh-config --remove']
    Gaptool::API.client.getallnodes(hidden: true).sort_by { |n| n['instance'] }.each do |node|
      host = Gaptool::API.get_host(node)
      puts " - #{Rainbow(host).blue}"
      data << Gaptool::SSH.config_for(node)
    end
    data << Gaptool::SSH::STOP_MARKER
    data = data.join("\n")
  end

  if Regexp.new(Gaptool::SSH::START_MARKER).match(content)
    content.gsub!(/#{Gaptool::SSH::START_MARKER}.*?#{Gaptool::SSH::STOP_MARKER}/m, data)
  elsif !remove?
    content = content + "\n" + data
  end
  File.open(config, 'w') { |f| f.write(content) }
end