Class: Chef::Knife::AxeRole

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/axe_role.rb

Instance Method Summary collapse

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chef/knife/axe_role.rb', line 12

def run
  if @name_args[0].nil?
    show_usage
    ui.fatal("You must specify a role file")
    exit 1
  end

  loader = Chef::Knife::Core::ObjectLoader.new(Chef::Role, ui)

  @name_args.each do |role_file|
    role_name = File.basename(role_file).split('.')[0]
    role_ff = loader.load_from("roles", role_file)
    role_fc = Chef::Role.load(role_ff.to_hash["name"])

    diff = Diffy::Diff.new(
      JSON.pretty_generate(role_fc.to_hash),
      JSON.pretty_generate(role_ff.to_hash),
      :context => 1)

    if diff.to_s(:text).empty?
      ui.warn("Role #{role_fc.name} has not been changed")
    else
      puts "You are going to update role #{role_fc.name}:"
      puts "\n#{diff.to_s(:color)}\n"

      ui.confirm("Continue")
      role_ff.save
      ui.msg("Saved new version of role #{role_fc.name}")
    end
  end
end