Commit a3a38f6e authored by Bryan McLellan's avatar Bryan McLellan
Browse files

CHEF-1914: load all environments with one command

Adds support for:
knife environment from file foo.json bar.json
knife environment from file -a
parent c6afe57f
......@@ -25,26 +25,59 @@ class Chef
require 'chef/knife/core/object_loader'
end
banner "knife environment from file FILE (options)"
banner "knife environment from file FILE [FILE..] (options)"
option :all,
:short => "-a",
:long => "--all",
:description => "Upload all environments"
def loader
@loader ||= Knife::Core::ObjectLoader.new(Chef::Environment, ui)
end
def environments_path
@environments_path ||= "environments"
end
def run
if @name_args[0].nil?
show_usage
ui.fatal("You must specify a file to load")
exit 1
end
def find_all_environments
loader.find_all_objects("./#{environments_path}/")
end
def load_all_environments
environments = find_all_environments
if environments.empty?
ui.fatal("Unable to find any environment files in '#{environments_path}'")
exit(1)
end
environments.each do |env|
load_environment(env)
end
end
updated = loader.load_from("environments", @name_args[0])
def load_environment(env)
updated = loader.load_from("environments", env)
updated.save
output(format_for_display(updated)) if config[:print_after]
ui.info("Updated Environment #{updated.name}")
end
def run
if config[:all] == true
load_all_environments
else
if @name_args[0].nil?
show_usage
ui.fatal("You must specify a file to load")
exit 1
end
@name_args.each do |arg|
load_environment(arg)
end
end
end
end
end
end
......@@ -43,6 +43,29 @@ describe Chef::Knife::EnvironmentFromFile do
@knife.run
end
context "when handling multiple environments" do
before(:each) do
@env_apple = @environment.dup
@env_apple.name("apple")
@knife.loader.stub!(:load_from).with("apple.rb").and_return @env_apple
end
it "loads multiple environments if given" do
@knife.name_args = [ "spec.rb", "apple.rb" ]
@environment.should_receive(:save).twice
@knife.run
end
it "loads all environments with -a" do
File.stub!(:expand_path).with("./environments/*.{json,rb}").and_return("/tmp/environments")
Dir.stub!(:glob).with("/tmp/environments").and_return(["spec.rb", "apple.rb"])
@knife.name_args = []
@knife.stub!(:config).and_return({:all => true})
@environment.should_receive(:save).twice
@knife.run
end
end
it "should not print the environment" do
@knife.should_not_receive(:output)
@knife.run
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment