Module: PDK::Template::Fetcher

Defined in:
lib/pdk/template/fetcher.rb,
lib/pdk/template/fetcher/git.rb,
lib/pdk/template/fetcher/local.rb

Defined Under Namespace

Classes: AbstractFetcher, Git, Local

Class Method Summary collapse

Class Method Details

.instance(uri, options = {}) ⇒ PDK::Template::Fetcher::AbstractTemplateFetcher

Returns a Template Fetcher implementation for the given Template URI

Parameters:

  • uri (PDK::Util::TemplateURI)

    The URI of the template to fetch

  • options (Hash{Object => Object}) (defaults to: {})

    A list of options to pass through to the fetcher.

Returns:

  • (PDK::Template::Fetcher::AbstractTemplateFetcher)

    An instance of a class which implements the AbstractFetcher class



14
15
16
17
# File 'lib/pdk/template/fetcher.rb', line 14

def self.instance(uri, options = {})
  return Git.new(uri, options) if Git.fetchable?(uri, options)
  Local.new(uri, options)
end

.with(uri, options = {}) {|fetcher| ... } ⇒ void

This method returns an undefined value.

Creates an instance of a PDK::Template::Fetcher::AbstractTemplateFetcher object with the path or URL to the template and the block of code to run to be run while the template is fetched.

The fetched directory is only guaranteed to be available on disk within the scope of the block passed to this method.

Examples:

Using a git repository as a template

PDK::Template::Fetcher.with('https://github.com/puppetlabs/pdk-templates') do |fetcher|
end

Parameters:

  • uri (PDK::Util::TemplateURI)

    The URI of the template to fetch.

  • options (Hash{Object => Object}) (defaults to: {})

    A list of options to pass through to the fetcher.

Yield Parameters:

  • fetcher (PDK::Template::Fetcher::AbstractTemplateFetcher)

    The initialised fetcher with the template already fetched

Raises:

  • (ArgumentError)

    If no block is given to this method.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pdk/template/fetcher.rb', line 37

def self.with(uri, options = {})
  raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name } unless block_given?
  fetcher = instance(uri, options)

  begin
    fetcher.fetch!
    yield fetcher
  ensure
    # If the the path is temporary, clean it up
    PDK::Util::Filesystem.rm_rf(fetcher.path) if fetcher.temporary
  end
  nil
end