summaryrefslogtreecommitdiffstats
path: root/lib/puppet/file_serving/content.rb
blob: c1ecff749bb077bcb892be55a71229e82a83ed90 (plain)
1
2
3
4
5
6
7
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
#  Created by Luke Kanies on 2007-10-16.
#  Copyright (c) 2007. All rights reserved.

require 'puppet/indirector'
require 'puppet/file_serving'
require 'puppet/file_serving/base'
require 'puppet/file_serving/indirection_hooks'

# A class that handles retrieving file contents.
# It only reads the file when its content is specifically
# asked for.
class Puppet::FileServing::Content < Puppet::FileServing::Base
    extend Puppet::Indirector
    indirects :file_content, :extend => Puppet::FileServing::IndirectionHooks

    attr_writer :content

    def self.supported_formats
        [:raw]
    end

    def self.from_raw(content)
        instance = new("/this/is/a/fake/path")
        instance.content = content
        instance
    end

    # Collect our data.
    def collect
        return if stat.ftype == "directory"
        content
    end

    # Read the content of our file in.
    def content
        unless defined?(@content) and @content
            # This stat can raise an exception, too.
            raise(ArgumentError, "Cannot read the contents of links unless following links") if stat().ftype == "symlink" 

            @content = ::File.read(full_path())
        end
        @content
    end

    def to_raw
        content
    end
end