Class: Quarantine::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



9
10
11
12
13
14
15
# File 'lib/quarantine/cli.rb', line 9

def initialize
  # default options
  @options = {
    quarantine_list_table_name: 'quarantine_list',
    failed_test_table_name: 'master_failed_tests'
  }
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/quarantine/cli.rb', line 7

def options
  @options
end

Instance Method Details

#create_tablesObject

TODO: eventually move to a separate file & create_table by db type when my db adapters



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
# File 'lib/quarantine/cli.rb', line 57

def create_tables
  dynamodb = Quarantine::Databases::DynamoDB.new(options)

  attributes = [
    { attribute_name: 'id', attribute_type: 'S', key_type: 'HASH' },
    { attribute_name: 'build_number', attribute_type: 'S', key_type: 'RANGE' }
  ]

  additional_arguments = {
    provisioned_throughput: {
      read_capacity_units: 10,
      write_capacity_units: 5
    }
  }

  begin
    dynamodb.create_table(options[:quarantine_list_table_name], attributes, additional_arguments)
  rescue Quarantine::DatabaseError => e
    warn "#{e&.cause&.class}: #{e&.cause&.message}"
  end

  begin
    dynamodb.create_table(options[:failed_test_table_name], attributes, additional_arguments)
  rescue Quarantine::DatabaseError => e
    warn "#{e&.cause&.class}: #{e&.cause&.message}"
  end
end

#parseObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/quarantine/cli.rb', line 17

def parse
  OptionParser.new do |parser|
    parser.banner = 'Usage: quarantine_dynamodb [options]'

    parser.on('-rREGION', '--aws_region=REGION', String, 'Specify the aws region for DynamoDB') do |aws_region|
      options[:aws_region] = aws_region
    end

    parser.on(
      '-qTABLE',
      '--quarantine_table=TABLE',
      String,
      "Specify the table name for the quarantine list | Default: #{options[:quarantine_list_table_name]}"
    ) do |table_name|
      options[:quarantine_list_table_name] = table_name
    end

    parser.on(
      '-fTABLE',
      '--failed_table=TABLE',
      String,
      "Specify the table name for the failed test list | Default: #{options[:failed_test_table_name]}"
    ) do |table_name|
      options[:failed_test_table_name] = table_name
    end

    parser.on('-h', '--help', 'Prints help page') do
      puts parser # rubocop:disable Rails/Output
      exit
    end
  end.parse!

  if options[:aws_region].nil?
    error_msg = 'Failed to specify the required aws region with -r option'.freeze
    warn error_msg
    raise ArgumentError.new(error_msg)
  end
end