Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change recieving options in Itamae::Resource::Base at initialize time #144

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/itamae/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,41 @@ def find_recipe_in_gem(recipe)
end
end

def initialize(runner, path)
def initialize(runner, path, options = {})
@runner = runner
@path = path
@delayed_notifications = []
@children = RecipeChildren.new
@options = options
end

def dir
::File.dirname(@path)
end

def load(vars = {})
context = EvalContext.new(self, vars)
context = EvalContext.new(self, vars, @options)
context.instance_eval(File.read(path), path, 1)
end

def run(options = {})
show_banner

Logger.formatter.with_indent do
@children.run(options)
run_delayed_notifications(options)
@children.run(@options)
run_delayed_notifications
end
end

private

def run_delayed_notifications(options)
def run_delayed_notifications
@delayed_notifications.uniq! do |notification|
[notification.action, notification.action_resource]
end

while notification = @delayed_notifications.shift
notification.run(options)
notification.run(@options)
end
end

Expand All @@ -67,8 +68,9 @@ def show_banner
end

class EvalContext
def initialize(recipe, vars)
def initialize(recipe, vars, options = {})
@recipe = recipe
@options = options

vars.each do |k, v|
define_singleton_method(k) { v }
Expand All @@ -92,7 +94,7 @@ def method_missing(*args, &block)
super
end

resource = klass.new(@recipe, name, &block)
resource = klass.new(@recipe, name, @options, &block)
@recipe.children << resource
end

Expand All @@ -116,7 +118,7 @@ def include_recipe(target)
return
end

recipe = Recipe.new(runner, path)
recipe = Recipe.new(runner, path, @options)
@recipe.children << recipe
recipe.load
end
Expand All @@ -138,7 +140,7 @@ class RecipeFromDefinition < Recipe
attr_accessor :definition

def load(vars = {})
context = EvalContext.new(self, vars)
context = EvalContext.new(self, vars, @options)
context.instance_eval(&@definition.class.definition_block)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/itamae/recipe_children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def run(options)
self.each do |resource|
case resource
when Resource::Base
resource.run(nil, dry_run: options[:dry_run])
resource.run
when Recipe
resource.run(options)
end
Expand Down
20 changes: 10 additions & 10 deletions lib/itamae/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ def define_attribute(name, options)
attr_reader :notifications
attr_reader :updated

def initialize(recipe, resource_name, &block)
def initialize(recipe, resource_name, options = {}, &block)
clear_current_attributes
@recipe = recipe
@resource_name = resource_name
@options = options
@updated = false

EvalContext.new(self).tap do |context|
Expand Down Expand Up @@ -132,11 +133,11 @@ def run(specific_action = nil, options = {})
end

[specific_action || attributes.action].flatten.each do |action|
run_action(action, options)
run_action(action)
end

verify unless options[:dry_run]
notify(options) if updated?
verify unless @options[:dry_run]
notify if updated?
end

@updated = false
Expand Down Expand Up @@ -165,7 +166,7 @@ def resource_type

alias_method :current, :current_attributes

def run_action(action, options)
def run_action(action, options = {})
@current_action = action

clear_current_attributes
Expand All @@ -185,12 +186,12 @@ def run_action(action, options)
show_differences

method_name = "action_#{action}"
if options[:dry_run]
if @options[:dry_run]
unless respond_to?(method_name)
Logger.error "action #{action.inspect} is unavailable"
end
else
public_send(method_name, options)
public_send(method_name, @options)
end

updated! if different?
Expand Down Expand Up @@ -323,7 +324,7 @@ def updated?
@updated
end

def notify(options)
def notify(options = {})
(notifications + recipe.children.subscribing(self)).each do |notification|
message = "Notifying #{notification.action} to #{notification.action_resource.resource_type} resource '#{notification.action_resource.resource_name}'"

Expand All @@ -342,7 +343,7 @@ def notify(options)
if notification.delayed?
@recipe.delayed_notifications << notification
elsif notification.immediately?
notification.run(options)
notification.run(@options)
end
end
end
Expand All @@ -360,4 +361,3 @@ def verify
end
end
end

9 changes: 5 additions & 4 deletions lib/itamae/resource/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def pre_action
when :edit
attributes.exist = true

content = backend.receive_file(attributes.path)
attributes.block.call(content)
attributes.content = content
unless @options[:dry_run]
content = backend.receive_file(attributes.path)
attributes.block.call(content)
attributes.content = content
end
end

send_tempfile
Expand Down Expand Up @@ -145,4 +147,3 @@ def send_tempfile
end
end
end

3 changes: 1 addition & 2 deletions lib/itamae/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def initialize(backend, options)

def load_recipes(paths)
paths.each do |path|
recipe = Recipe.new(self, File.expand_path(path))
recipe = Recipe.new(self, File.expand_path(path), @options)
children << recipe
recipe.load
end
Expand Down Expand Up @@ -85,4 +85,3 @@ def create_node
end
end
end

4 changes: 3 additions & 1 deletion spec/unit/lib/itamae/resource/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ class TestResource < Itamae::Resource::Base
end

context 'with dry_run' do
subject(:dry_run_resource) { described_class.new(recipe, "name", dry_run: true) }

context 'when specified action is unavailable' do
it 'logs error' do
expect(Itamae::Logger).to receive(:error).with(/action :name is unavailable/)
subject.run(nil, dry_run: true)
subject.run
end
end
end
Expand Down