Class: Russh::Accessor

Inherits:
Object
  • Object
show all
Defined in:
lib/russh/accessor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccessor

Returns a new instance of Accessor.



7
8
9
10
11
12
# File 'lib/russh/accessor.rb', line 7

def initialize
  @path = Dir.home + '/.ssh/config'
  @is_existing = is_existing?
  @is_readable = is_readable?
  @is_writable = is_writable?
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/russh/accessor.rb', line 5

def path
  @path
end

Instance Method Details

#backupObject

Backups the config file to config.bk



43
44
45
46
47
# File 'lib/russh/accessor.rb', line 43

def backup
  if is_readable?
    FileUtils.cp @path, "#{@path}.bk"
  end
end

#create(host, host_name, user) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/russh/accessor.rb', line 55

def create(host, host_name, user)
  # Ask the user for input
  @host = host
  @host_name = host_name
  @user = user
  # Check for read and write access
  if is_readable? && is_writable?
    backup
    open(@path, 'a+') do |f|
      # If the file is new don't add a newline.
      f.puts if f.readlines.size > 0
      # Format each entry
      f.puts format_entry
    end
  end
end

#is_existing?Boolean

Checks if the config file is existing. If not it creates a new one.

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/russh/accessor.rb', line 15

def is_existing?
  if File.exist? @path
    @is_existing = true
  else
    puts "You don't have an existing ssh config file. Creating a new one for you."
    File.new(@path, 'w')
  end
end

#is_readable?Boolean

Checks if the config file is readable.

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/russh/accessor.rb', line 25

def is_readable?
  begin
    @is_readable = File.readable? @path
  rescue => e
    puts "Couldn't read your ssh config: Error #{e}"
  end
end

#is_writable?Boolean

Checks if the config file is writable.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/russh/accessor.rb', line 34

def is_writable?
  begin
    @is_writable = File.writable? @path
  rescue => e
    puts "Couldn't write to your ssh config: Error #{e}"
  end
end

#readObject



49
50
51
52
53
# File 'lib/russh/accessor.rb', line 49

def read
  if is_readable?
    File.read @path
  end
end