diff options
author | danielsdeleo <dan@chef.io> | 2016-02-05 12:48:00 -0800 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2016-02-16 15:26:43 -0800 |
commit | a73e4b50789a50bd7e8b29d5470e05640a1fc551 (patch) | |
tree | 8d384649eec12a9f340b55e28ce9a3c27af5e5c6 /lib/chef/chef_fs | |
parent | 5a787bcbebac7a3b9a134b557055b814c0e981bb (diff) | |
download | chef-a73e4b50789a50bd7e8b29d5470e05640a1fc551.tar.gz |
Inlince cheffs data bags dir 2 levels
Diffstat (limited to 'lib/chef/chef_fs')
-rw-r--r-- | lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb | 99 |
1 files changed, 96 insertions, 3 deletions
diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb index 1777a8982f..80828c936c 100644 --- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb +++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_data_bags_dir.rb @@ -28,7 +28,10 @@ class Chef #class ChefRepositoryFileSystemDataBagsDir < ChefRepositoryFileSystemEntry # With ChefRepositoryFileSystemEntry inlined: - class ChefRepositoryFileSystemDataBagsDir < FileSystemEntry + #class ChefRepositoryFileSystemDataBagsDir < FileSystemEntry + + # With FileSystemEntry inlined + class ChefRepositoryFileSystemDataBagsDir < BaseFSDir # Original ## def initialize(name, parent, path = nil) @@ -41,9 +44,16 @@ class Chef ## @data_handler = data_handler ## end + # FileSystemEntry#initialize + ## def initialize(name, parent, file_path = nil) + ## super(name, parent) + ## @file_path = file_path || "#{parent.file_path}/#{name}" + ## end + # inlined initialize - def initialize(name, parent, path = nil) - super(name, parent, path) + def initialize(name, parent, file_path = nil) + super(name, parent) + @file_path = file_path || "#{parent.file_path}/#{name}" @data_handler = Chef::ChefFS::DataHandler::DataBagItemDataHandler.new end @@ -116,6 +126,89 @@ class Chef ChefRepositoryFileSystemEntry.new(child_name, self) end + public + + ############################## + # Inlined from FileSystemEntry + ############################## + + attr_reader :file_path + + def path_for_printing + file_path + 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 + end + + def create_child(child_name, file_contents=nil) + child = make_child_entry(child_name) + if child.exists? + raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) + end + 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 + end + child + end + + def dir? + File.directory?(file_path) + end + + def delete(recurse) + begin + if dir? + if !recurse + raise MustDeleteRecursivelyError.new(self, $!) + end + FileUtils.rm_r(file_path) + else + File.delete(file_path) + end + rescue Errno::ENOENT + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + end + end + + def exists? + File.exists?(file_path) && (parent.nil? || parent.can_have_child?(name, dir?)) + end + + def read + begin + File.open(file_path, "rb") {|f| f.read} + rescue Errno::ENOENT + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + end + end + + def write(content) + File.open(file_path, "wb") do |file| + file.write(content) + end + end + + # Overridden in superclass (ChefRepositoryFileSystemEntry) + ## protected + + ## def make_child_entry(child_name) + ## FileSystemEntry.new(child_name, self) + ## end end end |