Class: S3ListBuckets::Runner
- Inherits:
-
Object
- Object
- S3ListBuckets::Runner
- Defined in:
- lib/s3-list-buckets/runner.rb
Instance Method Summary collapse
- #display_json(buckets) ⇒ Object
- #display_location_text(buckets) ⇒ Object
-
#initialize(config) ⇒ Runner
constructor
A new instance of Runner.
- #name_matches_patterns?(bucket_name) ⇒ Boolean
- #promise ⇒ Object
- #promise_bucket(bucket) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(config) ⇒ Runner
Returns a new instance of Runner.
9 10 11 12 13 14 15 16 |
# File 'lib/s3-list-buckets/runner.rb', line 9 def initialize(config) @config = config @config.s3_client ||= begin = S3ListBuckets::Client..merge(config.) Aws::S3::Client.new() end end |
Instance Method Details
#display_json(buckets) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/s3-list-buckets/runner.rb', line 54 def display_json(buckets) buckets.each do |bucket| bucket[:creation_date] = bucket[:creation_date].utc.strftime '%Y-%m-%dT%H:%M:%SZ' puts JSON.generate(bucket) end end |
#display_location_text(buckets) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/s3-list-buckets/runner.rb', line 61 def display_location_text(buckets) unless buckets.empty? width = buckets.map {|d| (d[:location_constraint] || "-").length }.max format = "%-#{width}.#{width}s %s" buckets.each do |d| puts format % [ d[:location_constraint] || "-", d[:name] ] end end end |
#name_matches_patterns?(bucket_name) ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/s3-list-buckets/runner.rb', line 46 def name_matches_patterns?(bucket_name) return true if @config.patterns.nil? @config.patterns.any? do |pattern| bucket_name.match("^" + pattern) end end |
#promise ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/s3-list-buckets/runner.rb', line 18 def promise Rosarium::Promise.execute do @config.s3_client.list_buckets end.then do |response| # list_buckets doesn't paginate so this is relatively simple Rosarium::Promise.all( response.buckets.map {|bucket| promise_bucket bucket}.reject &:nil? ) end end |
#promise_bucket(bucket) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/s3-list-buckets/runner.rb', line 29 def promise_bucket(bucket) return nil unless name_matches_patterns?(bucket.name) Rosarium::Promise.execute do r = { name: bucket.name, creation_date: bucket.creation_date, } if @config.show_location r[:location_constraint] = @config.s3_client.get_bucket_location(bucket: bucket.name).location_constraint end r end end |
#run ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/s3-list-buckets/runner.rb', line 71 def run promise.then do |buckets| if @config.json_format display_json(buckets) elsif @config.show_location display_location_text(buckets) else buckets.each {|bucket| puts bucket[:name]} end end.value! 0 end |