Class: Chef::Knife::Edit

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

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, KNIFE_ROOT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL, VERSION

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_defaults, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, #root_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

Constructor Details

This class inherits a constructor from Chef::Knife

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|
      # Write the text to a temporary file
      file.write(text)
      file.close

      # Let the user edit the temporary file
      unless system("#{config[:editor]} #{file.path}")
        raise "Please set EDITOR environment variable. See https://docs.chef.io/workstation/knife_setup/#setting-your-text-editor for details."
      end

      result_text = IO.read(file.path)

      return result_text if result_text != text
    end
  end
end

#runObject



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
  # 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