Class: Gitlab::Template::Finders::GlobalTemplateFinder

Inherits:
BaseTemplateFinder show all
Defined in:
lib/gitlab/template/finders/global_template_finder.rb

Instance Method Summary collapse

Methods inherited from BaseTemplateFinder

#category_directory, filter_regex

Constructor Details

#initialize(base_dir, extension, categories = {}, excluded_patterns: []) ⇒ GlobalTemplateFinder

Returns a new instance of GlobalTemplateFinder.


8
9
10
11
12
13
14
# File 'lib/gitlab/template/finders/global_template_finder.rb', line 8

def initialize(base_dir, extension, categories = {}, excluded_patterns: [])
  @categories = categories
  @extension  = extension
  @excluded_patterns = excluded_patterns

  super(base_dir)
end

Instance Method Details

#find(key) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/template/finders/global_template_finder.rb', line 20

def find(key)
  return if excluded?(key)

  file_name = "#{key}#{@extension}"

  # The key is untrusted input, so ensure we can't be directed outside
  # of base_dir
  Gitlab::Utils.check_path_traversal!(file_name)

  directory = select_directory(file_name)
  directory ? File.join(category_directory(directory), file_name) : nil
end

#list_files_for(dir) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/template/finders/global_template_finder.rb', line 33

def list_files_for(dir)
  dir = "#{dir}/" unless dir.end_with?('/')

  Dir.glob(File.join(dir, "*#{@extension}")).select do |f|
    next if excluded?(f)

    f =~ self.class.filter_regex(@extension)
  end
end

#read(path) ⇒ Object


16
17
18
# File 'lib/gitlab/template/finders/global_template_finder.rb', line 16

def read(path)
  File.read(path)
end