Sha256: 22c5e1ea9ac6977a3f75728a12b31759af2620abd36b98796938ba7f122b6088

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

# Read a file on localhost and return its contents using ruby's `File.read`. This will
# only read files on the machine you run Bolt on.
Puppet::Functions.create_function(:'file::read', Puppet::Functions::InternalFunction) do
  # @param filename Absolute path or Puppet file path.
  # @return The file's contents.
  # @example Read a file from disk
  #   file::read('/tmp/i_dumped_this_here')
  # @example Read a file from the modulepath
  #   file::read('example/VERSION')
  dispatch :read do
    scope_param
    required_param 'String[1]', :filename
    return_type 'String'
  end

  def read(scope, filename)
    # Send Analytics Report
    executor = Puppet.lookup(:bolt_executor) {}
    executor&.report_function_call(self.class.name)

    future = executor&.future || Puppet.lookup(:future) { {} }
    fallback = future.fetch('file_paths', false)

    # Find the file path if it exists, otherwise return nil
    found = Bolt::Util.find_file_from_scope(filename, scope, fallback)
    unless found && Puppet::FileSystem.exist?(found)
      raise Puppet::ParseErrorWithIssue.from_issue_and_stack(
        Puppet::Pops::Issues::NO_SUCH_FILE_OR_DIRECTORY, file: filename
      )
    end
    executor&.report_file_source(self.class.name, filename)
    File.read(found)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bolt-3.17.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.16.1 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.16.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.15.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.14.1 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.13.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.12.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.11.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.10.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.9.2 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.9.1 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.9.0 bolt-modules/file/lib/puppet/functions/file/read.rb
bolt-3.8.1 bolt-modules/file/lib/puppet/functions/file/read.rb