Class: Aws::RiKanjoo::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



8
9
10
11
12
# File 'lib/aws/rikanjo/cli.rb', line 8

def initialize
  @options = {}
  @optparse = nil
  @cost = nil
end

Instance Method Details

#outputObject



93
94
95
96
97
98
99
# File 'lib/aws/rikanjo/cli.rb', line 93

def output
  if @options[:output_json]
    output_json()
  else
    output_text()
  end
end

#output_jsonObject



101
102
103
# File 'lib/aws/rikanjo/cli.rb', line 101

def output_json
  puts @cost.to_json
end

#output_textObject



105
106
107
108
109
# File 'lib/aws/rikanjo/cli.rb', line 105

def output_text
  @cost.keys.each do |key|
    puts "#{key}: #{@cost[key]}"
  end
end

#parseObject



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
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
# File 'lib/aws/rikanjo/cli.rb', line 20

def parse
  optparse = OptionParser.new do |opts|
    opts.banner = 'Usage: rikanjo ec2/rds [options]'

    # subcommand
    mode = ARGV.shift
    if mode == 'ec2'
    elsif mode == 'rds'
      opts.on('--multiaz', 'enable multi-az') do |value|
        @options[:multiaz] = value
      end
    else
      $stderr.puts "no such subcommand: #{mode}"
      puts opts
      exit 1
    end
    @options[:mode] = mode

    region_values = %w(us-east-1 us-west-1 us-west-2 eu-west-1 ap-southeast-1 ap-northeast-1 ap-southeast-2 sa-east-1 )
    opts.on('-r', '--region=VALUE', region_values, "specify aws-region (#{region_values.join('/')})") do |value|
      @options[:region] = value
    end

    opts.on('-t', '--instance_type=VALUE', 'specify ec2-instance-type') do |value|
      @options[:instance_type] = value
    end

    ri_util_values = %w(light medium heavy)
    opts.on('-u', '--ri_util=VALUE', ri_util_values, "specify ri-util (#{ri_util_values.join('/')})") do |value|
      @options[:ri_util] = value
    end

    opts.on('-j', '--output_json', "specify output_json flg") do |value|
      @options[:output_json] = value
    end

    opts.on('-h', '--help') do
      puts opts
      exit
    end
  end

  # validation
  begin
    optparse.parse!
    require_args = [:region, :instance_type, :ri_util]
    error_args = require_args.select { |param| @options[param].nil? }
    unless error_args.empty?
      puts "require arguments: #{error_args.join(', ')}"
      puts
      puts optparse
      exit
    end
  rescue
    puts $!.to_s
    puts
    puts optparse
    exit
  end
end

#rikanjoObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aws/rikanjo/cli.rb', line 81

def rikanjo
  # rikanjo
  a = Aws::RiKanjoo::Base.new(
      mode = @options[:mode],
      region = @options[:region],
      instance_type = @options[:instance_type],
      ri_util = @options[:ri_util],
      multiaz = @options[:multiaz],
  )
  @cost = a.total_cost_year
end

#startObject



14
15
16
17
18
# File 'lib/aws/rikanjo/cli.rb', line 14

def start
  parse
  rikanjo
  output
end