Class: Chef::Knife::Edit
Instance Method Summary
collapse
#chef_fs, #configure_chef, #create_chef_fs, #create_local_fs, deps, #discover_repo_dir, #format_path, inherited, #local_fs, #parallelize, #pattern_arg_from, #pattern_args, #pattern_args_from
Instance Method Details
#edit_text(text, extension) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/chef/knife/edit.rb', line 68
def edit_text(text, extension)
unless config[:disable_editing]
Tempfile.open([ "knife-edit-", extension ]) do |file|
file.write(text)
file.close
unless system("#{config[:editor]} #{file.path}")
raise "Please set EDITOR environment variable. See https://docs.chef.io/workstation/26/tools/knife/set_up/#set-a-text-editor for details."
end
result_text = File.read(file.path)
return result_text if result_text != text
end
end
end
|
#run ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/chef/knife/edit.rb', line 36
def run
error = false
pattern_args.each do |pattern|
Chef::ChefFS::FileSystem.list(config[:local] ? local_fs : chef_fs, pattern).each 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
|