rhcp_shell 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rhcp_shell.rb ADDED
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "readline"
4
+ require "logger"
5
+ require "getoptlong"
6
+
7
+ require 'rubygems'
8
+
9
+ require 'rhcp'
10
+
11
+ require 'base_shell'
12
+ require 'rhcp_shell_backend'
13
+
14
+ # TODO version number
15
+ SHELL_VERSION = "0.1"
16
+
17
+ class RhcpShell
18
+
19
+ HELP_STRING = <<EOF
20
+
21
+ RHCP Command Shell v #{SHELL_VERSION} (using RHCP library v #{RHCP::Version.to_s})
22
+
23
+ Usage:
24
+ rhcp_shell.rb [--hostname=<hostname>] [--username=<username> --password=<password>]
25
+ [--help]
26
+
27
+ Options:
28
+ --hostname=<hostname>
29
+ the URL to the RHCP server you want to connect against, e.g.
30
+ http://server.local.network/rhcp
31
+ If the specified hostname does not start with "http", it is automatically expanded to
32
+ http://<hostname>:42000/rhcp
33
+ You can optionally specify a port number to connect against like this:
34
+ http://myserver:42000
35
+ If you do not specify a hostname, the shell will try to connect against
36
+ http://localhost:42000
37
+
38
+ --username/--password
39
+ the authentication data you want to use for connecting to the RHCP server
40
+
41
+ --help
42
+ displays this help screen.
43
+
44
+ EOF
45
+
46
+ def run
47
+ opts = GetoptLong.new(
48
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
49
+ [ '--username', '-u', GetoptLong::REQUIRED_ARGUMENT ],
50
+ [ '--password', '-p', GetoptLong::REQUIRED_ARGUMENT ],
51
+ [ '--hostname', GetoptLong::REQUIRED_ARGUMENT ]
52
+ )
53
+
54
+ options = Hash.new
55
+ opts.each do |opt, arg|
56
+ case opt
57
+ when '--help'
58
+ puts HELP_STRING
59
+ Kernel.exit(1)
60
+ else
61
+ opt =~ /--(.+)/
62
+ $logger.debug "setting #{arg} for #{$1}" unless $1 == "password"
63
+ options[$1] = arg
64
+ end
65
+ end
66
+
67
+ host = options["hostname"]
68
+ if host == nil then
69
+ host = "http://localhost:42000"
70
+ else
71
+ if host !~ /http:/ then
72
+ host = "http://#{host}:42000/rhcp"
73
+ end
74
+ end
75
+ $logger.debug "now connecting to #{host}"
76
+
77
+ # TODO add interactive query for password!
78
+
79
+ begin
80
+ url = URI.parse(host)
81
+ @http_broker = RHCP::Client::HttpBroker.new(url)
82
+
83
+ backend = RHCPShellBackend.new(@http_broker)
84
+ backend.banner = <<EOF
85
+ Good morning, this is the generic RHCP Shell.
86
+ Press <tab> for command completion or type "help" for a list of commands.
87
+ If you want to exit this shell, please press Ctrl+C or type "exit".
88
+
89
+ EOF
90
+ $logger.debug "backend has been instantiated : #{backend}"
91
+
92
+ shell = BaseShell.new(backend)
93
+ shell.run
94
+ rescue => ex
95
+ puts "There occurred an HTTP error while connecting to the RHCP: #{ex}"
96
+ puts "Please connect against another server or fix the connection problem."
97
+ puts ex.backtrace.join("\n")
98
+ end
99
+ end
100
+ end
101
+
102
+
103
+ # TODO introduce something like the RAILS_ENVs
104
+ $logger = Logger.new("rhcp_shell.log")
105
+ RHCP::ModuleHelper.instance().logger = $logger
106
+
107
+ shell = RhcpShell.new
108
+ shell.run
@@ -272,6 +272,7 @@ class RHCPShellBackend < ShellBackend
272
272
  response = command.execute_request(request)
273
273
  if (response.status == RHCP::Response::Status::OK)
274
274
  $logger.debug "raw result : #{response.data}"
275
+ $logger.debug "result hints: #{command.result_hints}"
275
276
  $logger.debug "display_type : #{command.result_hints[:display_type]}"
276
277
  if command.result_hints[:display_type] == "table"
277
278
  @last_response = response # we might want to access this response in further commands
@@ -281,16 +282,19 @@ class RHCPShellBackend < ShellBackend
281
282
  output = ""
282
283
 
283
284
  # let's find out which columns we want to display
284
- $logger.debug "overview columns : #{command.result_hints[:overview_columns]}"
285
+ $logger.debug "overview columns : #{command.result_hints[:overview_columns]}"
286
+ # TODO this will probably fail if no overview_columns are specified
285
287
  columns_to_display = command.result_hints.has_key?(:overview_columns) ?
286
288
  command.result_hints[:overview_columns].clone() :
287
289
  # by default, we'll display all columns, sorted alphabetically
288
290
  columns_to_display = response.data[0].keys.sort
289
291
 
290
292
  # and which titles they should have (default : column names)
291
- column_title_list = command.result_hints.has_key?(:column_titles) ?
293
+ $logger.debug "column titles : #{command.result_hints[:column_titles].length}"
294
+ column_title_list = command.result_hints[:column_titles].length > 0 ?
292
295
  command.result_hints[:column_titles].clone() :
293
- column_title_list = columns_to_display
296
+ column_title_list = columns_to_display
297
+ $logger.debug "column title list : #{column_title_list}"
294
298
 
295
299
  # TODO the sorting column should be configurable
296
300
  first_column = columns_to_display[0]
@@ -306,15 +310,18 @@ class RHCPShellBackend < ShellBackend
306
310
  count = count+1
307
311
  end
308
312
 
309
-
310
313
  column_titles = {}
311
314
  0.upto(column_title_list.length - 1) do |i|
312
315
  column_titles[columns_to_display[i]] = column_title_list[i]
313
316
  end
314
- $logger.debug "column title : #{column_titles}"
317
+ $logger.debug "column titles : #{column_titles}"
315
318
 
316
- # find the maximum column width for each column
319
+ # initialize the max_width for all columns
317
320
  @max_width = {}
321
+ column_titles.each do |key, value|
322
+ @max_width[key] = 0
323
+ end
324
+ # find the maximum column width for each column
318
325
  response.data.each do |row|
319
326
  row.each do |k,v|
320
327
  if ! @max_width.has_key?(k) || v.to_s.length > @max_width[k]
@@ -323,7 +330,7 @@ class RHCPShellBackend < ShellBackend
323
330
  end
324
331
  end
325
332
 
326
- # check the column_title
333
+ # check the column_title for max width
327
334
  columns_to_display.each do |col_name|
328
335
  if column_titles[col_name].length > @max_width[col_name]
329
336
  @max_width[col_name] = column_titles[col_name].length
data/lib/version.rb CHANGED
@@ -5,7 +5,7 @@ module RhcpShell #:nodoc:
5
5
 
6
6
  MAJOR = 0
7
7
  MINOR = 0
8
- TINY = 6
8
+ TINY = 7
9
9
 
10
10
  def Version.to_s
11
11
  [ MAJOR, MINOR, TINY ].join(".")
@@ -63,8 +63,11 @@ class RhcpShellBackendTest < Test::Unit::TestCase
63
63
  #puts "log now : >>#{@log.join("\n")}<<"
64
64
  end
65
65
 
66
- def assert_received(expected)
67
- assert_equal(expected, @log.slice(- expected.length, expected.length))
66
+ def assert_received(expected)
67
+ @log_slice = expected.length < @log.length ?
68
+ @log.slice(- expected.length, expected.length) :
69
+ @log
70
+ assert_equal(expected, @log_slice)
68
71
  # clean the log so that assert_no_error works
69
72
  #@log.clear
70
73
  end
@@ -224,13 +227,45 @@ class RhcpShellBackendTest < Test::Unit::TestCase
224
227
  # TODO write a separate test for this stuff
225
228
  p $broker.get_command("build_a_table")
226
229
  @backend.process_input "build_a_table"
227
- # assert_received [
228
- # "Zaphod\tBeeblebrox",
229
- # "Arthur\tDent",
230
- # "Prostetnik\tYoltz(?)"
231
- # ]
230
+ # puts "===================\n#{@log}\n=====================\n"
231
+ assert_received [
232
+ "---------------------------------------\n" +
233
+ "| # | __idx | first_name | last_name |\n" +
234
+ "---------------------------------------\n" +
235
+ "| | 1 | Arthur | Dent |\n" +
236
+ "| | 2 | Prostetnik | Yoltz (?) |\n" +
237
+ "| | 3 | Zaphod | Beeblebrox |\n" +
238
+ "---------------------------------------\n"
239
+ ]
232
240
  end
233
241
 
242
+ def test_empty_table
243
+ @backend.process_input "build_a_table empty=true"
244
+ assert_received [
245
+ "--------------------------------------\n" +
246
+ "| # | __idx | first_name | last_name |\n" +
247
+ "--------------------------------------\n" +
248
+ "--------------------------------------\n"
249
+ ]
250
+ puts "===================\n#{@log}\n=====================\n"
251
+ end
252
+
234
253
  # TODO test behaviour after an internal error occurred in the shell (e.g. some problem while formatting the result)
254
+ # E, [2009-09-17T01:21:53.399309 #9118] ERROR -- : comparison of Fixnum with nil failed (ArgumentError)
255
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/rhcp_shell_backend.rb:335:in `>'
256
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/rhcp_shell_backend.rb:335:in `execute_command'
257
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/rhcp_shell_backend.rb:334:in `each'
258
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/rhcp_shell_backend.rb:334:in `execute_command'
259
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/rhcp_shell_backend.rb:183:in `execute_command_if_possible'
260
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/rhcp_shell_backend.rb:475:in `process_input'
261
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/base_shell.rb:93:in `run'
262
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/base_shell.rb:84:in `initialize'
263
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/base_shell.rb:84:in `new'
264
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/base_shell.rb:84:in `run'
265
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/base_shell.rb:83:in `loop'
266
+ # /var/lib/gems/1.8/gems/rhcp_shell-0.0.7/lib/base_shell.rb:83:in `run'
267
+ # ./virtualop_rhcp.rb:131:in `setup_local_shell'
268
+ # ./virtualop_rhcp.rb:168
269
+ # exception raised: comparison of Fixnum with nil failed
235
270
 
236
271
  end
@@ -92,13 +92,19 @@ broker.register_command command
92
92
 
93
93
  command2 = RHCP::Command.new("build_a_table", "this command returns tabular data",
94
94
  lambda { |req,res|
95
- [
96
- { :first_name => "Zaphod", :last_name => "Beeblebrox", :heads => 2, :character => "dangerous" },
97
- { :first_name => "Arthur", :last_name => "Dent", :heads => 1, :character => "harmless (mostly)" },
98
- { :first_name => "Prostetnik", :last_name => "Yoltz (?)", :heads => 1, :character => "ugly" }
95
+ req.has_param_value("empty") ? [] :
96
+ [
97
+ { "first_name" => "Zaphod", "last_name" => "Beeblebrox", "heads" => 2, "character" => "dangerous" },
98
+ { "first_name" => "Arthur", "last_name" => "Dent", "heads" => 1, "character" => "harmless (mostly)" },
99
+ { "first_name" => "Prostetnik", "last_name" => "Yoltz (?)", "heads" => 1, "character" => "ugly" }
99
100
  ]
100
101
  }
101
- )
102
+ ).add_param(RHCP::CommandParam.new("empty", "if this is true, the table will be empty",
103
+ {
104
+ :mandatory => false,
105
+ :is_default_param => true
106
+ }
107
+ ))
102
108
  command2.mark_as_read_only()
103
109
  command2.result_hints[:display_type] = "table"
104
110
  command2.result_hints[:overview_columns] = [ "first_name", "last_name" ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhcp_shell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philipp Traeder
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-21 00:00:00 +01:00
12
+ date: 2009-09-17 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,13 +32,15 @@ extra_rdoc_files: []
32
32
 
33
33
  files:
34
34
  - bin/rhcp_shell
35
- - lib/test.log
36
35
  - lib/shell_backend.rb
36
+ - lib/rhcp_shell.rb
37
37
  - lib/base_shell.rb
38
- - lib/version.rb
38
+ - lib/test.log
39
39
  - lib/rhcp_shell_backend.rb
40
- - test/setup_test_registry.rb
40
+ - lib/version.rb
41
+ - lib/rhcp_shell.log
41
42
  - test/rhcp_shell_backend_test.rb
43
+ - test/setup_test_registry.rb
42
44
  has_rdoc: true
43
45
  homepage: http://rubyforge.org/projects/rhcp
44
46
  post_install_message: