summaryrefslogtreecommitdiff
path: root/lib/chef/chef_fs
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2013-10-16 08:37:30 -0700
committerJohn Keiser <jkeiser@opscode.com>2013-10-16 08:37:30 -0700
commitd320610cdcaca87ba370027523a5b0d221c96f34 (patch)
tree38ff3fb92d611e9531924b005c4d1b33647e0fd6 /lib/chef/chef_fs
parentcb8845818e9a130d1a85793675aa1216b936d168 (diff)
downloadchef-d320610cdcaca87ba370027523a5b0d221c96f34.tar.gz
Prettify data when writing it out in --local-mode
Diffstat (limited to 'lib/chef/chef_fs')
-rw-r--r--lib/chef/chef_fs/data_handler/client_data_handler.rb3
-rw-r--r--lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb13
-rw-r--r--lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb12
-rw-r--r--lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb8
-rw-r--r--lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb25
-rw-r--r--lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb2
-rw-r--r--lib/chef/chef_fs/file_system/file_system_entry.rb10
7 files changed, 61 insertions, 12 deletions
diff --git a/lib/chef/chef_fs/data_handler/client_data_handler.rb b/lib/chef/chef_fs/data_handler/client_data_handler.rb
index 2db3fa897f..4b6b8f5c79 100644
--- a/lib/chef/chef_fs/data_handler/client_data_handler.rb
+++ b/lib/chef/chef_fs/data_handler/client_data_handler.rb
@@ -9,12 +9,11 @@ class Chef
defaults = {
'name' => remove_dot_json(entry.name),
'clientname' => remove_dot_json(entry.name),
- 'orgname' => entry.org,
'admin' => false,
'validator' => false,
'chef_type' => 'client'
}
- if entry.org
+ if entry.respond_to?(:org) && entry.org
defaults['orgname'] = entry.org
end
result = normalize_hash(client, defaults)
diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb
index 48fc4b6860..5203637012 100644
--- a/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb
+++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb
@@ -55,10 +55,7 @@ class Chef
begin
Dir.entries(file_path).sort.
select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
- map do |child_name|
- segment_info = CookbookDir::COOKBOOK_SEGMENT_INFO[child_name.to_sym] || {}
- ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, segment_info[:ruby_only], segment_info[:recursive])
- end.
+ map { |child_name| make_child(child_name) }.
select { |entry| !(entry.dir? && entry.children.size == 0) }
rescue Errno::ENOENT
raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
@@ -70,7 +67,6 @@ class Chef
# Only the given directories will be uploaded.
return CookbookDir::COOKBOOK_SEGMENT_INFO.keys.include?(name.to_sym) && name != 'root_files'
end
-
super(name, is_dir)
end
@@ -84,6 +80,13 @@ class Chef
def canonical_cookbook_name(entry_name)
self.class.canonical_cookbook_name(entry_name)
end
+
+ protected
+
+ def make_child(child_name)
+ segment_info = CookbookDir::COOKBOOK_SEGMENT_INFO[child_name.to_sym] || {}
+ ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, segment_info[:ruby_only], segment_info[:recursive])
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb
index 07de961891..6541b07065 100644
--- a/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb
+++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_entry.rb
@@ -37,7 +37,7 @@ class Chef
begin
Dir.entries(file_path).sort.
select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
- map { |child_name| ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, ruby_only, recursive) }.
+ map { |child_name| make_child(child_name) }.
select { |entry| !(entry.dir? && entry.children.size == 0) }
rescue Errno::ENOENT
raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
@@ -70,6 +70,16 @@ class Chef
true
end
+
+ def write_pretty_json
+ false
+ end
+
+ protected
+
+ def make_child(child_name)
+ ChefRepositoryFileSystemCookbookEntry.new(child_name, self, nil, ruby_only, recursive)
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb
index 1f3f9f2b5e..6e16f18f24 100644
--- a/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb
+++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_cookbooks_dir.rb
@@ -40,7 +40,7 @@ class Chef
begin
Dir.entries(file_path).sort.
select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
- map { |child_name| ChefRepositoryFileSystemCookbookDir.new(child_name, self) }.
+ map { |child_name| make_child(child_name) }.
select do |entry|
# empty cookbooks and cookbook directories are ignored
if entry.children.size == 0
@@ -58,6 +58,12 @@ class Chef
def can_have_child?(name, is_dir)
is_dir && !name.start_with?('.')
end
+
+ protected
+
+ def make_child(child_name)
+ ChefRepositoryFileSystemCookbookDir.new(child_name, self)
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb
index 90627a1c75..3d3f58201e 100644
--- a/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb
+++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_entry.rb
@@ -31,6 +31,10 @@ class Chef
@data_handler = data_handler
end
+ def write_pretty_json
+ root.write_pretty_json
+ end
+
def data_handler
@data_handler || parent.data_handler
end
@@ -48,17 +52,36 @@ class Chef
!is_dir && name[-5..-1] == '.json'
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
+
+ def minimize(file_contents, entry)
+ object = JSONCompat.from_json(file_contents, :create_additions => false)
+ object = data_handler.normalize(object, entry)
+ object = data_handler.minimize(object, entry)
+ 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.
select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
- map { |child_name| ChefRepositoryFileSystemEntry.new(child_name, self) }
+ map { |child_name| make_child(child_name) }
rescue Errno::ENOENT
raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
end
+ protected
+
+ def make_child(child_name)
+ ChefRepositoryFileSystemEntry.new(child_name, self)
+ end
end
end
end
diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb
index 1cfc15d757..d615e0f415 100644
--- a/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb
+++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb
@@ -39,6 +39,8 @@ class Chef
@child_paths = child_paths
end
+ attr_accessor :write_pretty_json
+
attr_reader :child_paths
def children
diff --git a/lib/chef/chef_fs/file_system/file_system_entry.rb b/lib/chef/chef_fs/file_system/file_system_entry.rb
index e9ece366b5..46d4eb5538 100644
--- a/lib/chef/chef_fs/file_system/file_system_entry.rb
+++ b/lib/chef/chef_fs/file_system/file_system_entry.rb
@@ -40,14 +40,14 @@ class Chef
def children
begin
- Dir.entries(file_path).sort.select { |entry| entry != '.' && entry != '..' }.map { |entry| FileSystemEntry.new(entry, self) }
+ Dir.entries(file_path).sort.select { |entry| entry != '.' && entry != '..' }.map { |entry| make_child(entry) }
rescue Errno::ENOENT
raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
end
end
def create_child(child_name, file_contents=nil)
- child = FileSystemEntry.new(child_name, self)
+ child = make_child(child_name)
if file_contents
child.write(file_contents)
else
@@ -88,6 +88,12 @@ class Chef
file.write(content)
end
end
+
+ protected
+
+ def make_child(child_name)
+ FileSystemEntry.new(child_name, self)
+ end
end
end
end