class Sshez::Exec

handles all the ssh commands and updates to the .ssh/config file

Constants

FILE_PATH
PRINTER

Attributes

listener[R]

to create an instance pass any Struct(listener) that handles the following methods

  • :argument_error(Command)

  • :done_with_no_guarantee

  • :permission_error

  • :finished_successfully

Public Class Methods

new(listener) click to toggle source

Must have the methods mentioned above

# File lib/sshez/exec.rb, line 25
def initialize(listener)
  @listener = listener
end

Public Instance Methods

start_exec(command, options) click to toggle source

Starts the execution of the Command parsed with its options

# File lib/sshez/exec.rb, line 32
def start_exec(command, options)
  all_args = command.args
  all_args << options
  self.send(command.name, *all_args)
end

Private Instance Methods

add(alias_name, user, host, options) click to toggle source

append an alias for the given user@host with the options passed

# File lib/sshez/exec.rb, line 56
def add(alias_name, user, host, options)
  begin
    PRINTER.verbose_print "Adding\n"
    config_append = form(alias_name, user, host, options)
    PRINTER.verbose_print config_append
    unless options.test
      file = File.open(FILE_PATH, 'a+')
      file.write(config_append)
      file.close

      # causes a bug in fedore if permission was not updated to 0600
      File.chmod(0600, FILE_PATH)
      # system "chmod 600 #{FILE_PATH}"
    end
  rescue
    return permission_error
  end
  PRINTER.verbose_print "to #{FILE_PATH}"
  PRINTER.print "Successfully added `#{alias_name}` as an alias for `#{user}@#{host}`"
  PRINTER.print "Try sshez connect #{alias_name}"

  finish_exec
end
all_hosts_in(file) click to toggle source

Returns all the alias names of in the file

# File lib/sshez/exec.rb, line 170
def all_hosts_in(file)
  servers = []
  file.each do |line|
    if line.include?('Host ')
      servers << line.sub('Host ', '').strip
    end
  end
  servers
end
connect(alias_name, options) click to toggle source

connects to host using alias

# File lib/sshez/exec.rb, line 42
def connect(alias_name, options)
  file = File.open(FILE_PATH, 'r')
  servers = all_hosts_in(file)
  if servers.include?alias_name
    PRINTER.verbose_print "Connecting to #{alias_name}"
    exec "ssh #{alias_name}"
  else
    PRINTER.print "Could not find host `#{alias_name}`"
  end
end
finish_exec() click to toggle source

finished editing the file successfully

# File lib/sshez/exec.rb, line 190
def finish_exec
  listener.finished_successfully
end
form(alias_name, user, host, options) click to toggle source

returns the text that will be added to the config file

# File lib/sshez/exec.rb, line 83
def form(alias_name, user, host, options)
  retuned = "\n"
  retuned += "Host #{alias_name}\n"
  retuned += "  HostName #{host}\n"
  retuned += "  User #{user}\n"

  options.file_content.each_pair do |key, value|
    retuned += value
  end
  retuned

end
list(options) click to toggle source

lists the aliases available in the config file

# File lib/sshez/exec.rb, line 145
def list(options)
  file = File.open(FILE_PATH, 'a+')
  servers = all_hosts_in(file)
  file.close
  if servers.empty?
    PRINTER.print 'No aliases added'
  else
    PRINTER.print 'Listing aliases:'
    servers.each{|x| PRINTER.print "\t- #{x}"}
  end
  finish_exec
end
permission_error() click to toggle source

Raises a permission error to the listener

# File lib/sshez/exec.rb, line 183
def permission_error
  listener.permission_error
end
remove(alias_name, options) click to toggle source

removes an alias from the config file (all its occurrences will be removed too)

# File lib/sshez/exec.rb, line 99
def remove(alias_name, options)
  file = File.open(FILE_PATH, 'r')
  servers = all_hosts_in(file)
  if servers.include?alias_name
    new_file = File.open(FILE_PATH + 'temp', 'w')
    remove_alias_name(alias_name, file, new_file)

    File.delete(FILE_PATH)
    File.rename(FILE_PATH + 'temp', FILE_PATH)
    # Causes a bug in fedore if permission was not updated to 0600
    File.chmod(0600, FILE_PATH)
    PRINTER.print "`#{alias_name}` was successfully removed from your hosts"
  else
    PRINTER.print "Could not find host `#{alias_name}`"
  end
  finish_exec
end
remove_alias_name(alias_name, file, new_file) click to toggle source

copies the content of the file to the new file without the sections concerning the alias_name

# File lib/sshez/exec.rb, line 121
def remove_alias_name(alias_name, file, new_file)
  started_removing = false
  file.each do |line|
    started_removing ||= line.include?("Host #{alias_name}")
    if started_removing
      # I will never stop till I find another host that is not the one I'm removing
      stop_removing = (started_removing && line.include?('Host ') && !(line =~ /\b#{alias_name}\b/))
      PRINTER.verbose_print line unless stop_removing
      if stop_removing && started_removing
        new_file.write(line)
      end
      started_removing = !stop_removing
    else
      # Everything else should be transfered safely to the other file
      new_file.write(line)
    end
  end
  file.close
  new_file.close
end
reset(options) click to toggle source
# File lib/sshez/exec.rb, line 158
def reset(options)
  resp = PRINTER.prompt 'Are you sure you want to remove all aliases? [Y/n]'
  if resp.match(/y/i)
    file = File.open(FILE_PATH, "w")
    file.close
    PRINTER.print 'You have successfully reset your ssh config file.'
  end
end