This repository has been archived by the owner on May 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathconsole
executable file
·106 lines (86 loc) · 2.39 KB
/
console
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
begin
require 'bundler'
rescue LoadError
begin
require 'rubygems'
require 'bundler'
rescue LoadError => e
$stderr.puts "ERROR: Unable to generate because an error occurred while loading: #{e}"
end
end
Bundler.setup
require 'optparse'
require 'json'
class ConsoleCLI
attr_reader :port
DEFAULT_PORT = 5000
def initialize(args)
@port = DEFAULT_PORT
parse_args(args)
end
def run
ENV['PORT'] = port.to_s
require 'irb'
require File.expand_path('../console.rb', __FILE__)
ARGV.unshift '--simple-prompt'
IRB.start(__FILE__)
end
protected
def parse_args(args)
begin
@args = args
parser.parse!
rescue ArgumentError => ex
$stderr.puts "#{$0}: error: #{ex.message}"
puts
puts parser
exit(-1)
end
end
def parser
@parser ||= OptionParser.new(@args) do |opts|
opts.banner = "Usage: script/console [options]"
opts.on('-h', '--help', "Show this message") do
puts opts
exit
end
opts.on('-p', '--port=PORT', Integer, "Use the given port (default #{DEFAULT_PORT})") do |port|
@port = port
end
opts.on('-P', '--profile=PATH', "Connect to the xulrunner running with the specified profile path") do |path|
needs 'json', '--profile'
read_port_from_profile(path)
end
opts.on('-J', '--job=JOBID', "Connect to the xulrunner running the job with the given GUID") do |jobid|
needs 'json', '--job'
read_port_from_profile("/var/wesabe/ssu-profiles/#{jobid}")
end
end
end
def needs(lib, who=$0)
begin
require lib
rescue LoadError
begin
require 'rubygems'
require lib
rescue LoadError
$stderr.puts "Using #{who} requires #{lib} to be installed, but it can't be found"
end
end
return true
end
def read_port_from_profile(path)
config_path = path + '/config'
raise ArgumentError, "Profile configuration does not exist at #{config_path}" unless File.exist?(config_path)
begin
config = JSON.parse(File.read(config_path))
rescue JSON::ParseError => ex
raise ArgumentError, "Profile configuration file is not valid JSON: #{ex.message}"
end
raise ArgumentError, "Profile config at #{config_path} does not contain 'port'" unless config.key?('port')
@port = config['port']
end
end
ConsoleCLI.new(ARGV).run