summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2013-04-09 15:57:52 -0700
committerJohn Keiser <jkeiser@opscode.com>2013-06-07 13:12:30 -0700
commit0ce86fed091da2abf41353b495ed3703a76b8ad1 (patch)
treedfcebd404574f5ea25c748306c8cf172a1b53b36
parentdd57d6f8705880ba686399440ca295c301b41e30 (diff)
downloadchef-0ce86fed091da2abf41353b495ed3703a76b8ad1.tar.gz
Add knife edit
-rw-r--r--lib/chef/knife/edit.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/chef/knife/edit.rb b/lib/chef/knife/edit.rb
new file mode 100644
index 0000000000..ba05c61053
--- /dev/null
+++ b/lib/chef/knife/edit.rb
@@ -0,0 +1,75 @@
+require 'chef/chef_fs/knife'
+require 'chef/chef_fs/file_system'
+require 'chef/chef_fs/file_system/not_found_error'
+
+class Chef
+ class Knife
+ class Edit < Chef::ChefFS::Knife
+ banner "knife edit [PATTERN1 ... PATTERNn]"
+
+ common_options
+
+ option :local,
+ :long => '--local',
+ :boolean => true,
+ :description => "Show local files instead of remote"
+
+ def run
+ # Get the matches (recursively)
+ error = false
+ pattern_args.each do |pattern|
+ Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern) do |result|
+ if result.dir?
+ ui.error "#{format_path(result)}: is a directory" if pattern.exact_path
+ error = true
+ else
+ begin
+ new_value = edit_text(result.read, File.extname(result.name))
+ if new_value
+ result.write(new_value)
+ output "Updated #{format_path(result)}"
+ else
+ output "#{format_path(result)} unchanged!"
+ end
+ rescue Chef::ChefFS::FileSystem::OperationNotAllowedError => e
+ ui.error "#{format_path(e.entry)}: #{e.reason}."
+ error = true
+ rescue Chef::ChefFS::FileSystem::NotFoundError => e
+ ui.error "#{format_path(e.entry)}: No such file or directory"
+ error = true
+ end
+ end
+ end
+ end
+ if error
+ exit 1
+ end
+ end
+
+ def edit_text(text, extension)
+ if (!config[:disable_editing])
+ file = Tempfile.new([ 'knife-edit-', extension ])
+ begin
+ # Write the text to a temporary file
+ file.open
+ file.write(text)
+ file.close
+
+ # Let the user edit the temporary file
+ if !system("#{config[:editor]} #{file.path}")
+ raise "Please set EDITOR environment variable"
+ end
+
+ file.open
+ result_text = file.read
+ return result_text if result_text != text
+
+ ensure
+ file.close!
+ end
+ end
+ end
+ end
+ end
+end
+