class DTK::Client::CLI::DirectoryParser::FileSystem

For Finding relevant content when files in a vanilla file system

Public Instance Methods

matching_file_obj?(file_types, opts = {}) click to toggle source

file_types - a single or array of FileObj objects opts can have keys

:file_path - string
:dir_path - string

Returns FileObj object or nil that match a file_type

# File lib/cli/directory_parser/file_system.rb, line 28
def matching_file_obj?(file_types, opts = {})
  file_type, file_path = matching_type_and_path?(Array(file_types), opts)
  file_obj_opts = {
    dir_path: opts[:dir_path],
    current_dir: OsUtil.current_dir,
    content: get_content?(file_path)
  }
  FileObj.new(file_type, file_path, file_obj_opts)
end

Private Instance Methods

get_content?(file_path) click to toggle source
# File lib/cli/directory_parser/file_system.rb, line 40
def get_content?(file_path)
  File.open(file_path).read if file_path and File.exists?(file_path)
end
is_backup_file?(file_path) click to toggle source
# File lib/cli/directory_parser/file_system.rb, line 97
def is_backup_file?(file_path)
  regex = Regexp.new("\.bak\.dtk\.(service|module)\.(yml|yaml)$")
  file_path =~ regex
end
matching_file_paths(dir_path, path_info) click to toggle source

returns an array of strings that are file paths; except bakup files (e.g. bak.dtk.service.yaml)

# File lib/cli/directory_parser/file_system.rb, line 92
def matching_file_paths(dir_path, path_info)
  return [] if File.exist?("#{dir_path}/.nested_module")
  Dir.glob("#{dir_path}/*", File::FNM_DOTMATCH).select { |file_path| File.file?(file_path) and !is_backup_file?(file_path) and path_info.matches?(file_path) }
end
matching_type_and_path?(file_types, opts = {}) click to toggle source

This method finds the base dsl file if it exists and returns its path opts can have keys:

:dir_path
:file_path
:flag

returns nil or [file_type, path]

# File lib/cli/directory_parser/file_system.rb, line 50
def matching_type_and_path?(file_types, opts = {})
  matches = 
    if path = opts[:file_path]
      file_types.map { | file_type | file_type.matches?(path) && [file_type, path] }.compact
    else
      base_dir = OsUtil.home_dir
      current_dir = opts[:dir_path] || OsUtil.current_dir
      most_nested_matching_types_and_paths(file_types, current_dir, base_dir, flag: opts[:flag])
    end
  
  case matches.size
  when 0
    nil
  when 1
    matches.first
  else
    MultipleMatches.resolve(matches)
  end
end
most_nested_matching_types_and_paths(file_types, current_dir, base_dir, opts = {}) click to toggle source

opts can have keys

:flag
# File lib/cli/directory_parser/file_system.rb, line 72
def most_nested_matching_types_and_paths(file_types, current_dir, base_dir, opts = {})
  # matches will be an array of [file_type, path]
  matches = []
  file_types.each do |file_type| 
    matching_file_paths(current_dir, file_type).each { |path|  matches << [file_type, path] }
  end

  return matches unless matches.empty?

  next_level_matches = []
  unless current_dir == base_dir or opts[:flag]
    if parent_path = OsUtil.parent_dir?(current_dir)
      next_level_matches = most_nested_matching_types_and_paths(file_types, parent_path, base_dir, opts)
    end
  end
  next_level_matches
end