class Chef::Knife::NodeEditor

Attributes

config[R]
node[R]
ui[R]

Public Class Methods

new(node, ui, config) click to toggle source

@param node [Chef::Node] @param ui [Chef::Knife::UI] @param config [Hash]

# File lib/chef/knife/core/node_editor.rb, line 32
def initialize(node, ui, config)
  @node, @ui, @config = node, ui, config
end

Public Instance Methods

apply_updates(updated_data) click to toggle source

@api private

# File lib/chef/knife/core/node_editor.rb, line 98
def apply_updates(updated_data)
  if node.name && node.name != updated_data["name"]
    ui.warn "Changing the name of a node results in a new node being created, #{node.name} will not be modified or removed."
    ui.confirm "Proceed with creation of new node"
  end

  data = updated_data.dup

  unless config[:all_attributes]
    data["automatic"] = node.automatic_attrs
    data["default"] = node.default_attrs
    data["override"] = node.override_attrs
  end

  @updated_node = Node.from_hash(data)
end
edit_node() click to toggle source

Opens the node data (as JSON) in the user's editor and returns a new {Chef::Node} reflecting the user's changes.

@return [Chef::Node]

# File lib/chef/knife/core/node_editor.rb, line 40
def edit_node
  abort "You specified the --disable_editing option, nothing to edit" if config[:disable_editing]
  assert_editor_set!

  updated_node_data = ui.edit_hash(view)
  apply_updates(updated_node_data)
  @updated_node
end
updated?() click to toggle source

Returns an array of the names of properties that have been changed or false if none were changed.

@return [Array<String>] if any properties have been changed. @return [false] if no properties have been changed.

# File lib/chef/knife/core/node_editor.rb, line 54
def updated?
  return false if @updated_node.nil?

  pristine_copy = Chef::JSONCompat.parse(Chef::JSONCompat.to_json(node))
  updated_copy  = Chef::JSONCompat.parse(Chef::JSONCompat.to_json(@updated_node))

  updated_properties = %w{
    name
    chef_environment
    automatic
    default
    normal
    override
    policy_name
    policy_group
    run_list
  }.reject do |key|
    pristine_copy[key] == updated_copy[key]
  end

  updated_properties.any? && updated_properties
end
view() click to toggle source

@api private

# File lib/chef/knife/core/node_editor.rb, line 78
def view
  result = {
    "name" => node.name,
    "chef_environment" => node.chef_environment,
    "normal" => node.normal_attrs,
    "policy_name" => node.policy_name,
    "policy_group" => node.policy_group,
    "run_list" => node.run_list,
  }

  if config[:all_attributes]
    result["default"]   = node.default_attrs
    result["override"]  = node.override_attrs
    result["automatic"] = node.automatic_attrs
  end

  result
end

Private Instance Methods

abort(message) click to toggle source
# File lib/chef/knife/core/node_editor.rb, line 117
def abort(message)
  ui.error(message)
  exit 1
end
assert_editor_set!() click to toggle source
# File lib/chef/knife/core/node_editor.rb, line 122
def assert_editor_set!
  unless config[:editor]
    abort "You must set your EDITOR environment variable or configure your editor via knife.rb"
  end
end