Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Fix #204 - Define a no_commands helper #emit_events
Browse files Browse the repository at this point in the history
  • Loading branch information
erran committed Sep 30, 2016
1 parent 4e35604 commit eea722f
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions bin/convection
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,20 @@ module Convection
desc 'converge STACK', 'Converge your cloud'
option :stack_group, :type => :string, :desc => 'The name of a stack group defined in your cloudfile to converge'
option :stacks, :type => :array, :desc => 'A ordered space separated list of stacks to converge'
option :verbose, :type => :boolean, :aliases => '--v', :desc => 'Show stack progress', default: true
option :'very-verbose', :type => :boolean, :aliases => '--vv', :desc => 'Show unchanged stacks', default: true
def converge(stack = nil)
@cloud.configure(File.absolute_path(options['cloudfile'], @cwd))
@cloud.converge(stack, stack_group: options[:stack_group], stacks: options[:stacks]) do |event, errors|
say_status(*event.to_thor)
errors.each do |error|
say "* #{ error.message }"
error.backtrace.each { |b| say " #{ b }" }
end unless errors.nil?
end
@cloud.converge(stack, stack_group: options[:stack_group], stacks: options[:stacks], &emit_events)
end

desc 'delete STACK', 'Delete stack(s) from your cloud'
option :stack_group, :type => :string, :desc => 'The name of a stack group defined in your cloudfile to delete'
option :stacks, :type => :array, :desc => 'A ordered space separated list of stacks to delete'
def delete(stack = nil)
@cloud.configure(File.absolute_path(options['cloudfile'], @cwd))
print_status = lambda do |event, *errors|
say_status(*event.to_thor)
errors.flatten.each do |error|
say "* #{ error.message }"
error.backtrace.each { |b| say " #{ b }" }
end unless errors.nil?
end

stacks = @cloud.stacks_until(stack, options, &print_status)
stacks = @cloud.stacks_until(stack, options, &emit_events)
if stacks.empty?
say_status(:delete_failed, 'No stacks found matching the provided input (STACK, --stack-group, and/or --stacks).', :red)
return
Expand All @@ -50,7 +39,7 @@ module Convection

confirmation = ask('Are you sure you want to delete the above stack(s)?', limited_to: %w(yes no))
if confirmation.eql?('yes')
@cloud.delete(stacks, &print_status)
@cloud.delete(stacks, &emit_events)
else
say_status(:delete_aborted, 'Aborted deletion of the above stack(s).', :green)
end
Expand All @@ -63,26 +52,7 @@ module Convection
option :stacks, :type => :array, :desc => 'A ordered space separated list of stacks to diff'
def diff(stack = nil)
@cloud.configure(File.absolute_path(options['cloudfile'], @cwd))
last_event = nil

@cloud.diff(stack, stack_group: options[:stack_group], stacks: options[:stacks]) do |d|
if d.is_a? Model::Event
if options[:'very-verbose'] || d.name == :error
say_status(*d.to_thor)
elsif options[:verbose]
say_status(*d.to_thor) if d.name == :compare
end
last_event = d
elsif d.is_a? Model::Diff
if !options[:'very-verbose'] && !options[:verbose]
say_status(*last_event.to_thor) unless last_event.nil?
last_event = nil
end
say_status(*d.to_thor)
else
say_status(*d.to_thor)
end
end
@cloud.diff(stack, stack_group: options[:stack_group], stacks: options[:stacks], &emit_events)
end

desc 'print STACK', 'Print the rendered template for STACK'
Expand All @@ -96,6 +66,34 @@ module Convection
@cloud.configure(File.absolute_path(options['cloudfile'], @cwd))
@cloud.stacks[stack].validate
end

no_commands do
attr_accessor :last_event

def emit_events(event, *errors)
if event.is_a? Model::Event
if options[:'very-verbose'] || event.name == :error
say_status(*event.to_thor)
elsif options[:verbose]
say_status(*event.to_thor) if event.name == :compare
end
@last_event = event
elsif event.is_a? Model::Diff
if !options[:'very-verbose'] && !options[:verbose]
say_status(*last_event.to_thor) unless last_event.nil?
@last_event = nil
end
say_status(*event.to_thor)
else
say_status(*event.to_thor)
end

errors.each do |error|
say "* #{ error.message }"
error.backtrace.each { |trace| say " #{ trace }" }
end
end
end
end
end

Expand Down

0 comments on commit eea722f

Please # to comment.