summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom May <thom@chef.io>2016-04-18 18:46:08 +0100
committerThom May <thom@chef.io>2016-04-21 08:37:08 +0100
commit4f646dbc1361a2f8878c5e204f29fcc84c0bba52 (patch)
tree178719c8fa4ecb162613189cb71eb3c15fc2e22d
parent89ce17a2f2936070d65fe2c83dc472aa41979d1a (diff)
downloadchef-4f646dbc1361a2f8878c5e204f29fcc84c0bba52.tar.gz
Combine and tidy two of the file_system_entry impls
This merges together ChefRepositoryFileSystemEntry and FileSystemEntry, since they weren't really being used separately.
-rw-r--r--lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb6
-rw-r--r--lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb2
-rw-r--r--lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb93
-rw-r--r--lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb4
-rw-r--r--lib/chef/chef_fs/file_system/repository/file_system_entry.rb96
5 files changed, 75 insertions, 126 deletions
diff --git a/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb b/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
new file mode 100644
index 0000000000..eb2c3e8ff6
--- /dev/null
+++ b/lib-backcompat/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
@@ -0,0 +1,6 @@
+require "chef/chef_fs/file_system/repository/file_system_entry"
+
+module Chef::ChefFS::FileSystem::Repository
+ Chef.log_deprecation "Chef::ChefFS::FileSystem::Repository::ChefRepositoryFileSystemEntry is deprecated. Please use FileSystemEntry directly"
+ ChefRepositoryFileSystemEntry = FileSystemEntry
+end
diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb
index f533aa1080..703c0fc635 100644
--- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb
+++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_cookbook_entry.rb
@@ -16,7 +16,7 @@
# limitations under the License.
#
-require "chef/chef_fs/file_system/repository/chef_repository_file_system_entry"
+require "chef/chef_fs/file_system/repository/file_system_entry"
require "chef/chef_fs/file_system/repository/cookbooks_dir"
require "chef/chef_fs/file_system/exceptions"
diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
deleted file mode 100644
index e490daa584..0000000000
--- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_entry.rb
+++ /dev/null
@@ -1,93 +0,0 @@
-#
-# Author:: John Keiser (<jkeiser@chef.io>)
-# Author:: Ho-Sheng Hsiao (<hosh@chef.io>)
-# Copyright:: Copyright 2012-2016, Chef Software Inc.
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require "chef/chef_fs/file_system/repository/file_system_entry"
-require "chef/chef_fs/file_system/exceptions"
-
-class Chef
- module ChefFS
- module FileSystem
- module Repository
- # ChefRepositoryFileSystemEntry works just like FileSystemEntry,
- # except can inflate Chef objects
- class ChefRepositoryFileSystemEntry < FileSystemEntry
- def initialize(name, parent, file_path = nil, data_handler = nil)
- super(name, parent, file_path)
- @data_handler = data_handler
- end
-
- 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 chef_object
- begin
- return data_handler.chef_object(Chef::JSONCompat.parse(read))
- rescue
- Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{$!}")
- end
- nil
- end
-
- def can_have_child?(name, is_dir)
- !is_dir && name[-5..-1] == ".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 write(file_contents)
- if file_contents && write_pretty_json && name[-5..-1] == ".json"
- file_contents = minimize(file_contents, self)
- end
- super(file_contents)
- end
- alias :create :write
-
- 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
-
- protected
-
- def make_child_entry(child_name)
- ChefRepositoryFileSystemEntry.new(child_name, self)
- end
- end
- end
- end
- end
-end
diff --git a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb
index 263d1b2b09..1b26ced372 100644
--- a/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb
+++ b/lib/chef/chef_fs/file_system/repository/chef_repository_file_system_root_dir.rb
@@ -30,7 +30,7 @@ require "chef/chef_fs/file_system/repository/policy_groups_dir"
require "chef/chef_fs/file_system/repository/roles_dir"
require "chef/chef_fs/file_system/repository/users_dir"
require "chef/chef_fs/file_system/repository/client_keys_dir"
-require "chef/chef_fs/file_system/repository/chef_repository_file_system_entry"
+require "chef/chef_fs/file_system/repository/file_system_entry"
require "chef/chef_fs/file_system/repository/policies_dir"
require "chef/chef_fs/file_system/repository/versioned_cookbooks_dir"
require "chef/chef_fs/file_system/multiplexed_dir"
@@ -150,7 +150,7 @@ class Chef
existing_paths = root_paths.select { |path| File.exists?(path) }
if existing_paths.size > 0
MultiplexedDir.new(existing_paths.map do |path|
- dir = ChefRepositoryFileSystemEntry.new(name, parent, path)
+ dir = FileSystemEntry.new(name, parent, path)
dir.write_pretty_json = !!write_pretty_json
dir
end)
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