Module: KubeDeployTools::FileFilter

Defined in:
lib/kube_deploy_tools/file_filter.rb

Constant Summary collapse

VALID_FILTER_NAMES =
%w(include_dir exclude_dir)

Class Method Summary collapse

Class Method Details

.filter_files(filters:, files_path:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kube_deploy_tools/file_filter.rb', line 8

def self.filter_files(filters:, files_path:)
  all_files = Dir[File.join(files_path, '**', '*')].to_set
  filtered_files = if filters.any? { |k, _| k =~ /include_/ }
    Set.new
  else
    Set.new(all_files)
  end

  filters.each do |action, globpath|
    if action =~ /_dir$/
      # ensure globpath correctly selects an entire dir recursively
      # always starts with files_path
      # always ends with /**/* (to recusively select entire dir)
      globpath = File.join(files_path, globpath.gsub(/^[\/*]+|[\/*]+$/, ""), '**', '*')
    end

    case action
    when /^include_(files|dir)$/
      filtered_files.merge( all_files.select{ |f| File.fnmatch?(globpath, f, File::FNM_PATHNAME) } )
    when /^exclude_(files|dir)$/
      filtered_files.reject!{ |f| File.fnmatch?(globpath, f, File::FNM_PATHNAME) }
    else
      raise "you want to #{action}: wat do"
    end
  end
  Logger.debug("\nYour filter generates following paths: \n#{filtered_files.to_a.join("\n")}")
  filtered_files
end

.filters_from_hash(h) ⇒ Object



37
38
39
40
41
# File 'lib/kube_deploy_tools/file_filter.rb', line 37

def self.filters_from_hash(h)
  VALID_FILTER_NAMES.flat_map do |filter_name|
    Array(h[filter_name]).map { |dir_path| [filter_name, dir_path] }
  end
end