Class: Chef::Knife::Edit

Inherits:
ChefFS::Knife show all
Defined in:
lib/chef/knife/edit.rb

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from ChefFS::Knife

#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

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#edit_text(text, extension) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chef/knife/edit.rb', line 52

def edit_text(text, extension)
  if !config[:disable_editing]
    Tempfile.open([ "knife-edit-", extension ]) do |file|
      # Write the text to a temporary file
      file.write(text)
      file.close

      # Let the user edit the temporary file
      if !system("#{config[:editor]} #{file.path}")
        raise "Please set EDITOR environment variable. See https://docs.chef.io/knife_using.html for details."
      end

      result_text = IO.read(file.path)

      return result_text if result_text != text
    end
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef/knife/edit.rb', line 20

def run
  # Get the matches (recursively)
  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