Class: TuneMyGc::CLI
- Inherits:
-
Object
- Object
- TuneMyGc::CLI
- Defined in:
- lib/tunemygc/cli.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_config ⇒ Object
-
#initialize(options) ⇒ CLI
constructor
A new instance of CLI.
- #register ⇒ Object
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 = @client = TuneMyGc.http_client if [:email] register elsif [: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
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/tunemygc/cli.rb', line 8 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/tunemygc/cli.rb', line 8 def @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? = {} OptionParser.new do |opts| opts. = "Usage: tunemygc [options]" opts.on("-r ", "--register EMAIL", "Register this application with the https://tunemygc.com service") do |email| [:email] = email end opts.on("-c ", "--config TOKEN", "Fetch the last known config for a given application") do |token| [:config] = token end opts.on_tail("-h", "--help", "How to use the TuneMyGC agent CLI") do puts opts exit end end.parse!(args) new() end |
Instance Method Details
#fetch_config ⇒ Object
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/#{[:config]}") response = client.request(config) if Net::HTTPNoContent === response puts "There is no configuration for Rails app with token #{[:config]} yet" elsif Net::HTTPNotFound === response puts "Rails app with token #{[: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 |
#register ⇒ Object
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 => [: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 |