Method: NicInfo::Main#initialize

Defined in:
lib/nicinfo/nicinfo_main.rb

#initialize(args, config = nil) ⇒ Main

Returns a new instance of Main.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/nicinfo/nicinfo_main.rb', line 83

def initialize args, config = nil

  if config
    @config = config
  else
    @config = NicInfo::Config.new(NicInfo::Config::formulate_app_data_dir())
  end

  @config.options.require_query = true
  @config.options.jcr = JcrMode::NO_VALIDATION

  @opts = OptionParser.new do |opts|

    opts.banner = "Usage: nicinfo [options] QUERY_VALUE"
    opts.version = NicInfo::VERSION

    opts.separator ""
    opts.separator "Query Options:"

    opts.on("-t", "--type TYPE",
            "Specify type of the query value.",
            "  ip4addr      - IPv4 address",
            "  ip6addr      - IPv6 address",
            "  ip4cidr      - IPv4 cidr block",
            "  ip6cidr      - IPv6 cidr block",
            "  asnumber     - autonomous system number",
            "  domain       - domain name",
            "  entityhandle - handle or id of a contact, organization, registrar or other entity",
            "  nameserver   - fully qualified domain name of a nameserver",
            "  result       - result from a previous query",
            "  esbyname     - entity search by name",
            "  dsbyname     - domain search by name",
            "  dsbynsname   - domain search by nameserver name",
            "  dsbynsip     - domain search by nameserver IP address",
            "  nsbyname     - nameserver search by nameserver name",
            "  nsbyip       - nameserver search by IP address",
            "  trace        - trace route",
            "  url          - RDAP URL",
            "  help         - server help") do |type|
      uptype = type.upcase
      raise OptionParser::InvalidArgument, type.to_s unless QueryType.has_value?(uptype)
      @config.options.query_type = uptype
      @config.options.require_query = false if uptype == "HELP"
    end

    opts.on("-r", "--reverse",
            "Creates a reverse DNS name from an IP address. ") do |reverse|
      @config.options.reverse_ip = true
    end

    opts.on("-b", "--base (or bootstrap) URL",
            "The base URL of the RDAP Service.",
            "When set, the internal bootstrap is bypassed.") do |url|
      @config.config[ NicInfo::BOOTSTRAP][ NicInfo::BOOTSTRAP_URL ] = url
    end

    opts.separator ""
    opts.separator "Cache Options:"

    opts.on("--cache-expiry SECONDS",
            "Age in seconds of items in the cache to be considered expired.") do |s|
      @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EXPIRY ] = s
    end

    opts.on("--cache YES|NO|TRUE|FALSE",
            "Controls if the cache is used or not.") do |cc|
      @config.config[ NicInfo::CACHE ][ NicInfo::USE_CACHE ] = false if cc =~ /no|false/i
      @config.config[ NicInfo::CACHE ][ NicInfo::USE_CACHE ] = true if cc =~ /yes|true/i
      raise OptionParser::InvalidArgument, cc.to_s unless cc =~ /yes|no|true|false/i
    end

    opts.on("--empty-cache",
            "Empties the cache of all files regardless of eviction policy.") do |cc|
      @config.options.empty_cache = true
      @config.options.require_query = false
    end

    opts.on("--demo",
            "Populates the cache with demonstration results.") do |cc|
      @config.options.demo = true
      @config.options.require_query = false
    end

    opts.separator ""
    opts.separator "Output Options:"

    opts.on( "--messages MESSAGE_LEVEL",
             "Specify the message level",
             "  none - no messages are to be output",
             "  some - some messages but not all",
             "  all  - all messages to be outupt" ) do |m|
      @config.logger.message_level = m.to_s.upcase
      begin
        @config.logger.validate_message_level
      rescue
        raise OptionParser::InvalidArgument, m.to_s
      end
    end

    opts.on( "--messages-out FILE",
             "FILE where messages will be written." ) do |f|
      @config.logger.messages_out = File.open( f, "w+" )
    end

    opts.on( "--data DATA_AMOUNT",
             "Specify the amount of data",
             "  terse  - enough data to identify the object",
             "  normal - normal view of data on objects",
             "  extra  - all data about the object" ) do |d|
      @config.logger.data_amount = d.to_s.upcase
      begin
        @config.logger.validate_data_amount
      rescue
        raise OptionParser::InvalidArgument, d.to_s
      end
    end

    opts.on( "--data-out FILE",
             "FILE where data will be written." ) do |f|
      @config.logger.data_out = File.open( f, "w+" )
    end

    opts.on( "--pager YES|NO|TRUE|FALSE",
             "Turns the pager on and off." ) do |pager|
      @config.logger.pager = false if pager =~ /no|false/i
      @config.logger.pager = true if pager =~ /yes|true/i
      raise OptionParser::InvalidArgument, pager.to_s unless pager =~ /yes|no|true|false/i
    end

    opts.on( "--color-scheme DARK|LIGHT|NONE",
             "Determines color scheme to use:",
             "  dark  - for terminals with dark backgrounds",
             "  light - for terminals with light backgrounds",
             "  none  - turn off colors" ) do |cs|
      @config.logger.color_scheme = cs.to_s.upcase
      raise OptionParser::InvalidArgument, cs.to_s unless cs =~ /dark|light|none/i
    end

    opts.on( "-V",
             "Equivalent to --messages all and --data extra" ) do |v|
      @config.logger.data_amount = NicInfo::DataAmount::EXTRA_DATA
      @config.logger.message_level = NicInfo::MessageLevel::ALL_MESSAGES
    end

    opts.on( "-Q",
             "Equivalent to --messages none and --data extra and --pager false" ) do |q|
      @config.logger.data_amount = NicInfo::DataAmount::EXTRA_DATA
      @config.logger.message_level = NicInfo::MessageLevel::NO_MESSAGES
      @config.logger.pager = false
    end

    opts.on( "--json",
             "Output raw JSON response." ) do |json|
      @config.options.output_json = true
    end

    opts.on( "--jv VALUE",
             "Outputs a specific JSON value." ) do |value|
      unless @config.options.json_values
        @config.options.json_values = Array.new
      end
      @config.options.json_values << value
    end

    opts.separator ""
    opts.separator "Security Options:"

    opts.on( "--try-insecure YES|NO|TRUE|FALSE",
             "Try HTTP if HTTPS fails" ) do |try_insecure|
      @config.config[ NicInfo::SECURITY ][ NicInfo::TRY_INSECURE ] = false if try_insecure =~ /no|false/i
      @config.config[ NicInfo::SECURITY ][ NicInfo::TRY_INSECURE ] = true if try_insecure =~ /yes|true/i
      raise OptionsParser::InvalidArgument, try_insecure.to_s unless try_insecure =~/yes|no|true|false/i
    end

    opts.separator ""
    opts.separator "General Options:"

    opts.on( "-h", "--help",
             "Show this message" ) do
      @config.options.help = true
      @config.options.require_query = false
    end

    opts.on( "--reset",
             "Reset configuration to defaults" ) do
      @config.options.reset_config = true
      @config.options.require_query = false
    end

    opts.on( "--iana",
             "Download RDAP bootstrap files from IANA" ) do
      @config.options.get_iana_files = true
      @config.options.require_query = false
    end

    opts.on( "--jcr STANDARD|STRICT",
             "Validate RDAP response with JCR") do |mode|
      upmode = mode.upcase
      raise OptionParser::InvalidArgument, type.to_s unless JcrMode.has_value?(upmode)
      @config.options.jcr = upmode
      get_jcr_context if upmode == JcrMode::STANDARD_VALIDATION
      get_jcr_strict_context if upmode == JcrMode::STRICT_VALIDATION
    end

  end

  begin
    @opts.parse!(args)
  rescue OptionParser::InvalidOption => e
    puts e.message
    puts "use -h for help"
    exit
  rescue OptionParser::InvalidArgument => e
    puts e.message
    puts "use -h for help"
    exit
  rescue
    puts "Unable to parse command line options"
    puts "use -h for help"
    exit
  end
  @config.options.argv = args

end