Class: TuneMyGc::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CLI

Returns a new instance of CLI.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tunemygc/cli.rb', line 30

def initialize(options)
  @options = options
  @client = TuneMyGc.http_client
  if options[:email]
    register
  elsif options[:config]
    fetch_config
  else
    raise ArgumentError, "Invalid CLI argument: you can either register or retrieve your last known GC config"
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/tunemygc/cli.rb', line 8

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/tunemygc/cli.rb', line 8

def options
  @options
end

Class Method Details

.start(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tunemygc/cli.rb', line 10

def self.start(args)
  args = ["-h"] if args.empty?
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: tunemygc [options]"

    opts.on("-r ", "--register EMAIL", "Register this application with the https://tunemygc.com service") do |email|
      options[:email] = email
    end
    opts.on("-c ", "--config TOKEN", "Fetch the last known config for a given application") do |token|
      options[:config] = token
    end
    opts.on_tail("-h", "--help", "How to use the TuneMyGC agent CLI") do
      puts opts
      exit
    end
  end.parse!(args)
  new(options)
end

Instance Method Details

#fetch_configObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tunemygc/cli.rb', line 59

def fetch_config
  timeout do
    config = Net::HTTP::Get.new("/apps/#{options[:config]}")
    response = client.request(config)
    if Net::HTTPNoContent === response
      puts "There is no configuration for Rails app with token #{options[:config]} yet"
    elsif Net::HTTPNotFound === response
      puts "Rails app with token #{options[:config]} doesn't exist"
    elsif Net::HTTPSuccess === response
      puts "=== Suggested GC configuration:"
      puts
      puts response.body
    else
      puts "Config retrieval error: #{response.body}"
    end
  end
rescue Exception => e
  puts "Config retrieval error: #{e.inspect}"
end

#registerObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tunemygc/cli.rb', line 42

def register
  timeout do
    registration = Net::HTTP::Post.new('/accounts')
    registration.set_form_data(:email => options[:email], :app => app_name)
    response = client.request(registration)
    if Net::HTTPUnprocessableEntity === response
      puts "Registration error: #{response.body}"
    elsif Net::HTTPSuccess === response
      puts "Application #{app_name} registered. Use RUBY_GC_TOKEN=#{response.body} in your environment."
    else
      puts "Registration error: #{response.body}"
    end
  end
rescue Exception => e
  puts "Registration error: #{e.inspect}"
end