Skip to content

Instantly share code, notes, and snippets.

@matschaffer
Created January 29, 2016 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matschaffer/89ca2d4fc3ad5df67f1c to your computer and use it in GitHub Desktop.
Save matschaffer/89ca2d4fc3ad5df67f1c to your computer and use it in GitHub Desktop.
require 'chef/knife'
module Chef
module Knife
class DiffEnvironment < Chef::Knife
banner 'knife diff environment ENVIRONMENT_FILE'
deps do
require 'chef/environment'
require 'chef/knife/core/object_loader'
require 'chef/json_compat'
require 'knife_neat/diff'
end
def run
file = name_args.first
unless file
show_usage
ui.fatal('You must specify an environment name')
ui.fatal("ex: #{$0} diff environment chef/environments/development.rb")
exit 1
end
unless File.exist?(file)
ui.fatal("Environment file #{file} could not be found")
exit 1
end
loader = Chef::Knife::Core::ObjectLoader.new(Chef::Environment, ui)
local_env = loader.load_from('environments', file)
local_result = Chef::JSONCompat.to_json_pretty(local_env)
env_name = local_env.name
if env_name.empty?
ui.fatal("Environment file #{file} must contain a name")
exit 1
end
chef_env = Chef::Environment.load(env_name)
chef_result = Chef::JSONCompat.to_json_pretty(chef_env)
ui.info "Comparison of '#{env_name}' environment from #{server_url} (---) with local file '#{file}' (+++):"
diff = KnifeNeat::Diff.diff(chef_result, local_result)
ui.info(diff)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment