rct 0.6 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rct CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
3
+ # Copyright 2012-2015 Jyri J. Virkki <jyri@virkki.com>
4
4
  #
5
5
  # This file is part of rct.
6
6
  #
@@ -23,7 +23,7 @@
23
23
  # rct main
24
24
  #
25
25
  # Runs either a CLI invocation or a test suite depending on arguments.
26
- #
26
+ #
27
27
 
28
28
  require 'rct'
29
29
  require 'rct/rct_init'
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012-2014 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2015 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -19,18 +19,24 @@
19
19
 
20
20
 
21
21
  #-----------------------------------------------------------------------------
22
- # Assorted utility methods of general interest. TODO: cleanup
22
+ # Assorted utility methods of general interest.
23
23
  #
24
24
  class RCT
25
25
 
26
26
  @log_level = RESULT
27
27
 
28
28
 
29
+ #---------------------------------------------------------------------------
30
+ # Return current log level.
31
+ #
29
32
  def self.log_level
30
33
  @log_level
31
34
  end
32
35
 
33
36
 
37
+ #---------------------------------------------------------------------------
38
+ # Set log level (RESULT, INFO, DEBUG; from constants.rb).
39
+ #
34
40
  def self.set_log_level(level)
35
41
  @log_level = RESULT if level == RESULT
36
42
  @log_level = INFO if level == INFO
@@ -38,11 +44,17 @@ class RCT
38
44
  end
39
45
 
40
46
 
47
+ #---------------------------------------------------------------------------
48
+ # Increase log verbosity level by one.
49
+ #
41
50
  def self.increase_log_level
42
51
  @log_level += 1
43
52
  end
44
53
 
45
54
 
55
+ #---------------------------------------------------------------------------
56
+ # Log a line of output, if appropriate for current log level.
57
+ #
46
58
  def self.log(level, line)
47
59
  if (level <= @log_level)
48
60
  puts line
@@ -50,23 +62,36 @@ class RCT
50
62
  end
51
63
 
52
64
 
65
+ #---------------------------------------------------------------------------
66
+ # Log an error to stdout (prefixed with 'error:')
67
+ #
53
68
  def self.error(msg)
54
69
  puts "error: #{msg}"
55
70
  end
56
71
 
57
72
 
73
+ #---------------------------------------------------------------------------
74
+ # Log an error to stdout (prefixed with 'error:') and exit.
75
+ #
58
76
  def self.bad_invocation(msg)
59
77
  error(msg)
60
78
  exit(1)
61
79
  end
62
80
 
63
81
 
82
+ #---------------------------------------------------------------------------
83
+ # Log an error to stdout (prefixed with 'error:') and exit.
84
+ #
64
85
  def self.die(msg)
65
86
  error(msg)
66
87
  exit(1)
67
88
  end
68
89
 
69
90
 
91
+ #---------------------------------------------------------------------------
92
+ # Return the value of the flag at position pos in ARGV.
93
+ # Removes both the value and the flag from ARGV.
94
+ #
70
95
  def self.argv_get(pos)
71
96
  value = ARGV[pos + 1]
72
97
  ARGV.delete_at(pos + 1)
@@ -75,6 +100,9 @@ class RCT
75
100
  end
76
101
 
77
102
 
103
+ #---------------------------------------------------------------------------
104
+ #
105
+ #
78
106
  def self.help
79
107
 
80
108
  # default is CLI invocation of method name
@@ -160,21 +188,33 @@ class RCT
160
188
  end
161
189
 
162
190
 
191
+ #---------------------------------------------------------------------------
192
+ # Set a key,value pair in the global state.
193
+ #
163
194
  def self.sset(key, value, temp=false)
164
195
  $STATE.set(key, value, temp)
165
196
  end
166
197
 
167
198
 
199
+ #---------------------------------------------------------------------------
200
+ # Set a temporary key,value pair in the global state.
201
+ #
168
202
  def self.ssettmp(key, value)
169
203
  $STATE.set(key, value, true)
170
204
  end
171
205
 
172
206
 
207
+ #---------------------------------------------------------------------------
208
+ # Get the value of a key from the global state.
209
+ #
173
210
  def self.sget(key)
174
211
  $STATE.get(key)
175
212
  end
176
213
 
177
214
 
215
+ #---------------------------------------------------------------------------
216
+ # Remove the value of a key from the global state.
217
+ #
178
218
  def self.sdelete(key)
179
219
  $STATE.delete(key)
180
220
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012-2014 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2015 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -17,7 +17,7 @@
17
17
  # along with rct. If not, see <http://www.gnu.org/licenses/>.
18
18
  #
19
19
 
20
- RCT_VERSION = '0.6'
20
+ RCT_VERSION = '0.7'
21
21
 
22
22
  RCT_MODE = '_mode'
23
23
  RCT_MODE_CLI = 'cli'
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012-2013 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2015 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -32,6 +32,16 @@
32
32
  class RCTCLI
33
33
 
34
34
 
35
+ #----------------------------------------------------------------------------
36
+ # CLI entry point.
37
+ #
38
+ # ARGV[0] must contain string "Class.operation". We'll instantiate 'Class'
39
+ # and execute CLI operation 'operation'. This operation must be one described
40
+ # by the 'cli' inspection method.
41
+ #
42
+ # The exception is if 'operation' is 'HELP', in which case auto-generated
43
+ # help output is shown.
44
+ #
35
45
  def self.rct_cli
36
46
  method = ARGV.shift
37
47
  if (method == nil || method.empty?)
@@ -44,18 +54,33 @@ class RCTCLI
44
54
  RCT.log(DEBUG, "Requested [#{method}]")
45
55
  RCT.log(DEBUG, "CLI class: #{class_name}, method: #{method_name}")
46
56
 
57
+ if (!class_name)
58
+ RCT.error("No class specified for CLI operation")
59
+ exit(1)
60
+ end
61
+
62
+ if (!method_name)
63
+ RCT.error("No method specified for CLI operation")
64
+ exit(1)
65
+ end
66
+
47
67
  obj = Object::const_get(class_name).new()
48
68
  begin
49
69
  cli_info = obj.send('cli')
50
70
  rescue Exception => e
51
71
  puts e
52
- error("#{class_name} does not support CLI operations")
72
+ RCT.error("#{class_name} does not support CLI operations")
53
73
  exit(1)
54
74
  end
55
75
 
76
+ if (method_name == "HELP")
77
+ description = obj.send('description')
78
+ show_help(description, cli_info)
79
+ end
80
+
56
81
  method_info = cli_info[method_name]
57
82
  if (method_info == nil)
58
- error("#{class_name}.#{method_name} not available")
83
+ RCT.error("#{class_name}.#{method_name} not available")
59
84
  exit(1)
60
85
  end
61
86
 
@@ -77,4 +102,44 @@ class RCTCLI
77
102
  end
78
103
 
79
104
 
105
+ #----------------------------------------------------------------------------
106
+ # Show autogenerated help based on cli_info
107
+ #
108
+ def self.show_help(description, cli_info)
109
+
110
+ puts
111
+ if (description != nil)
112
+ puts description
113
+ puts
114
+ end
115
+
116
+ cli_info.each_key { |key|
117
+
118
+ method_info = cli_info[key]
119
+ description = method_info['description']
120
+ req = method_info['required']
121
+ opt = method_info['optional']
122
+
123
+ puts
124
+ puts "#{key}: #{description}"
125
+
126
+ if (req != nil && req.length > 0)
127
+ puts " Required arguments:"
128
+ req.each { |key,arg|
129
+ puts " #{arg[0]} (#{arg[1]}) : #{arg[2]}"
130
+ }
131
+ end
132
+
133
+ if (opt != nil && opt.length > 0)
134
+ puts " Optional arguments:"
135
+ opt.each { |key,arg|
136
+ puts " #{arg[0]} (#{arg[1]}) : #{arg[2]}"
137
+ }
138
+ end
139
+ }
140
+
141
+ exit(0)
142
+ end
143
+
144
+
80
145
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2012-2014 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2012-2015 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -156,6 +156,12 @@ class RCTHTTP
156
156
  end
157
157
 
158
158
  response = Response.new(res)
159
+
160
+ status = response.status
161
+ if (status == 401)
162
+ response.add_error("Unauthorized")
163
+ end
164
+
159
165
  show_response(response)
160
166
  return response
161
167
  end
@@ -57,7 +57,7 @@ class Response
57
57
  @fail_msg = "#{@fail_msg}; #{msg}"
58
58
  end
59
59
  end
60
-
60
+
61
61
 
62
62
  #----------------------------------------------------------------------------
63
63
  # Returns true unless this response has been flagged as an error via
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2013 Jyri J. Virkki <jyri@virkki.com>
2
+ # Copyright 2013-2015 Jyri J. Virkki <jyri@virkki.com>
3
3
  #
4
4
  # This file is part of rct.
5
5
  #
@@ -18,7 +18,33 @@
18
18
  #
19
19
 
20
20
 
21
+ #-----------------------------------------------------------------------------
21
22
  # CLI scripts built on top of rct can require this file to initialize rct.
23
+ #
24
+ # The common use case for this is when implementing convenience
25
+ # scripts to call the CLI functionality of an rct module (which
26
+ # supports CLI operation). For an example see 'jira' script installed
27
+ # by rct_jira gem.
28
+ #
29
+ # If global $RCT_CLI_APP_CLASS has been set, it is used as the CLI
30
+ # class name to run. This allows convenience script to set their class
31
+ # name so the user can provide only the CLI operation name. If not
32
+ # set, ARGV from caller is not modified. See below.
33
+ #
34
+
22
35
 
23
36
  require 'rct'
24
37
  require 'rct/rct_init'
38
+
39
+
40
+ if ($RCT_CLI_APP_CLASS != nil)
41
+ if (ARGV.length == 0)
42
+ ARGV.insert(0, "#{$RCT_CLI_APP_CLASS}.HELP")
43
+ else
44
+ method = ARGV.shift
45
+ ARGV.insert(0, "#{$RCT_CLI_APP_CLASS}.#{method}")
46
+ end
47
+ end
48
+
49
+ RCT.parse_global_options()
50
+ RCTCLI.rct_cli()
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rct
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.7'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-17 00:00:00.000000000 Z
12
+ date: 2015-02-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: wip
15
15
  email: jyri@virkki.com