s3-list-buckets 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1b62a416e731b81b585b94ea0b6d438ade56fc7
4
+ data.tar.gz: 226b73bb87bdde5c157743c5a9a87799ae80c388
5
+ SHA512:
6
+ metadata.gz: 846915c1e8679430b42d5582802a4269e79022e29d0c03c9ac3849b56c3b9b19d4744250682b6f223561631f56d32ad31977bc2c2cbd0e67084cc18d073cb2e8
7
+ data.tar.gz: cabceda215be9a0a96a0c06dce766602631432ae03351265d1d969c808a434c2326d72bb026154cd102a7a3667d81dd9a4e6e4cd8a1be14a54a2de9f92693956
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require 's3-list-buckets'
6
+
7
+ config = S3ListBuckets::Config.new
8
+
9
+ opts_parser = OptionParser.new do |opts|
10
+ opts.banner = "Usage: s3-list-buckets [OPTIONS] [PATTERN ...]"
11
+ opts.separator ""
12
+ opts.on("-l", "--location", "Show the location") do
13
+ config.show_location = true
14
+ end
15
+ opts.on("-j", "--json", "Show output in json format") do
16
+ config.json_format = true
17
+ end
18
+ opts.separator ""
19
+ opts.separator <<-EOF
20
+ If one or more PATTERNs are given, a bucket is only shown if the bucket name
21
+ matches at least one PATTERN (each of which is implicitly prefixed by "^", the
22
+ start-of-string pattern).
23
+
24
+ Examples:
25
+
26
+ # Show all your buckets (just bucket names):
27
+ s3-list-buckets
28
+
29
+ # Show buckets, prefixed by the location constraint
30
+ s3-list-buckets --location
31
+
32
+ # Show names and location constraints of buckets whose names start with "a"
33
+ # or "b", as json:
34
+ s3-list-buckets --location --json a b
35
+
36
+ EOF
37
+ end
38
+
39
+ opts_parser.parse!
40
+
41
+ unless ARGV.empty?
42
+ config.patterns = ARGV
43
+ end
44
+
45
+ config.validate
46
+
47
+ rc = S3ListBuckets::Runner.new(config).run
48
+ exit rc
49
+
50
+ # eof s3-list-buckets
@@ -0,0 +1,4 @@
1
+ require_relative './s3-list-buckets/client.rb'
2
+ require_relative './s3-list-buckets/config.rb'
3
+ require_relative './s3-list-buckets/runner.rb'
4
+ require_relative './s3-list-buckets/version.rb'
@@ -0,0 +1,25 @@
1
+ module S3ListBuckets
2
+
3
+ class Client
4
+
5
+ def self.configure
6
+ Aws.config.merge! core_v2_options
7
+ end
8
+
9
+ def self.core_v2_options
10
+ {
11
+ http_proxy: get_proxy,
12
+ user_agent_suffix: "s3-list-buckets #{VERSION}",
13
+ # http_wire_trace: true,
14
+ }
15
+ end
16
+
17
+ def self.get_proxy
18
+ e = ENV['https_proxy']
19
+ e = "https://#{e}" if e && !e.empty? && !e.start_with?('http')
20
+ return e
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,21 @@
1
+ module S3ListBuckets
2
+
3
+ class Config
4
+ # Stronger builder pattern would be nice
5
+ attr_accessor :client_options, :s3_client,
6
+ :patterns,
7
+ :show_location,
8
+ :json_format
9
+
10
+ def initialize
11
+ @client_options = {}
12
+ @patterns = nil
13
+ @show_location = false
14
+ @json_format = false
15
+ end
16
+
17
+ def validate
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,87 @@
1
+ require 'aws-sdk'
2
+ require 'json'
3
+ require 'rosarium'
4
+
5
+ module S3ListBuckets
6
+
7
+ class Runner
8
+
9
+ def initialize(config)
10
+ @config = config
11
+
12
+ @config.s3_client ||= begin
13
+ effective_options = S3ListBuckets::Client.core_v2_options.merge(config.client_options)
14
+ Aws::S3::Client.new(effective_options)
15
+ end
16
+ end
17
+
18
+ def promise
19
+ Rosarium::Promise.execute do
20
+ @config.s3_client.list_buckets
21
+ end.then do |response|
22
+ # list_buckets doesn't paginate so this is relatively simple
23
+ Rosarium::Promise.all(
24
+ response.buckets.map {|bucket| promise_bucket bucket}.reject &:nil?
25
+ )
26
+ end
27
+ end
28
+
29
+ def promise_bucket(bucket)
30
+ return nil unless name_matches_patterns?(bucket.name)
31
+
32
+ Rosarium::Promise.execute do
33
+ r = {
34
+ name: bucket.name,
35
+ creation_date: bucket.creation_date,
36
+ }
37
+
38
+ if @config.show_location
39
+ r[:location_constraint] = @config.s3_client.get_bucket_location(bucket: bucket.name).location_constraint
40
+ end
41
+
42
+ r
43
+ end
44
+ end
45
+
46
+ def name_matches_patterns?(bucket_name)
47
+ return true if @config.patterns.nil?
48
+
49
+ @config.patterns.any? do |pattern|
50
+ bucket_name.match("^" + pattern)
51
+ end
52
+ end
53
+
54
+ def display_json(buckets)
55
+ buckets.each do |bucket|
56
+ bucket[:creation_date] = bucket[:creation_date].utc.strftime '%Y-%m-%dT%H:%M:%SZ'
57
+ puts JSON.generate(bucket)
58
+ end
59
+ end
60
+
61
+ def display_location_text(buckets)
62
+ unless buckets.empty?
63
+ width = buckets.map {|d| (d[:location_constraint] || "-").length }.max
64
+ format = "%-#{width}.#{width}s %s"
65
+ buckets.each do |d|
66
+ puts format % [ d[:location_constraint] || "-", d[:name] ]
67
+ end
68
+ end
69
+ end
70
+
71
+ def run
72
+ promise.then do |buckets|
73
+ if @config.json_format
74
+ display_json(buckets)
75
+ elsif @config.show_location
76
+ display_location_text(buckets)
77
+ else
78
+ buckets.each {|bucket| puts bucket[:name]}
79
+ end
80
+ end.value!
81
+
82
+ 0
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,3 @@
1
+ module S3ListBuckets
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3-list-buckets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rachel Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rosarium
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ description: "\n s3-list-buckets shows the names and (optionally) location constraints
42
+ of\n your S3 buckets.\n\n Options are available to control display of location
43
+ constraint and json\n output.\n\n Respects $https_proxy.\n "
44
+ email: s3-list-buckets-git@rve.org.uk
45
+ executables:
46
+ - s3-list-buckets
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - bin/s3-list-buckets
51
+ - lib/s3-list-buckets.rb
52
+ - lib/s3-list-buckets/client.rb
53
+ - lib/s3-list-buckets/config.rb
54
+ - lib/s3-list-buckets/runner.rb
55
+ - lib/s3-list-buckets/version.rb
56
+ homepage: https://github.com/rvedotrc/s3-list-buckets
57
+ licenses:
58
+ - Apache-2.0
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.5.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: List S3 bucket names and their location constraints
80
+ test_files: []