Class: IParty::CLI::Application
- Inherits:
-
Object
- Object
- IParty::CLI::Application
show all
- Includes:
- Actions, Appinfo, Options, Colorize
- Defined in:
- lib/iparty/cli/application.rb,
lib/iparty/cli/application/actions.rb,
lib/iparty/cli/application/appinfo.rb,
lib/iparty/cli/application/options.rb,
lib/iparty/cli/application/irb_context.rb
Defined Under Namespace
Modules: Actions, Appinfo, Options
Classes: ActionNotFound, DefaultOut, Error, IrbContext, UnknownFormatter
Constant Summary
Constants included
from Colorize
Colorize::COLORMAP
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#_deep_onlyexcept_kv_match?(key, value, matchers, keep: true, keystack: []) ⇒ Boolean
-
#build_formatter(fmt = @opts[:formatter], **kw) ⇒ Object
-
#create_matchers(expressions) ⇒ Object
-
#deep_onlyexcept_data(data, matchers, keep: true, keystack: []) ⇒ Object
-
#dispatch(action: nil) ⇒ Object
-
#each_address(use_argf: read_from_stdin?, , &block) ⇒ Object
-
#each_line_in_argf_as_addresses(prompt: $stdin.tty?, ps1: "> ") ⇒ Object
-
#ensure_mmdb_files!(fetch_when = @opts[:mmdb_fetch_when]) ⇒ Object
-
#formatter ⇒ Object
-
#initialize(env:, argv:, argf:, **opts) {|_self| ... } ⇒ Application
constructor
A new instance of Application.
-
#ip_to_data(ip, colorize: false) ⇒ Object
-
#onlyexcept_data!(data) ⇒ Object
-
#read_from_stdin? ⇒ Boolean
-
#replace_names_with_singular_for!(lang, data) ⇒ Object
-
#stdin_select? ⇒ Boolean
-
#summarize(data, colorize: ) ⇒ Object
-
#summarize_asn_detail(data) ⇒ Object
-
#summarize_location_detail(data) ⇒ Object
-
#summarize_network_detail(data) ⇒ Object
Methods included from Colorize
#colorize, #decolorize, #with_color
Methods included from Appinfo
#appinfo_cli_config, #appinfo_cli_opts, #appinfo_formatters, #appinfo_iparty_config, #appinfo_mmdb_status, #appinfo_runtime
Methods included from Actions
#dispatch_appinfo, #dispatch_help, #dispatch_info, #dispatch_irb
Methods included from Options
#colorized_help_text, #default_options, #init_optparse, #loadrc, #parse_options!, #require_resolv
Constructor Details
#initialize(env:, argv:, argf:, **opts) {|_self| ... } ⇒ Application
Returns a new instance of Application.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/iparty/cli/application.rb', line 33
def initialize(env:, argv:, argf:, **opts)
@env = env
@argv = argv
@argf = argf
@config_path = Pathname.new(env.fetch("IPARTY_CFGDIR", "~/.iparty")).expand_path
@config_file = @config_path.join("config.rb")
@opts = default_options.merge(opts)
@optparse = init_optparse
@options_parsed = false
@rc_disabled = @argv.delete("--no-rc")
@out = DefaultOut.new
loadrc
yield(self) if block_given?
end
|
Instance Attribute Details
#argf ⇒ Object
Returns the value of attribute argf.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def argf
@argf
end
|
#argv ⇒ Object
Returns the value of attribute argv.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def argv
@argv
end
|
#config_file ⇒ Object
Returns the value of attribute config_file.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def config_file
@config_file
end
|
#config_path ⇒ Object
Returns the value of attribute config_path.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def config_path
@config_path
end
|
#env ⇒ Object
Returns the value of attribute env.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def env
@env
end
|
#opts ⇒ Object
Returns the value of attribute opts.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def opts
@opts
end
|
#out ⇒ Object
Returns the value of attribute out.
31
32
33
|
# File 'lib/iparty/cli/application.rb', line 31
def out
@out
end
|
Instance Method Details
#_deep_onlyexcept_kv_match?(key, value, matchers, keep: true, keystack: []) ⇒ Boolean
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/iparty/cli/application.rb', line 228
def _deep_onlyexcept_kv_match? key, value, matchers, keep: true, keystack: []
fullkey = (keystack + [key]).join(".")
unless matched = matchers.any?{ fullkey.match?(_1) }
deep_onlyexcept_data(value, matchers, keep: keep, keystack: keystack + [key])
end
if keep
value.respond_to?(:each) ? !matched && value.empty? : !matched
else
matched || (value.respond_to?(:each) && value.empty?)
end
end
|
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/iparty/cli/application.rb', line 92
def build_formatter fmt = @opts[:formatter], **kw
if fmt.is_a?(CLI::Formatter)
fmt
elsif fmt.is_a?(Class)
fmt.new(self, **kw)
elsif fmt_class = CLI::Formatter.find_by_id(fmt)
fmt_class.new(self, argument: fmt, **kw)
else
raise UnknownFormatter, "unknown formatter: #{fmt}"
end
end
|
#create_matchers(expressions) ⇒ Object
211
212
213
214
215
216
217
|
# File 'lib/iparty/cli/application.rb', line 211
def create_matchers expressions
return if expressions.empty?
expressions.map do |exp|
/\A#{exp.gsub(/\*\*|\*/, { "**" => ".*", "*" => "[^.]*" })}\z/i
end
end
|
#deep_onlyexcept_data(data, matchers, keep: true, keystack: []) ⇒ Object
219
220
221
222
223
224
225
226
|
# File 'lib/iparty/cli/application.rb', line 219
def deep_onlyexcept_data data, matchers, keep: true, keystack: []
case data
when Hash
data.delete_if {|k, v| _deep_onlyexcept_kv_match?(k, v, matchers, keep: keep, keystack: keystack) }
when Array
data.delete_if.with_index {|v, i| _deep_onlyexcept_kv_match?(i, v, matchers, keep: keep, keystack: keystack) }
end
end
|
#dispatch(action: nil) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/iparty/cli/application.rb', line 242
def dispatch action: nil
parse_options!
action ||= @opts[:action]
action_method = :"dispatch_#{action}"
raise ActionNotFound, "unknown action: #{action} (does not respond to ##{action_method})" unless respond_to?(action_method)
puts "[iparty-debug] dispatching #{action_method}" if @opts[:debug]
send(action_method)
rescue CLI::Application::Error => ex
appinfo_formatters(pad: 0) if ex.is_a?(CLI::Application::UnknownFormatter)
@opts[:debug] ? raise(ex) : abort(c(ex.message, :red))
rescue Interrupt, SystemExit => ex
raise(ex) if @opts[:debug]
end
|
#each_address(use_argf: read_from_stdin?, , &block) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/iparty/cli/application.rb', line 82
def each_address use_argf: read_from_stdin?, &block
if use_argf
each_line_in_argf_as_addresses do |addresses|
addresses.each(&block)
end
else
IParty.expand_hostnames(@argv).each(&block)
end
end
|
#each_line_in_argf_as_addresses(prompt: $stdin.tty?, ps1: "> ") ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/iparty/cli/application.rb', line 62
def each_line_in_argf_as_addresses prompt: $stdin.tty?, ps1: "> "
index = 0
print ps1 if prompt
@argf.each_line do |line|
raise(Interrupt) if prompt && line.chomp.match?(/^(q|quit|exit)$/)
line.split(/\s+/).each do |chunk|
addresses = IParty.expand_hostnames(chunk)
yield(addresses, index)
index += addresses.length
end
if prompt
print ps1
index = 0
end
end
end
|
#ensure_mmdb_files!(fetch_when = @opts[:mmdb_fetch_when]) ⇒ Object
50
51
52
|
# File 'lib/iparty/cli/application.rb', line 50
def ensure_mmdb_files! fetch_when = @opts[:mmdb_fetch_when]
IParty::MaxMind.fetch_db_files!(fetch_when, verbose: true)
end
|
104
105
106
|
# File 'lib/iparty/cli/application.rb', line 104
def formatter
@_formatter ||= build_formatter(@opts[:formatter], colorize: @opts[:colorize])
end
|
#ip_to_data(ip, colorize: false) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/iparty/cli/application.rb', line 108
def ip_to_data ip, colorize: false
ipp = IParty(ip)
data = {
type: ipp.type,
prefix: ipp.prefix,
address: ipp.to_s,
cidr: ipp.to_cidr,
}
data[:hostname] = Resolv.getnames(ip).join(" ") if @opts[:resolv]
data.merge!(ipp.as_json)
replace_names_with_singular_for!(@opts[:lang].to_s, data) if @opts[:lang] && @opts[:lang].to_s != "all"
data = summarize(data, colorize: colorize) if @opts[:summarize]
onlyexcept_data!(data)
data
rescue StandardError => ex
@opts[:debug] ? raise(ex) : { error_class: ex.class, error: ex.message }
end
|
#onlyexcept_data!(data) ⇒ Object
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/iparty/cli/application.rb', line 199
def onlyexcept_data! data
if @opts[:only] && matchers = create_matchers(@opts[:only])
deep_onlyexcept_data(data, matchers, keep: true)
end
if @opts[:except] && matchers = create_matchers(@opts[:except])
deep_onlyexcept_data(data, matchers, keep: false)
end
data
end
|
#read_from_stdin? ⇒ Boolean
58
59
60
|
# File 'lib/iparty/cli/application.rb', line 58
def read_from_stdin?
@opts[:stdin] || (@argv.empty? && stdin_select?)
end
|
#replace_names_with_singular_for!(lang, data) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/iparty/cli/application.rb', line 139
def replace_names_with_singular_for!(lang, data)
case data
when Hash
if (names = data.dig(:names)) && (name = names.dig(lang.to_sym) || names.dig(:en))
data[:name] = name
data.delete(:names)
end
data.each_value { replace_names_with_singular_for!(lang, _1) }
when Array
data.each{ replace_names_with_singular_for!(lang, _1) }
end
end
|
#stdin_select? ⇒ Boolean
54
55
56
|
# File 'lib/iparty/cli/application.rb', line 54
def stdin_select?
!$stdin.wait_readable(0).nil?
end
|
#summarize(data, colorize: ) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/iparty/cli/application.rb', line 153
def summarize data, colorize: @opts[:colorize]
with_color(colorize) do
latlong = [data.dig(:location, :latitude), data.dig(:location, :longitude)].compact
{
type: "#{data[:type]}[/#{data[:prefix]}]",
hostname: (data[:hostname] if data[:hostname] && !data[:hostname].empty?),
cidr: (data[:cidr] unless data[:cidr] == data[:address]),
network: summarize_network_detail(data),
name: data.dig(:annotations, :name),
tags: (data.dig(:annotations, :tags).join(" ") if data.dig(:annotations, :tags)&.any?),
location: summarize_location_detail(data),
time_zone: data.dig(:location, :time_zone),
latlong: (c((@opts[:fmt_latlong] || "%f, %f") % latlong, :magenta) unless latlong.empty?),
}.compact
end
end
|
#summarize_asn_detail(data) ⇒ Object
171
172
173
174
175
176
177
|
# File 'lib/iparty/cli/application.rb', line 171
def summarize_asn_detail data
return unless asn_number = data.dig(:autonomous_system_number)
asn_org = data.dig(:autonomous_system_organization)
asn_detail = c("AS#{asn_number} #{c(asn_org, :cyan)}")
asn_detail unless decolorize(asn_detail).empty?
end
|
#summarize_location_detail(data) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/iparty/cli/application.rb', line 184
def summarize_location_detail data
continent_name = data.dig(:continent, :name) || data.dig(:continent, :names, :en)
country_name = data.dig(:country, :name) || data.dig(:country, :names, :en)
country_name ||= data.dig(:registered_country, :name) || data.dig(:registered_country, :names, :en)
city_name = data.dig(:city, :name) || data.dig(:city, :names, :en)
location_detail = [
(c(continent_name, :green) if continent_name),
(c(country_name, :yellow) if country_name),
([c(data.dig(:postal, :code), :cyan), c(city_name, :blue)].compact.join(" ") if city_name),
].compact.join(c(" / ", :black))
location_detail unless decolorize(location_detail).empty?
end
|
#summarize_network_detail(data) ⇒ Object
179
180
181
182
|
# File 'lib/iparty/cli/application.rb', line 179
def summarize_network_detail data
network_detail = [c(data[:network], :blue), summarize_asn_detail(data)].compact.join(c(" -- ", :black))
network_detail unless decolorize(network_detail).empty?
end
|