summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs/file_system/repository/file_system_entry.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/chef_fs/file_system/repository/file_system_entry.rb')
-rw-r--r--lib/chef/chef_fs/file_system/repository/file_system_entry.rb96
1 files changed, 66 insertions, 30 deletions
diff --git a/lib/chef/chef_fs/file_system/repository/file_system_entry.rb b/lib/chef/chef_fs/file_system/repository/file_system_entry.rb
index 20e2eb7efc..45ae002521 100644
--- a/lib/chef/chef_fs/file_system/repository/file_system_entry.rb
+++ b/lib/chef/chef_fs/file_system/repository/file_system_entry.rb
@@ -27,26 +27,64 @@ class Chef
module FileSystem
module Repository
class FileSystemEntry < BaseFSDir
- def initialize(name, parent, file_path = nil)
+ def initialize(name, parent, file_path = nil, data_handler = nil)
super(name, parent)
@file_path = file_path || "#{parent.file_path}/#{name}"
+ @data_handler = data_handler
end
attr_reader :file_path
+ def write_pretty_json=(value)
+ @write_pretty_json = value
+ end
+
+ def write_pretty_json
+ @write_pretty_json.nil? ? root.write_pretty_json : @write_pretty_json
+ end
+
+ def data_handler
+ @data_handler || parent.data_handler
+ end
+
def path_for_printing
file_path
end
+ def chef_object
+ data_handler.chef_object(Chef::JSONCompat.parse(read))
+ rescue
+ Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{$!}")
+ nil
+ end
+
+ def can_have_child?(name, is_dir)
+ !is_dir && File.extname(name) == ".json"
+ end
+
+ def name_valid?
+ !name.start_with?(".")
+ end
+
+ # basic implementation to support Repository::Directory API
+ def fs_entry_valid?
+ name_valid? && File.exist?(file_path)
+ end
+
+ def minimize(file_contents, entry)
+ object = Chef::JSONCompat.parse(file_contents)
+ object = data_handler.normalize(object, entry)
+ object = data_handler.minimize(object, entry)
+ Chef::JSONCompat.to_json_pretty(object)
+ end
+
def children
# Except cookbooks and data bag dirs, all things must be json files
- begin
- Dir.entries(file_path).sort.
- map { |child_name| make_child_entry(child_name) }.
- select { |child| child && can_have_child?(child.name, child.dir?) }
- rescue Errno::ENOENT
- raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
- end
+ Dir.entries(file_path).sort.
+ map { |child_name| make_child_entry(child_name) }.
+ select { |new_child| new_child.fs_entry_valid? && can_have_child?(new_child.name, new_child.dir?) }
+ rescue Errno::ENOENT
+ raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
def create_child(child_name, file_contents = nil)
@@ -57,13 +95,11 @@ class Chef
if file_contents
child.write(file_contents)
else
- begin
- Dir.mkdir(child.file_path)
- rescue Errno::EEXIST
- raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child)
- end
+ Dir.mkdir(child.file_path)
end
child
+ rescue Errno::EEXIST
+ raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child)
end
def dir?
@@ -71,18 +107,16 @@ class Chef
end
def delete(recurse)
- begin
- if dir?
- if !recurse
- raise MustDeleteRecursivelyError.new(self, $!)
- end
- FileUtils.rm_r(file_path)
- else
- File.delete(file_path)
+ if dir?
+ if !recurse
+ raise MustDeleteRecursivelyError.new(self, $!)
end
- rescue Errno::ENOENT
- raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
+ FileUtils.rm_r(file_path)
+ else
+ File.delete(file_path)
end
+ rescue Errno::ENOENT
+ raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
def exists?
@@ -90,18 +124,20 @@ class Chef
end
def read
- begin
- File.open(file_path, "rb") { |f| f.read }
- rescue Errno::ENOENT
- raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
- end
+ File.open(file_path, "rb") { |f| f.read }
+ rescue Errno::ENOENT
+ raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
- def write(content)
+ def write(file_contents)
+ if file_contents && write_pretty_json && File.extname(name) == ".json"
+ file_contents = minimize(file_contents, self)
+ end
File.open(file_path, "wb") do |file|
- file.write(content)
+ file.write(file_contents)
end
end
+ alias :create :write
protected