Class: SSH::Bookmarks::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh/bookmarks.rb

Overview

This class handle the configuration file

Constant Summary collapse

PATH =

Constant which stored path of ssh config file

"#{ENV['HOME']}/.ssh/config"
OPTIONS =

Constant which stored of array custom options of ssh config

%w(hidden)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

load and initialize parsing configuration file



19
20
21
22
23
24
# File 'lib/ssh/bookmarks.rb', line 19

def initialize
  new = File.readlines(PATH).map(&:split)
  @data = construct new
rescue => err
  abort "Error: config is invalid.\nReason: #{err}"
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



85
86
87
# File 'lib/ssh/bookmarks.rb', line 85

def data
  @data
end

Instance Method Details

#comment?(block) ⇒ Boolean (private)

Determine whether the block is comment?

Parameters:

  • block (String)

    the testable string

Returns:

  • (Boolean)

    return current state of block



70
71
72
# File 'lib/ssh/bookmarks.rb', line 70

def comment?(block)
  block[0] == '#' ? true : false
end

#construct(raw) ⇒ Hash (private)

Parse and build configuration

Parameters:

  • raw (Array)

    original data ssh config file

Returns:

  • (Hash)

    container of parsed config



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ssh/bookmarks.rb', line 29

def construct(raw)
  container = []
  raw.each_with_index do |block, index|
    next if block.empty? || comment?(block)
    if block[0] == 'Host'
      container << {}
      container.last.merge!(options: options(raw, index))
    end
    container.last.merge!(block[0].downcase.to_sym => block[1]) if defined? container
  end
  container
end

#exist?Boolean (private)

Determine whether the config is exist

Returns:

  • (Boolean)

    return true if config exist or abort the process



76
77
78
79
80
81
82
83
# File 'lib/ssh/bookmarks.rb', line 76

def exist?
  # ssh configuration file
  if File.exist?(PATH)
    return true
  else
    abort "Error: no configuration file is found.\nExpected path: #{path}"
  end
end

#option?(block) ⇒ Boolean (private)

Determine whether the block in the list of allowed

Parameters:

  • block (String)

    the testable string

Returns:

  • (Boolean)

    returns the current state of block



63
64
65
# File 'lib/ssh/bookmarks.rb', line 63

def option?(block)
  OPTIONS.include?(block[1])
end

#options(raw, index) ⇒ Array (private)

Parse options placed in comments which allowed in OPTIONS constant

Parameters:

  • raw (Array)

    original data of ssh config file

  • index (Integer)

    index which used for definition of membership of comments

Returns:

  • (Array)

    list of options for specific group which is found in specific range



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ssh/bookmarks.rb', line 46

def options(raw, index)
  @prev_index = 0 unless defined? @prev_index
  options = {}
  index += 1 if index > 0
  # catch options between range
  raw[@prev_index..index].each do |opt|
    if comment?(opt) && option?(opt)
      options.merge!(opt[1].downcase.to_sym => true)
    end
  end
  @prev_index = index
  options
end