class SSH::Bookmarks::Config

This class handle the configuration file

Constants

OPTIONS

Constant which stored of array custom options of ssh config

PATH

Constant which stored path of ssh config file

Attributes

data[R]

Public Class Methods

new() click to toggle source

load and initialize parsing configuration file

# 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

Private Instance Methods

comment?(block) click to toggle source

Determine whether the block is comment? @param block [String] the testable string @return [Boolean] return current state of block

# File lib/ssh/bookmarks.rb, line 70
def comment?(block)
  block[0] == '#' ? true : false
end
construct(raw) click to toggle source

Parse and build configuration @param raw [Array] original data ssh config file @return [Hash] container of parsed config

# 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?() click to toggle source

Determine whether the config is exist @return [Boolean] return true if config exist or abort the process

# 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) click to toggle source

Determine whether the block in the list of allowed @param block [String] the testable string @return [Boolean] returns the current state of block

# File lib/ssh/bookmarks.rb, line 63
def option?(block)
  OPTIONS.include?(block[1])
end
options(raw, index) click to toggle source

Parse options placed in comments which allowed in OPTIONS constant @param raw [Array] original data of ssh config file @param index [Integer] index which used for definition of membership of comments @return [Array] list of options for specific group which is found in specific range

# 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