Class: Arcus::Cli
- Inherits:
-
Object
show all
- Includes:
- Api
- Defined in:
- lib/arcus/cli.rb
Instance Method Summary
collapse
Methods included from Api
configure, load_config_file, parse_action, #scoped, settings, settings=, targets
Constructor Details
#initialize(options = {}) ⇒ Cli
Returns a new instance of Cli.
10
11
12
13
14
15
16
17
|
# File 'lib/arcus/cli.rb', line 10
def initialize(options = {})
Api.configure do |c|
c.api_uri = options[:api_uri] if options[:api_uri]
c.api_key = options[:api_key] if options[:api_key]
c.api_secret = options[:api_secret] if options[:api_secret]
c.default_response = options[:default_response] if options[:default_response]
end
end
|
Instance Method Details
#class_string(clazz) ⇒ Object
35
36
37
|
# File 'lib/arcus/cli.rb', line 35
def class_string(clazz)
clazz.name.split('::').last
end
|
#class_sym(clazz) ⇒ Object
39
40
41
|
# File 'lib/arcus/cli.rb', line 39
def class_sym(clazz)
class_string(clazz).to_sym
end
|
#method_string(meth) ⇒ Object
47
48
49
|
# File 'lib/arcus/cli.rb', line 47
def method_string(meth)
meth["name"]
end
|
#method_sym(meth) ⇒ Object
43
44
45
|
# File 'lib/arcus/cli.rb', line 43
def method_sym(meth)
meth["name"].to_sym
end
|
#run ⇒ Object
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/arcus/cli.rb', line 51
def run
cmd = CmdParse::CommandParser.new(true, true)
cmd.program_name = $0
cmd.program_version = [0, 0, 1]
cmd.options = CmdParse::OptionParserWrapper.new do |opt|
opt.separator "Global options:"
opt.on("--verbose", "Be verbose when outputting info") { Arcus::Api.settings.verbose = true }
end
cmd.add_command(CmdParse::HelpCommand.new)
cmd.add_command(CmdParse::VersionCommand.new)
Api.targets.each do |t|
target_name = class_string(t).downcase
target_cmd = CmdParse::Command.new(target_name, true, true)
cmd.add_command(target_cmd)
t.actions.each do |a|
action_name = a.action_name
action_cmd = CmdParse::Command.new(action_name, false)
action_cmd.short_desc = a.description.split(".").first
required_params = optional_params = {}
action_cmd.options = CmdParse::OptionParserWrapper.new do |opt|
if !a.required_args.empty?
opt.separator "Required:"
opt.separator ""
a.required_args.each do |ra|
opt.on("--#{ra['name']} #{ra['name'].upcase}", *text_width(ra["description"], 100)) do |val|
required_params[ra["name"].to_sym] = val
end
end
end
if !a.optional_args.empty?
opt.separator ""
opt.separator "Optional:"
opt.separator ""
a.optional_args.each do |ra|
opt.on("--#{ra['name']} #{ra['name'].upcase}", *text_width(ra["description"], 100)) do |val|
required_params[ra["name"].to_sym] = val
end
end
end
if a.is_async
opt.on("--sync POLL_INTERVAL", "continuously poll the job for results ever POLL_INTERVAL seconds") do |val|
a.sync = val
end
end
end
action_cmd.set_execution_block {
missing_params = a.required_args.map { |n| n["name"].to_sym } - required_params.keys
if (missing_params.empty?)
begin
response_type = optional_params[:response] || Api.settings.default_response
if a.sync
result = a.new.prepare(required_params.merge(optional_params)).fetch
job_id = result["#{a.name.downcase}response"]["jobid"]
job_finished = false
until job_finished
sleep a.sync.to_i.seconds
job_result = AsyncJobResult.new.query({:jobid => job_id}).fetch
if job_result["queryasyncjobresultresponse"]["jobstatus"] != 0
job_finished = true
end
end
puts AsyncJobResult.new.query({:jobid => job_id}).fetch(response_type.to_sym)
else
puts a.new.prepare(required_params.merge(optional_params)).fetch(response_type.to_sym)
end
rescue Timeout::Error => e
puts "Timeout connecting to #{e.api_uri}"
exit(false)
end
else
puts "Missing required ARGS: #{missing_params.map { |n| "--#{n}" }.join(", ")}\n"
action_cmd.show_help
exit(false)
end
}
target_cmd.add_command(action_cmd)
end
end
cmd.parse
end
|
#text_width(text, width) ⇒ Object
31
32
33
|
# File 'lib/arcus/cli.rb', line 31
def text_width(text, width)
word_wrap(text, :line_width => width).split("\n") if !text.nil?
end
|
#word_wrap(text, *args) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/arcus/cli.rb', line 19
def word_wrap(text, *args)
options = args.
unless args.blank?
options[:line_width] = args[0] || 80
end
options.reverse_merge!(:line_width => 80)
text.split("\n").collect do |line|
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
end * "\n"
end
|