summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/file.rb
blob: 4f231e9ec5ba790c8ef2ef2ef91ab8c01a8e6bb7 (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
50
51
52
53
54
55
56
require 'puppet/indirector/terminus'

# An empty terminus type, meant to just return empty objects.
class Puppet::Indirector::File < Puppet::Indirector::Terminus
    def destroy(file)
        if respond_to?(:path)
            path = path(file.name)
        else
            path = file.path
        end
        raise Puppet::Error.new("File %s does not exist; cannot destroy" % [file]) unless File.exist?(path)

        begin
            File.unlink(path)
        rescue => detail
            raise Puppet::Error, "Could not remove %s: %s" % [file, detail]
        end
    end

    def find(name)
        if respond_to?(:path)
            path = path(name)
        else
            path = name
        end

        return nil unless File.exist?(path)

        begin
            content = File.read(path)
        rescue => detail
            raise Puppet::Error, "Could not retrieve path %s: %s" % [path, detail]
        end

        file = model.new(name)
        file.content = content
        return file
    end

    def save(file)
        if respond_to?(:path)
            path = path(file.name)
        else
            path = file.path
        end
        dir = File.dirname(path)

        raise Puppet::Error.new("Cannot save %s; parent directory %s does not exist" % [file, dir]) unless File.directory?(dir)

        begin
            File.open(path, "w") { |f| f.print file.content }
        rescue => detail
            raise Puppet::Error, "Could not write %s: %s" % [file, detail]
        end
    end
end