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["#{__dir__}/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["#{__dir__}/checks/*.rb"].map { |path| ::File.basename(path, '.rb') }.sort
end

#check_service(service) ⇒ Object



96
97
98
99
# File 'lib/aws_public_ips/cli.rb', line 96

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

#output(formatter, options, results) ⇒ Object



101
102
103
104
105
106
# File 'lib/aws_public_ips/cli.rb', line 101

def output(formatter, options, results)
  require "aws_public_ips/formatters/#{formatter}.rb"
  formatter_klass = ::AwsPublicIps::Formatters.const_get(formatter.capitalize)
  output = formatter_klass.new(results, options).format
  ::STDOUT.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
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/aws_public_ips/cli.rb', line 49

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

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

    parser.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

    parser.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

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

    parser.on_tail('--version', 'Print version') do
      require 'aws_public_ips/version'
      ::STDOUT.puts ::AwsPublicIps::VERSION
      return nil  # nil to avoid rubocop warning
    end

    parser.on_tail('-h', '--help', 'Show this help message') do
      ::STDOUT.puts parser
      return nil  # nil to avoid rubocop warning
    end
  end.parse(args)

  options
end

#run(args) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/aws_public_ips/cli.rb', line 108

def run(args)
  options = parse(args)
  return unless options

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

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