lsaws 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e90dd8d823481d65134a5faf02b99152e6d47e01047feef20388ff2f0634e24d
4
+ data.tar.gz: 8d4cdeb7a41e68f2ad88ca4b8a9746d8a712f05c790d8734debf3d621e76f426
5
+ SHA512:
6
+ metadata.gz: 1811a4fb62be35633527c1a440b45a820addb64d6a2932b445c7254832d1780ce36eea7df5f12bc04a872f2ed0be40f581eb819ab443cf8f1e74af8854f32aac
7
+ data.tar.gz: 357bb0e3e7fb584fc22625936a2ca89b358afb08d754df1a95714480baf12aecf2e1146d33961a20b01109d051478af1a53d03d25602780f688aa1d5b98d8d2c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at andrey.zaikin@every-pay.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in lsaws.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem "rake", "~> 13.0"
10
+ gem "rspec", "~> 3.0"
11
+ gem "rubocop", "~> 1.21"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lsaws (0.1.0)
5
+ aws-sdk-core (~> 3.131)
6
+ tabulo (~> 2.8)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ aws-eventstream (1.2.0)
13
+ aws-partitions (1.705.0)
14
+ aws-sdk-core (3.170.0)
15
+ aws-eventstream (~> 1, >= 1.0.2)
16
+ aws-partitions (~> 1, >= 1.651.0)
17
+ aws-sigv4 (~> 1.5)
18
+ jmespath (~> 1, >= 1.6.1)
19
+ aws-sigv4 (1.5.2)
20
+ aws-eventstream (~> 1, >= 1.0.2)
21
+ diff-lcs (1.5.0)
22
+ jmespath (1.6.2)
23
+ json (2.6.3)
24
+ parallel (1.22.1)
25
+ parser (3.2.0.0)
26
+ ast (~> 2.4.1)
27
+ rainbow (3.1.1)
28
+ rake (13.0.6)
29
+ regexp_parser (2.6.2)
30
+ rexml (3.2.5)
31
+ rspec (3.12.0)
32
+ rspec-core (~> 3.12.0)
33
+ rspec-expectations (~> 3.12.0)
34
+ rspec-mocks (~> 3.12.0)
35
+ rspec-core (3.12.1)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-expectations (3.12.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-mocks (3.12.3)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.12.0)
43
+ rspec-support (3.12.0)
44
+ rubocop (1.44.1)
45
+ json (~> 2.3)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.2.0.0)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml (>= 3.2.5, < 4.0)
51
+ rubocop-ast (>= 1.24.1, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 2.4.0, < 3.0)
54
+ rubocop-ast (1.24.1)
55
+ parser (>= 3.1.1.0)
56
+ ruby-progressbar (1.11.0)
57
+ tabulo (2.8.2)
58
+ tty-screen (= 0.8.1)
59
+ unicode-display_width (~> 2.2)
60
+ tty-screen (0.8.1)
61
+ unicode-display_width (2.4.2)
62
+
63
+ PLATFORMS
64
+ arm64-darwin-21
65
+
66
+ DEPENDENCIES
67
+ lsaws!
68
+ rake (~> 13.0)
69
+ rspec (~> 3.0)
70
+ rubocop (~> 1.21)
71
+
72
+ BUNDLED WITH
73
+ 2.3.11
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Andrey "Zed" Zaikin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Lsaws
2
+
3
+ User-friendly AWS resources listing tool
4
+
5
+ ## Usage
6
+
7
+ # lsaws
8
+
9
+ Usage: lsaws [options] <sdk> [entity_type]
10
+ -o, --output FMT Format: js/json/json-stream/table/text/yaml
11
+ --no-header Suppress header
12
+ -x Shortcut for "-o text --no-header"
13
+ --tags Show tags
14
+ -f, --filter K=V Add filter
15
+ -C, --columns C Show only specified column(s)
16
+ --max-results N Fetch only specified number of results
17
+ --max-width X max text width for table/text mode, default: auto
18
+
19
+ -v, --verbose Verbose output
20
+ --debug
21
+
22
+ -L, --list List SDKs or entity types
23
+ -A, --all List all entity types within SDK
24
+
25
+
26
+ ## License
27
+
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
29
+
30
+ ## Code of Conduct
31
+
32
+ Everyone interacting in the Lsaws project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zed-0xff/lsaws/blob/master/CODE_OF_CONDUCT.md).
data/README.md.tpl ADDED
@@ -0,0 +1,15 @@
1
+ # Lsaws
2
+
3
+ User-friendly AWS resources listing tool
4
+
5
+ ## Usage
6
+
7
+ % lsaws
8
+
9
+ ## License
10
+
11
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
12
+
13
+ ## Code of Conduct
14
+
15
+ Everyone interacting in the Lsaws project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zed-0xff/lsaws/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ desc "build readme"
15
+ task :readme do
16
+ require "erb"
17
+ tpl = File.read("README.md.tpl").gsub(/^%\s+(.+)/) do |x|
18
+ x.sub!(/^%/, "")
19
+ "<%= run(\"#{x}\") %>"
20
+ end
21
+ def run(cmd)
22
+ cmd.strip!
23
+ puts "[.] #{cmd} ..."
24
+ r = " # #{cmd}\n\n"
25
+ cmd.sub!(/^lsaws/, "./exe/lsaws")
26
+ lines = `#{cmd}`.sub(/\A\n+/m, "").sub(/\s+\Z/, "").split("\n")
27
+ lines = lines[0, 25] + ["..."] if lines.size > 50
28
+ r << lines.map { |x| " #{x}" }.join("\n")
29
+ r << "\n"
30
+ end
31
+ result = ERB.new(tpl).result
32
+ File.open("README.md", "w") { |f| f << result }
33
+ end
data/exe/lsaws ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "lsaws"
5
+
6
+ Lsaws::CLI.new(ARGV).run!
data/lib/lsaws/cli.rb ADDED
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module Lsaws
6
+ class CLI
7
+ SUPPORTED_FORMATS = %w[table yaml json json-stream js text].sort
8
+ DEFAULT_FORMAT = :table
9
+
10
+ def initialize(argv)
11
+ @options = {
12
+ format: DEFAULT_FORMAT,
13
+ header: true,
14
+ filters: {},
15
+ show_cols: [],
16
+ max_results: nil
17
+ }
18
+ @commands = option_parser.parse!(argv)
19
+ if @options[:all]
20
+ raise Error, "Unsupported arguments combination" if @commands.size != 1
21
+
22
+ @commands << :all
23
+ end
24
+ @commands << :list if @options[:list]
25
+ end
26
+
27
+ def option_parser
28
+ @option_parser ||=
29
+ OptionParser.new do |opt|
30
+ opt.banner = "Usage: lsaws [options] <sdk> [entity_type]"
31
+
32
+ opt.on("-o", "--output FMT", SUPPORTED_FORMATS, "Format: #{SUPPORTED_FORMATS.join("/")}") do |f|
33
+ @options[:format] = f.to_sym
34
+ end
35
+ opt.on("--no-header", "Suppress header") { @options[:header] = false }
36
+ opt.on("-x", 'Shortcut for "-o text --no-header"') do
37
+ @options[:format] = :text
38
+ @options[:header] = false
39
+ end
40
+ opt.on("--tags", "Show tags") { @options[:show_tags] = true }
41
+ opt.on("-f", "--filter K=V", "Add filter") { |o| @options[:filters].merge!(Hash[*o.split("=", 2)]) }
42
+ opt.on("-C", "--columns C", "Show only specified column(s)") do |o|
43
+ if o[","]
44
+ @options[:show_cols].append(*o.split(","))
45
+ else
46
+ @options[:show_cols] << o
47
+ end
48
+ end
49
+ opt.on("--max-results N", Integer, "Fetch only specified number of results") do |o|
50
+ @options[:max_results] = o
51
+ end
52
+ opt.on("--max-width X", Integer, "max text width for table/text mode, default: auto") do |o|
53
+ @options[:max_width] = o
54
+ end
55
+
56
+ opt.separator ""
57
+ opt.on("-v", "--verbose", "Verbose output") { @options[:verbose] = true }
58
+ opt.on("--debug") { @options[:debug] = true }
59
+
60
+ opt.separator ""
61
+ opt.on("-L", "--list", "List SDKs or entity types") { @options[:list] = true }
62
+ opt.on("-A", "--all", "List all entity types within SDK") { @options[:all] = true }
63
+ end
64
+ end
65
+
66
+ def run!
67
+ case @commands.size
68
+ when 1, 2
69
+ Lister.new(@options).process_command(*@commands)
70
+ else
71
+ puts option_parser.help
72
+ nil
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,286 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws-sdk-core"
4
+ require "json"
5
+ require "tabulo"
6
+ require "yaml"
7
+
8
+ module Lsaws
9
+ class Lister
10
+ CONFIG = YAML.load_file(File.join(Lsaws.root, "lsaws.yml"))
11
+
12
+ def initialize(options)
13
+ @options = options
14
+ end
15
+
16
+ def _prepare_entities(sdk, type)
17
+ edef = CONFIG.dig(sdk, type)
18
+ if edef.is_a?(String)
19
+ type = edef
20
+ edef = CONFIG[sdk][type] # should exist
21
+ raise Error, "unknown entity type: #{type}" unless edef
22
+ end
23
+ edef ||= {}
24
+ require(edef["require"] || "aws-sdk-#{sdk}")
25
+
26
+ params = edef["default_params"] || {}
27
+ if @options[:filters].any?
28
+ params[:filters] = @options[:filters].map { |k, v| { name: k, values: [v] } }
29
+ elsif edef["default_filter"]
30
+ params[:filters] = [edef["default_filter"]]
31
+ end
32
+ params[:max_results] = @options[:max_results] if @options[:max_results]
33
+
34
+ client_class = edef["client_class"] || guess_client_class(sdk)
35
+ client = Kernel.const_get(client_class).new
36
+ method_name = edef["method"] || (client.respond_to?("describe_#{type}") ? "describe_#{type}" : "list_#{type}")
37
+ unless client.respond_to?(method_name)
38
+ if type == "default"
39
+ warn "[!] no default entity type set for #{sdk.inspect} SDK"
40
+ else
41
+ warn "[!] #{sdk.inspect} SDK does not have #{type.inspect} entity type"
42
+ end
43
+ puts "Known entity types are:"
44
+ list_entity_types(sdk)
45
+ exit 1
46
+ end
47
+ results = client.send(method_name, params)
48
+
49
+ pp results if @options[:debug]
50
+
51
+ if !edef["result_keys"] && results.any?
52
+ r = results.first
53
+ r = r.last if r.is_a?(Array)
54
+ edef["result_keys"] = if r.respond_to?(type)
55
+ [type]
56
+ else
57
+ [(r.members - [:next_token]).first]
58
+ end
59
+ end
60
+ edef["result_keys"].each do |key|
61
+ results = if results.is_a?(Array)
62
+ results.map(&key.to_sym).flatten
63
+ else
64
+ results.send(key)
65
+ end
66
+ end
67
+ edef["cols"] = @options[:show_cols] if @options[:show_cols].any?
68
+
69
+ col_defs = {}
70
+ Array(edef["cols"]).each do |r|
71
+ case r
72
+ when String, Symbol
73
+ col_defs[r] = proc { |entity| entity.send(r) }
74
+ when Hash
75
+ col_defs.merge!(r)
76
+ else
77
+ raise Error, "unexpected #{r.inspect}"
78
+ end
79
+ end
80
+ col_defs["tags"] = _convert_tags_proc if @options[:show_tags] || col_defs["tags"]
81
+
82
+ results ||= []
83
+ if results.any? && !results.first.respond_to?(:name)
84
+ results.first.class.class_eval do
85
+ def name
86
+ tags.find { |tag| tag.key == "Name" }&.value
87
+ end
88
+ end
89
+ end
90
+
91
+ # ec2 instance_event_notification_attributes
92
+ # 'Array(results)' doesn't work here
93
+ results = [results] unless results.is_a?(Array)
94
+
95
+ if block_given?
96
+ results.map do |entity|
97
+ yield entity, col_defs
98
+ end
99
+ else
100
+ [results, col_defs]
101
+ end
102
+ end
103
+
104
+ def entities2records(sdk, type)
105
+ rows, cols = _prepare_entities(sdk, type)
106
+ if rows.is_a?(Array) && rows[0].is_a?(String)
107
+ # sqs
108
+ cols = { value: proc { |entity| entity } }
109
+ elsif rows.respond_to?(:members)
110
+ rows = [rows]
111
+ end
112
+ [rows, cols]
113
+ end
114
+
115
+ def entities2hashes(sdk, type)
116
+ rows =
117
+ if @options[:show_cols].any?
118
+ # show cols specified on cmdline
119
+ _prepare_entities(sdk, type) do |entity, _col_defs|
120
+ case entity
121
+ when String
122
+ { value: entity } # might conflict with show_cols, let's always display for now
123
+ else
124
+ @options[:show_cols].map { |c| [c, entity.send(c)] }.to_h
125
+ end
126
+ end
127
+ else
128
+ # either predefined cols from YAML or automatic ones
129
+ _prepare_entities(sdk, type) do |entity, _col_defs|
130
+ case entity
131
+ when String
132
+ { value: entity }
133
+ else
134
+ # ec2 instance_event_notification_attributes
135
+ entity.to_h
136
+ end
137
+ end
138
+ end
139
+ if rows&.first&.key?("tags")
140
+ rows.each do |row|
141
+ row["tags"] = row["tags"].map { |tag| [tag.key, tag.value] }.to_h
142
+ end
143
+ end
144
+ rows
145
+ end
146
+
147
+ # only for table/text view
148
+ def _convert_tags_proc
149
+ proc { |entity| entity.tags.map { |tag| "#{tag.key}=#{tag.value}" }.join(", ") }
150
+ end
151
+
152
+ def get_sdks
153
+ r = []
154
+ Gem.path.each do |p|
155
+ next unless Dir.exist?(p)
156
+
157
+ r.append(*Dir[File.join(p, "gems/aws-sdk-*")].map do |gem_dir|
158
+ a = File.basename(gem_dir).split("-")
159
+ a.size == 4 ? a[2] : nil
160
+ end)
161
+ end
162
+ r.compact.uniq.sort - ["core"]
163
+ end
164
+
165
+ def list_sdks
166
+ _list_array get_sdks
167
+ end
168
+
169
+ def _list_array(a)
170
+ case @options[:format]
171
+ when :json
172
+ puts a.to_json
173
+ when :yaml
174
+ puts a.to_yaml
175
+ else
176
+ puts a.join("\n")
177
+ end
178
+ end
179
+
180
+ def guess_client_class(sdk)
181
+ c = Aws.constants.find { |x| x.to_s.downcase == sdk }
182
+ "Aws::#{c}::Client"
183
+ end
184
+
185
+ def _get_method_rdoc(data, method)
186
+ pos = data =~ /^\s+def\s+#{method}\s*\(/
187
+ return nil unless pos
188
+
189
+ chunk = ""
190
+ bs = 4096
191
+ until chunk["\n\n"]
192
+ chunk = data[pos - bs..pos]
193
+ bs *= 2
194
+ end
195
+ chunk[chunk.rindex("\n\n") + 2..]
196
+ end
197
+
198
+ def get_entity_types(sdk)
199
+ require "aws-sdk-#{sdk}"
200
+ client_class = Kernel.const_get(guess_client_class(sdk))
201
+ methods = client_class
202
+ .instance_methods
203
+ .find_all { |m| m =~ /^(describe|list)_.+s$/ && m !~ /(status|access)$/ }
204
+
205
+ return [] if methods.empty?
206
+
207
+ data = File.read(client_class.instance_method(methods[0]).source_location[0])
208
+ methods.delete_if do |m|
209
+ rdoc = _get_method_rdoc(data, m)
210
+ next unless rdoc
211
+
212
+ required_params = rdoc.scan(/^\s+# @option params \[required, (.+?)\] :(\w+)/)
213
+ required_params.any?
214
+ end
215
+
216
+ methods.map { |m| m.to_s.sub(/^(describe|list)_/, "") }.sort
217
+ end
218
+
219
+ def list_entity_types(sdk)
220
+ _list_array get_entity_types sdk
221
+ end
222
+
223
+ def _tabulo_guess_max_cols(rows, _cols)
224
+ max_cols = rows[0].members.size
225
+ return max_cols if max_cols < 4 || !@options[:max_width]
226
+
227
+ 4.upto(max_cols) do |ncols|
228
+ tbl = Tabulo::Table.new(rows[0, 100], *rows[0].members[0, ncols])
229
+ tbl.autosize_columns
230
+ tbl_width = tbl.column_registry.values.map { |c| c.padded_width + 1 }.inject(:+) + 1
231
+ return ncols - 1 if tbl_width >= @options[:max_width]
232
+ end
233
+ max_cols
234
+ end
235
+
236
+ def process_command(sdk, type = "default")
237
+ if sdk == :list
238
+ return list_sdks
239
+ elsif type == :list
240
+ return list_entity_types(sdk)
241
+ elsif type == :all
242
+ get_entity_types(sdk).each do |etype|
243
+ puts "#{etype}:"
244
+ process_command(sdk, etype)
245
+ end
246
+ return
247
+ end
248
+
249
+ case @options[:format]
250
+ when :text, :table
251
+ @options[:max_width] ||= ($stdout.tty? ? TTY::Screen.width : nil)
252
+
253
+ rows, cols = entities2records(sdk, type)
254
+ return unless rows.any?
255
+
256
+ style = @options[:format] == :text ? :blank : :modern
257
+
258
+ tbl = Tabulo::Table.new(rows, border: style, align_header: :left, header_frequency: @options[:header])
259
+ if cols.any?
260
+ cols.each { |name, func| tbl.add_column(name, &func) }
261
+ else
262
+ max_cols = _tabulo_guess_max_cols(rows, cols)
263
+ rows[0].members[0, max_cols].each { |col| tbl.add_column(col) }
264
+ end
265
+ puts tbl.pack(max_table_width: @options[:max_width])
266
+ when :json
267
+ rows = entities2hashes(sdk, type)
268
+ puts rows.to_json
269
+ when :'json-stream', :js
270
+ rows = entities2hashes(sdk, type)
271
+ rows.each do |row|
272
+ puts row.to_json
273
+ end
274
+ when :yaml
275
+ rows = entities2hashes(sdk, type)
276
+ rows.each { |row| Utils._deep_transform_keys_in_object!(row, &:to_s) }
277
+ puts rows.to_yaml
278
+ else
279
+ warn "[!] unknown format: #{@options[:format]}"
280
+ exit 1
281
+ end
282
+ rescue Aws::Errors::ServiceError => e
283
+ warn "[!] #{e}"
284
+ end
285
+ end
286
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lsaws
4
+ module Utils
5
+ # copypasted from lib/active_support/core_ext/hash/keys.rb
6
+ def self._deep_transform_keys_in_object!(object, &block)
7
+ case object
8
+ when Hash
9
+ object.each_key do |key|
10
+ value = object.delete(key)
11
+ object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
12
+ end
13
+ object
14
+ when Array
15
+ object.map! { |e| _deep_transform_keys_in_object!(e, &block) }
16
+ else
17
+ object
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lsaws
4
+ VERSION = "0.1.0"
5
+ end
data/lib/lsaws.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lsaws
4
+ class Error < StandardError; end
5
+
6
+ def self.root
7
+ File.dirname(File.dirname(File.expand_path(__FILE__)))
8
+ end
9
+ end
10
+
11
+ require_relative "lsaws/version"
12
+ require_relative "lsaws/cli"
13
+ require_relative "lsaws/lister"
data/lsaws.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/lsaws/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lsaws"
7
+ spec.version = Lsaws::VERSION
8
+ spec.authors = ["Andrey \"Zed\" Zaikin"]
9
+ spec.email = ["zed.0xff@gmail.com"]
10
+
11
+ spec.summary = "User-friendly AWS resources listing tool"
12
+ spec.homepage = "https://github.com/zed-0xff/lsaws"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ spec.add_dependency "aws-sdk-core", "~> 3.131"
32
+ spec.add_dependency "tabulo", "~> 2.8"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
data/lsaws.yml ADDED
@@ -0,0 +1,41 @@
1
+ ec2:
2
+ default: instances
3
+ images:
4
+ default_params:
5
+ owners: [ self ]
6
+ cols:
7
+ - image_id
8
+ - name
9
+ - creation_date
10
+ fpga_images:
11
+ default_params:
12
+ owners: [ self ]
13
+ instances:
14
+ default_filter:
15
+ name: instance-state-name
16
+ values:
17
+ - pending
18
+ - running
19
+ - shutting-down
20
+ - stopping
21
+ result_keys:
22
+ - reservations
23
+ - instances
24
+ cols:
25
+ - instance_id
26
+ - name
27
+ - vpc_id
28
+ - private_ip_address
29
+ - launch_time
30
+ addresses:
31
+ cols:
32
+ - public_ip
33
+ - private_ip_address
34
+ - instance_id
35
+ - name
36
+ kms:
37
+ aliases:
38
+ cols:
39
+ - alias_name
40
+ - target_key_id
41
+ - last_updated_date
data/sig/lsaws.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Lsaws
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lsaws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey "Zed" Zaikin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.131'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.131'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tabulo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.8'
41
+ description:
42
+ email:
43
+ - zed.0xff@gmail.com
44
+ executables:
45
+ - lsaws
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE
55
+ - README.md
56
+ - README.md.tpl
57
+ - Rakefile
58
+ - exe/lsaws
59
+ - lib/lsaws.rb
60
+ - lib/lsaws/cli.rb
61
+ - lib/lsaws/lister.rb
62
+ - lib/lsaws/utils.rb
63
+ - lib/lsaws/version.rb
64
+ - lsaws.gemspec
65
+ - lsaws.yml
66
+ - sig/lsaws.rbs
67
+ homepage: https://github.com/zed-0xff/lsaws
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/zed-0xff/lsaws
72
+ source_code_uri: https://github.com/zed-0xff/lsaws
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.6.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.3.7
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: User-friendly AWS resources listing tool
92
+ test_files: []