Class: AwsPublicIps::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_public_ips/cli.rb

Instance Method Summary collapse

Instance Method Details

#all_formatsObject



45
46
47
# File 'lib/aws_public_ips/cli.rb', line 45

def all_formats
  @all_formats ||= Dir['lib/aws_public_ips/formatters/*.rb'].map { |path| File.basename(path, '.rb') }.sort
end

#all_servicesObject

Services that maybe have public endpoints / still need testing: fargate amazonmq directory service (AD) emr Directconnect Kinesis SES aws.amazon.com/products/ AWS Neptune (still in preview / not GA yet)



41
42
43
# File 'lib/aws_public_ips/cli.rb', line 41

def all_services
  @all_services ||= Dir['lib/aws_public_ips/checks/*.rb'].map { |path| File.basename(path, '.rb') }.sort
end

#check_service(service) ⇒ Object



83
84
85
86
# File 'lib/aws_public_ips/cli.rb', line 83

def check_service(service)
  require "aws_public_ips/checks/#{service}.rb"
  AwsPublicIps::Checks.const_get(service.capitalize).run
end

#output(formatter, results) ⇒ Object



88
89
90
91
92
93
# File 'lib/aws_public_ips/cli.rb', line 88

def output(formatter, results)
  require "aws_public_ips/formatters/#{formatter}.rb"
  formatter_klass = AwsPublicIps::Formatters.const_get(formatter.capitalize)
  output = formatter_klass.new(results).format
  puts output unless output.empty?
end

#parse(args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aws_public_ips/cli.rb', line 49

def parse(args)
  options = {
    format: 'text',
    services: all_services,
    verbose: false
  }

  OptionParser.new do |opts|
    opts.banner = 'Usage: aws_public_ips [options]'

    opts.on('-s', '--services <s1>,<s2>,<s3>', Array, 'List of AWS services to check. Available services: ' \
      "#{all_services.join(',')}. Defaults to all.") do |services|
      services.map(&:downcase!).uniq!
      invalid_services = services - all_services
      raise ArgumentError, "Invalid service(s): #{invalid_services.join(',')}" unless invalid_services.empty?
      options[:services] = services
    end

    opts.on('-f', '--format <format>', String, 'Set output format. Available formats: ' \
      "#{all_formats.join(',')}. Defaults to text.") do |fmt|
      unless all_formats.include?(fmt)
        raise ArgumentError, "Invalid format '#{fmt}'. Valid formats are: #{all_formats.join(',')}"
      end
      options[:format] = fmt
    end

    opts.on('-v', '--[no-]verbose', 'Enable debug/trace output') do |verbose|
      options[:verbose] = verbose
    end
  end.parse(args)

  options
end

#run(args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aws_public_ips/cli.rb', line 95

def run(args)
  options = parse(args)

  results = options[:services].map do |service|
    [service.to_sym, check_service(service)]
  end.to_h

  output(options[:format], results)
rescue StandardError, Interrupt => ex
  puts ex.inspect
  puts ex.backtrace if options[:verbose]
  Process.exit(1)
end