Skip to content

Commit

Permalink
Fixes #141: Make a more detailed environment list. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
askreet committed Oct 18, 2016
1 parent be8c48d commit f20ca56
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/moonshot/commands/list.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require_relative '../stack_lister'
require_relative '../stack_list_printer'

module Moonshot
module Commands
class List < Moonshot::Command
self.usage = 'list [options]'
self.description = 'List stacks for this application'

def execute
controller.list
stacks = StackLister.new(controller.config.app_name).list
StackListPrinter.new(stacks).print
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/moonshot/stack_list_printer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Moonshot
class StackListPrinter
attr_accessor :stacks

def initialize(stacks)
@stacks = stacks
@table = UnicodeTable.new('Environment List')
end

def print
rows = @stacks.map do |stack|
[stack.name, stack.creation_time, stack.status]
end

@table.add_table(rows)

@table.draw
@table.draw_children
end
end
end
19 changes: 15 additions & 4 deletions lib/moonshot/stack_lister.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
module Moonshot
# The StackLister is world renoun for it's ability to list stacks.
class StackLister
EnvironmentDescription = Struct.new(:name, :creation_time, :status)
include CredsHelper

def initialize(app_name)
@app_name = app_name
end

def list
all_stacks = cf_client.describe_stacks.stacks
app_stacks = all_stacks.reject { |s| s.stack_name !~ /^#{@app_name}/ }
result = []
next_token = nil
loop do
resp = cf_client.describe_stacks(next_token: next_token)
resp.stacks.each do |stack|
app_tag = stack.tags.find { |t| t.key == 'moonshot_application' }
env_tag = stack.tags.find { |t| t.key == 'moonshot_environment' }

app_stacks.sort_by(&:stack_name).each do |stack|
puts stack.stack_name
next unless app_tag && app_tag.value == Moonshot.config.app_name
result <<
EnvironmentDescription.new(env_tag.value, stack.creation_time, stack.stack_status)
end
break unless resp.next_token
next_token = resp.next_token
end
result.sort_by(&:name)
end
end
end

0 comments on commit f20ca56

Please sign in to comment.