corl 0.4.0 → 0.4.1
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/Gemfile +5 -10
- data/Gemfile.lock +13 -11
- data/README.rdoc +1 -1
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/bin/corl +2 -54
- data/bootstrap/bootstrap.sh +91 -0
- data/bootstrap/lib/shell/LICENSE.txt +674 -0
- data/bootstrap/lib/shell/command.sh +214 -0
- data/bootstrap/lib/shell/filesystem.sh +139 -0
- data/bootstrap/lib/shell/load.sh +73 -0
- data/bootstrap/lib/shell/os.sh +67 -0
- data/bootstrap/lib/shell/script.sh +160 -0
- data/bootstrap/lib/shell/starter.sh +64 -0
- data/bootstrap/lib/shell/validators.sh +50 -0
- data/bootstrap/os/ubuntu/00_base.sh +5 -0
- data/bootstrap/os/ubuntu/01_git.sh +5 -0
- data/bootstrap/os/ubuntu/05_ruby.sh +7 -0
- data/bootstrap/os/ubuntu/06_puppet.sh +38 -0
- data/bootstrap/os/ubuntu/10_corl.sh +9 -0
- data/corl.gemspec +61 -124
- data/lib/{corl → CORL}/action/bootstrap.rb +13 -7
- data/lib/CORL/action/exec.rb +39 -0
- data/lib/CORL/action/image.rb +31 -0
- data/lib/{corl → CORL}/action/images.rb +1 -1
- data/lib/CORL/action/lookup.rb +34 -0
- data/lib/{corl → CORL}/action/machines.rb +1 -1
- data/lib/CORL/action/provision.rb +31 -0
- data/lib/{corl → CORL}/action/seed.rb +3 -3
- data/lib/{corl → CORL}/action/spawn.rb +2 -2
- data/lib/CORL/action/start.rb +31 -0
- data/lib/CORL/action/stop.rb +31 -0
- data/lib/{corl → CORL}/configuration/file.rb +1 -1
- data/lib/{corl → CORL}/event/puppet.rb +1 -1
- data/lib/{corl → CORL}/extension/puppetloader.rb +1 -1
- data/lib/{corl → CORL}/machine/fog.rb +3 -3
- data/lib/{corl → CORL}/machine/physical.rb +2 -2
- data/lib/{corl → CORL}/network/default.rb +1 -1
- data/lib/{corl → CORL}/node/aws.rb +0 -4
- data/lib/{corl → CORL}/node/google.rb +0 -4
- data/lib/{corl → CORL}/node/local.rb +1 -1
- data/lib/{corl → CORL}/node/rackspace.rb +0 -4
- data/lib/{corl → CORL}/provisioner/puppetnode.rb +1 -14
- data/lib/{corl → CORL}/provisioner/puppetnode/resource.rb +0 -0
- data/lib/{corl → CORL}/provisioner/puppetnode/resource_group.rb +0 -0
- data/lib/{corl → CORL}/template/environment.rb +1 -1
- data/lib/core/facade.rb +49 -0
- data/lib/{corl_core → core}/mixin/action/keypair.rb +10 -10
- data/lib/{corl_core → core}/mixin/lookup.rb +0 -0
- data/lib/{corl_core → core}/mod/hiera_backend.rb +0 -0
- data/lib/{corl_core/mixin/action/node.rb → core/plugin/action.rb} +66 -33
- data/lib/{corl_core → core}/plugin/configuration.rb +2 -2
- data/lib/{corl/node → core/plugin}/fog.rb +5 -1
- data/lib/{corl_core → core}/plugin/machine.rb +2 -2
- data/lib/{corl_core → core}/plugin/network.rb +4 -4
- data/lib/{corl_core → core}/plugin/node.rb +11 -9
- data/lib/{corl_core → core}/plugin/provisioner.rb +2 -2
- data/lib/{corl_core → core}/util/ssh.rb +1 -1
- data/lib/corl.rb +53 -112
- data/lib/puppet/parser/functions/ensure.rb +0 -4
- data/locales/en.yml +55 -148
- metadata +84 -222
- data/lib/corl/action/add.rb +0 -69
- data/lib/corl/action/clone.rb +0 -40
- data/lib/corl/action/create.rb +0 -55
- data/lib/corl/action/exec.rb +0 -41
- data/lib/corl/action/extract.rb +0 -49
- data/lib/corl/action/image.rb +0 -30
- data/lib/corl/action/lookup.rb +0 -35
- data/lib/corl/action/provision.rb +0 -37
- data/lib/corl/action/remove.rb +0 -51
- data/lib/corl/action/save.rb +0 -53
- data/lib/corl/action/start.rb +0 -37
- data/lib/corl/action/stop.rb +0 -30
- data/lib/corl/action/update.rb +0 -37
- data/lib/corl/command/shell.rb +0 -164
- data/lib/corl/event/regex.rb +0 -52
- data/lib/corl/project/git.rb +0 -465
- data/lib/corl/project/github.rb +0 -108
- data/lib/corl/template/json.rb +0 -16
- data/lib/corl/template/wrapper.rb +0 -16
- data/lib/corl/template/yaml.rb +0 -16
- data/lib/corl/translator/json.rb +0 -27
- data/lib/corl/translator/yaml.rb +0 -27
- data/lib/corl_core/codes.rb +0 -107
- data/lib/corl_core/config.rb +0 -337
- data/lib/corl_core/config/collection.rb +0 -57
- data/lib/corl_core/config/options.rb +0 -70
- data/lib/corl_core/core.rb +0 -59
- data/lib/corl_core/corl.rb +0 -254
- data/lib/corl_core/errors.rb +0 -84
- data/lib/corl_core/facade.rb +0 -126
- data/lib/corl_core/gems.rb +0 -72
- data/lib/corl_core/manager.rb +0 -425
- data/lib/corl_core/mixin/action/commit.rb +0 -58
- data/lib/corl_core/mixin/action/project.rb +0 -53
- data/lib/corl_core/mixin/action/push.rb +0 -52
- data/lib/corl_core/mixin/config/collection.rb +0 -53
- data/lib/corl_core/mixin/config/ops.rb +0 -53
- data/lib/corl_core/mixin/config/options.rb +0 -39
- data/lib/corl_core/mixin/macro/object_interface.rb +0 -361
- data/lib/corl_core/mixin/macro/plugin_interface.rb +0 -380
- data/lib/corl_core/mixin/settings.rb +0 -46
- data/lib/corl_core/mixin/sub_config.rb +0 -148
- data/lib/corl_core/mod/hash.rb +0 -29
- data/lib/corl_core/plugin/action.rb +0 -381
- data/lib/corl_core/plugin/base.rb +0 -374
- data/lib/corl_core/plugin/command.rb +0 -98
- data/lib/corl_core/plugin/event.rb +0 -53
- data/lib/corl_core/plugin/extension.rb +0 -12
- data/lib/corl_core/plugin/project.rb +0 -927
- data/lib/corl_core/plugin/template.rb +0 -80
- data/lib/corl_core/plugin/translator.rb +0 -38
- data/lib/corl_core/util/cli.rb +0 -352
- data/lib/corl_core/util/data.rb +0 -404
- data/lib/corl_core/util/disk.rb +0 -114
- data/lib/corl_core/util/git.rb +0 -47
- data/lib/corl_core/util/interface.rb +0 -319
- data/lib/corl_core/util/liquid.rb +0 -17
- data/lib/corl_core/util/package.rb +0 -93
- data/lib/corl_core/util/shell.rb +0 -239
- data/spec/corl_core/interface_spec.rb +0 -489
data/lib/corl_core/util/shell.rb
DELETED
@@ -1,239 +0,0 @@
|
|
1
|
-
|
2
|
-
module CORL
|
3
|
-
module Util
|
4
|
-
class Shell < Core
|
5
|
-
|
6
|
-
include Celluloid
|
7
|
-
|
8
|
-
#-----------------------------------------------------------------------------
|
9
|
-
|
10
|
-
@@supervisors = {}
|
11
|
-
|
12
|
-
#-----------------------------------------------------------------------------
|
13
|
-
# Execution result interface
|
14
|
-
|
15
|
-
class Result
|
16
|
-
attr_accessor :status
|
17
|
-
attr_reader :command
|
18
|
-
|
19
|
-
def initialize(command)
|
20
|
-
@command = command
|
21
|
-
@output = ''
|
22
|
-
@errors = ''
|
23
|
-
@status = CORL.code.success
|
24
|
-
end
|
25
|
-
|
26
|
-
#---
|
27
|
-
|
28
|
-
def output
|
29
|
-
@output.strip
|
30
|
-
end
|
31
|
-
|
32
|
-
def errors
|
33
|
-
@errors.strip
|
34
|
-
end
|
35
|
-
|
36
|
-
#---
|
37
|
-
|
38
|
-
def append_output(output_str)
|
39
|
-
@output << output_str
|
40
|
-
end
|
41
|
-
|
42
|
-
def append_errors(error_str)
|
43
|
-
@errors << error_str
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
#-----------------------------------------------------------------------------
|
48
|
-
# Shell interface
|
49
|
-
|
50
|
-
def self.connection(name = :core)
|
51
|
-
name = name.to_sym
|
52
|
-
init_shell(name) unless @@supervisors.has_key?(name)
|
53
|
-
@@supervisors[name]
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.init_shell(name)
|
57
|
-
name = name.to_sym
|
58
|
-
|
59
|
-
Shell.supervise_as name
|
60
|
-
@@supervisors[name] = Celluloid::Actor[name]
|
61
|
-
end
|
62
|
-
|
63
|
-
#---
|
64
|
-
|
65
|
-
execute_block_on_receiver :exec
|
66
|
-
|
67
|
-
def exec(command, options = {}, &code)
|
68
|
-
config = Config.ensure(options)
|
69
|
-
|
70
|
-
min = config.get(:min, 1).to_i
|
71
|
-
tries = config.get(:tries, min).to_i
|
72
|
-
tries = ( min > tries ? min : tries )
|
73
|
-
|
74
|
-
info_prefix = config.get(:info_prefix, '')
|
75
|
-
info_suffix = config.get(:info_suffix, '')
|
76
|
-
error_prefix = config.get(:error_prefix, '')
|
77
|
-
error_suffix = config.get(:error_suffix, '')
|
78
|
-
|
79
|
-
ui = config.get(:ui, CORL.ui)
|
80
|
-
|
81
|
-
conditions = CORL.events(config.get(:exit, {}), true)
|
82
|
-
|
83
|
-
$stdout.sync = true
|
84
|
-
$stderr.sync = true
|
85
|
-
|
86
|
-
system_result = Result.new(command)
|
87
|
-
|
88
|
-
for i in tries.downto(1)
|
89
|
-
logger.info(">> running: #{command}")
|
90
|
-
|
91
|
-
begin
|
92
|
-
t1, output_new, output_orig, output_reader = pipe_exec_stream($stdout, conditions, {
|
93
|
-
:prefix => info_prefix,
|
94
|
-
:suffix => info_suffix,
|
95
|
-
}, 'output') do |data|
|
96
|
-
system_result.append_output(data)
|
97
|
-
block_given? ? code.call(:output, command, data) : true
|
98
|
-
end
|
99
|
-
|
100
|
-
t2, error_new, error_orig, error_reader = pipe_exec_stream($stderr, conditions, {
|
101
|
-
:prefix => error_prefix,
|
102
|
-
:suffix => error_suffix,
|
103
|
-
}, 'error') do |data|
|
104
|
-
system_result.append_errors(data)
|
105
|
-
block_given? ? code.call(:error, command, data) : true
|
106
|
-
end
|
107
|
-
|
108
|
-
system_success = system(command)
|
109
|
-
system_result.status = $?.exitstatus
|
110
|
-
|
111
|
-
ensure
|
112
|
-
output_success = close_exec_pipe(t1, $stdout, output_orig, output_new, 'output')
|
113
|
-
error_success = close_exec_pipe(t2, $stderr, error_orig, error_new, 'error')
|
114
|
-
end
|
115
|
-
|
116
|
-
success = ( system_success && output_success && error_success )
|
117
|
-
|
118
|
-
min -= 1
|
119
|
-
break if success && min <= 0 && conditions.empty?
|
120
|
-
end
|
121
|
-
system_result
|
122
|
-
end
|
123
|
-
|
124
|
-
#---
|
125
|
-
|
126
|
-
def pipe_exec_stream(output, conditions, options, label, &code)
|
127
|
-
original = output.dup
|
128
|
-
read, write = IO.pipe
|
129
|
-
|
130
|
-
match_prefix = ( options[:match_prefix] ? options[:match_prefix] : 'EXIT' )
|
131
|
-
|
132
|
-
thread = process_stream(read, original, options, label) do |data|
|
133
|
-
check_conditions(data, conditions, match_prefix) do
|
134
|
-
block_given? ? code.call(data) : true
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
thread.abort_on_exception = false
|
139
|
-
|
140
|
-
output.reopen(write)
|
141
|
-
return thread, write, original, read
|
142
|
-
end
|
143
|
-
protected :pipe_exec_stream
|
144
|
-
|
145
|
-
#---
|
146
|
-
|
147
|
-
def close_exec_pipe(thread, output, original, write, label)
|
148
|
-
output.reopen(original)
|
149
|
-
|
150
|
-
write.close
|
151
|
-
success = thread.value
|
152
|
-
|
153
|
-
original.close
|
154
|
-
return success
|
155
|
-
end
|
156
|
-
protected :close_exec_pipe
|
157
|
-
|
158
|
-
#---
|
159
|
-
|
160
|
-
def check_conditions(data, conditions, match_prefix = '', &code)
|
161
|
-
prefix = ''
|
162
|
-
|
163
|
-
unless ! conditions || conditions.empty?
|
164
|
-
conditions.each do |key, event|
|
165
|
-
if event.check(data)
|
166
|
-
prefix = match_prefix
|
167
|
-
conditions.delete(key)
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
result = true
|
173
|
-
if block_given?
|
174
|
-
result = code.call
|
175
|
-
|
176
|
-
unless prefix.empty?
|
177
|
-
case result
|
178
|
-
when Hash
|
179
|
-
result[:prefix] = prefix
|
180
|
-
else
|
181
|
-
result = { :success => result, :prefix => prefix }
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
185
|
-
return result
|
186
|
-
end
|
187
|
-
protected :check_conditions
|
188
|
-
|
189
|
-
#---
|
190
|
-
|
191
|
-
def process_stream(input, output, options, label, &code)
|
192
|
-
return Thread.new do
|
193
|
-
success = true
|
194
|
-
default_prefix = ( options[:prefix] ? options[:prefix] : '' )
|
195
|
-
default_suffix = ( options[:suffix] ? options[:suffix] : '' )
|
196
|
-
|
197
|
-
begin
|
198
|
-
while ( data = input.readpartial(1024) )
|
199
|
-
message = data.strip
|
200
|
-
newline = ( data[-1,1].match(/\n/) ? true : false )
|
201
|
-
|
202
|
-
unless message.empty?
|
203
|
-
lines = message.split(/\n/)
|
204
|
-
lines.each_with_index do |line, index|
|
205
|
-
prefix = default_prefix
|
206
|
-
suffix = default_suffix
|
207
|
-
|
208
|
-
unless line.empty?
|
209
|
-
if block_given?
|
210
|
-
result = code.call(line)
|
211
|
-
|
212
|
-
if result && result.is_a?(Hash)
|
213
|
-
prefix = result[:prefix]
|
214
|
-
suffix = result[:suffix]
|
215
|
-
result = result[:success]
|
216
|
-
end
|
217
|
-
success = result if success
|
218
|
-
end
|
219
|
-
|
220
|
-
prefix = ( prefix && ! prefix.empty? ? "#{prefix}: " : '' )
|
221
|
-
suffix = ( suffix && ! suffix.empty? ? suffix : '' )
|
222
|
-
eol = ( index < lines.length - 1 || newline ? "\n" : ' ' )
|
223
|
-
|
224
|
-
output.write(prefix.lstrip + line + suffix.rstrip + eol)
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
rescue EOFError
|
230
|
-
end
|
231
|
-
|
232
|
-
input.close()
|
233
|
-
success
|
234
|
-
end
|
235
|
-
end
|
236
|
-
protected :process_stream
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
@@ -1,489 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
module CORL
|
5
|
-
|
6
|
-
describe Util::Interface do
|
7
|
-
|
8
|
-
#---------------------------------------------------------------------------
|
9
|
-
# UI functionality
|
10
|
-
|
11
|
-
describe "#say" do
|
12
|
-
|
13
|
-
#-------------------------------------------------------------------------
|
14
|
-
# Delegation
|
15
|
-
|
16
|
-
it "can delegate to another class that contains this method" do
|
17
|
-
output = double('output')
|
18
|
-
output.should_receive(:puts).with('message')
|
19
|
-
|
20
|
-
ui = Util::Interface.new({
|
21
|
-
:output => output,
|
22
|
-
:printer => :puts,
|
23
|
-
})
|
24
|
-
Util::Interface.new({ :ui_delegate => ui }).say(:info, 'message')
|
25
|
-
end
|
26
|
-
|
27
|
-
#-------------------------------------------------------------------------
|
28
|
-
# Output formats
|
29
|
-
|
30
|
-
it "prints a message with default options" do
|
31
|
-
output1 = double('output1')
|
32
|
-
output1.should_receive(:puts).with('message')
|
33
|
-
|
34
|
-
Util::Interface.new({ :output => output1 }).say(:info, 'message')
|
35
|
-
|
36
|
-
output2 = double('output2')
|
37
|
-
output2.should_receive(:puts).with('[component] message')
|
38
|
-
|
39
|
-
Util::Interface.new({
|
40
|
-
:resource => 'component',
|
41
|
-
:output => output2,
|
42
|
-
}).say(:info, 'message')
|
43
|
-
end
|
44
|
-
|
45
|
-
#---
|
46
|
-
|
47
|
-
it "prints a message with and without newlines included" do
|
48
|
-
output1 = double('output1')
|
49
|
-
output1.should_receive(:puts).with('message')
|
50
|
-
|
51
|
-
test = Util::Interface.new({ :output => output1 })
|
52
|
-
test.say(:info, 'message', { :new_line => true })
|
53
|
-
|
54
|
-
output2 = double('output2')
|
55
|
-
output2.should_receive(:print).with('message')
|
56
|
-
|
57
|
-
test = Util::Interface.new({ :output => output2 })
|
58
|
-
test.say(:info, 'message', { :new_line => false })
|
59
|
-
end
|
60
|
-
|
61
|
-
#---
|
62
|
-
|
63
|
-
it "routes message to output and error channels based on type given" do
|
64
|
-
[:info, :warn, :success].each do |type|
|
65
|
-
output = double('output')
|
66
|
-
output.should_receive(:puts).with('message')
|
67
|
-
|
68
|
-
Util::Interface.new({
|
69
|
-
:output => output,
|
70
|
-
:printer => :puts,
|
71
|
-
:color => false,
|
72
|
-
}).say(type, 'message')
|
73
|
-
end
|
74
|
-
|
75
|
-
error = double('error')
|
76
|
-
error.should_receive(:puts).with('message')
|
77
|
-
|
78
|
-
Util::Interface.new({
|
79
|
-
:error => error,
|
80
|
-
:printer => :puts,
|
81
|
-
:color => false,
|
82
|
-
}).say(:error, 'message')
|
83
|
-
end
|
84
|
-
|
85
|
-
#---
|
86
|
-
|
87
|
-
it "routes message to output and error channels based on channel given" do
|
88
|
-
[:info, :warn, :success].each do |type|
|
89
|
-
output = double('output')
|
90
|
-
output.should_receive(:puts).with('message')
|
91
|
-
|
92
|
-
Util::Interface.new({
|
93
|
-
:output => output,
|
94
|
-
:printer => :puts,
|
95
|
-
}).say(:info, 'message', { :channel => type })
|
96
|
-
end
|
97
|
-
|
98
|
-
error = double('error')
|
99
|
-
error.should_receive(:puts).with('message')
|
100
|
-
|
101
|
-
Util::Interface.new({
|
102
|
-
:error => error,
|
103
|
-
:printer => :puts,
|
104
|
-
:color => false,
|
105
|
-
}).say(:info, 'message', { :channel => :error })
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
#---
|
110
|
-
|
111
|
-
describe "#ask" do
|
112
|
-
|
113
|
-
#-------------------------------------------------------------------------
|
114
|
-
# Delegation
|
115
|
-
|
116
|
-
it "can delegate to another class that contains this method"
|
117
|
-
|
118
|
-
#-------------------------------------------------------------------------
|
119
|
-
# Input
|
120
|
-
|
121
|
-
it "displays a prompt and returns user feedback"
|
122
|
-
end
|
123
|
-
|
124
|
-
#---
|
125
|
-
|
126
|
-
describe "#info" do
|
127
|
-
|
128
|
-
#-------------------------------------------------------------------------
|
129
|
-
# Delegation
|
130
|
-
|
131
|
-
it "can delegate to another class that contains this method" do
|
132
|
-
output = double('output')
|
133
|
-
output.should_receive(:puts).with('message')
|
134
|
-
|
135
|
-
ui = Util::Interface.new({
|
136
|
-
:output => output,
|
137
|
-
:printer => :puts,
|
138
|
-
})
|
139
|
-
Util::Interface.new({ :ui_delegate => ui }).info('message')
|
140
|
-
end
|
141
|
-
|
142
|
-
#-------------------------------------------------------------------------
|
143
|
-
# Printing
|
144
|
-
|
145
|
-
it "prints an uncolored information message" do
|
146
|
-
output = double('output')
|
147
|
-
output.should_receive(:puts).with('message')
|
148
|
-
|
149
|
-
Util::Interface.new({
|
150
|
-
:output => output,
|
151
|
-
:printer => :puts,
|
152
|
-
}).info('message')
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
#---
|
157
|
-
|
158
|
-
describe "#warn" do
|
159
|
-
|
160
|
-
#-------------------------------------------------------------------------
|
161
|
-
# Delegation
|
162
|
-
|
163
|
-
it "can delegate to another class that contains this method" do
|
164
|
-
output = double('output')
|
165
|
-
output.should_receive(:puts).with('message')
|
166
|
-
|
167
|
-
ui = Util::Interface.new({
|
168
|
-
:output => output,
|
169
|
-
:printer => :puts,
|
170
|
-
:color => false,
|
171
|
-
})
|
172
|
-
Util::Interface.new({ :ui_delegate => ui }).warn('message')
|
173
|
-
end
|
174
|
-
|
175
|
-
#-------------------------------------------------------------------------
|
176
|
-
# Printing
|
177
|
-
|
178
|
-
it "prints an uncolored warning message" do
|
179
|
-
output = double('output')
|
180
|
-
output.should_receive(:puts).with('message')
|
181
|
-
|
182
|
-
Util::Interface.new({
|
183
|
-
:output => output,
|
184
|
-
:printer => :puts,
|
185
|
-
:color => false,
|
186
|
-
}).warn('message')
|
187
|
-
end
|
188
|
-
|
189
|
-
#---
|
190
|
-
|
191
|
-
it "prints a colored warning message" do
|
192
|
-
output = double('output')
|
193
|
-
output.should_receive(:print).with(/^\e\[33mmessage\e\[0m$/)
|
194
|
-
|
195
|
-
Util::Interface.new({
|
196
|
-
:output => output,
|
197
|
-
:color => true,
|
198
|
-
}).warn('message', { :new_line => false })
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
#---
|
203
|
-
|
204
|
-
describe "#error" do
|
205
|
-
|
206
|
-
#-------------------------------------------------------------------------
|
207
|
-
# Delegation
|
208
|
-
|
209
|
-
it "can delegate to another class that contains this method" do
|
210
|
-
error = double('error')
|
211
|
-
error.should_receive(:puts).with('message')
|
212
|
-
|
213
|
-
ui = Util::Interface.new({
|
214
|
-
:error => error,
|
215
|
-
:printer => :puts,
|
216
|
-
:color => false,
|
217
|
-
})
|
218
|
-
Util::Interface.new({ :ui_delegate => ui }).error('message')
|
219
|
-
end
|
220
|
-
|
221
|
-
#-------------------------------------------------------------------------
|
222
|
-
# Printing
|
223
|
-
|
224
|
-
it "prints an uncolored error message" do
|
225
|
-
error = double('error')
|
226
|
-
error.should_receive(:puts).with('message')
|
227
|
-
|
228
|
-
Util::Interface.new({
|
229
|
-
:error => error,
|
230
|
-
:printer => :puts,
|
231
|
-
:color => false,
|
232
|
-
}).error('message')
|
233
|
-
end
|
234
|
-
|
235
|
-
#---
|
236
|
-
|
237
|
-
it "prints a colored error message" do
|
238
|
-
error = double('error')
|
239
|
-
error.should_receive(:print).with(/^\e\[31mmessage\e\[0m$/)
|
240
|
-
|
241
|
-
Util::Interface.new({
|
242
|
-
:error => error,
|
243
|
-
:color => true,
|
244
|
-
}).error('message', { :new_line => false })
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
#---
|
249
|
-
|
250
|
-
describe "#success" do
|
251
|
-
|
252
|
-
#-------------------------------------------------------------------------
|
253
|
-
# Delegation
|
254
|
-
|
255
|
-
it "can delegate to another class that contains this method" do
|
256
|
-
output = double('output')
|
257
|
-
output.should_receive(:puts).with('message')
|
258
|
-
|
259
|
-
ui = Util::Interface.new({
|
260
|
-
:output => output,
|
261
|
-
:printer => :puts,
|
262
|
-
:color => false,
|
263
|
-
})
|
264
|
-
Util::Interface.new({ :ui_delegate => ui }).success('message')
|
265
|
-
end
|
266
|
-
|
267
|
-
#-------------------------------------------------------------------------
|
268
|
-
# Printing
|
269
|
-
|
270
|
-
it "prints an uncolored success message" do
|
271
|
-
output = double('output')
|
272
|
-
output.should_receive(:puts).with('message')
|
273
|
-
|
274
|
-
Util::Interface.new({
|
275
|
-
:output => output,
|
276
|
-
:printer => :puts,
|
277
|
-
:color => false,
|
278
|
-
}).success('message')
|
279
|
-
end
|
280
|
-
|
281
|
-
#---
|
282
|
-
|
283
|
-
it "prints a colored success message" do
|
284
|
-
output = double('output')
|
285
|
-
output.should_receive(:print).with(/^\e\[32mmessage\e\[0m$/)
|
286
|
-
|
287
|
-
Util::Interface.new({
|
288
|
-
:output => output,
|
289
|
-
:color => true,
|
290
|
-
}).success('message', { :new_line => false })
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
#---------------------------------------------------------------------------
|
295
|
-
# Utilities
|
296
|
-
|
297
|
-
describe "#format_message" do
|
298
|
-
|
299
|
-
#-------------------------------------------------------------------------
|
300
|
-
# Delegation
|
301
|
-
|
302
|
-
it "can delegate to another class that contains this method" do
|
303
|
-
message = Util::Interface.new({
|
304
|
-
:ui_delegate => Util::Interface.new('delegate')
|
305
|
-
}).format_message(:info, 'message', { :prefix => true })
|
306
|
-
|
307
|
-
message.should == '[delegate] message'
|
308
|
-
end
|
309
|
-
|
310
|
-
#-------------------------------------------------------------------------
|
311
|
-
# Prefix specifications
|
312
|
-
|
313
|
-
it "returns without a prefix because no resource" do
|
314
|
-
message = Util::Interface.new.format_message(:info, 'message', { :prefix => true })
|
315
|
-
message.should == 'message'
|
316
|
-
end
|
317
|
-
|
318
|
-
#---
|
319
|
-
|
320
|
-
it "returns without a prefix because prefix is false" do
|
321
|
-
message = Util::Interface.new('component').format_message(:info, 'message', { :prefix => false })
|
322
|
-
message.should == 'message'
|
323
|
-
end
|
324
|
-
|
325
|
-
#---
|
326
|
-
|
327
|
-
it "returns without a prefix because no prefix option given" do
|
328
|
-
message = Util::Interface.new('component').format_message(:info, 'message')
|
329
|
-
message.should == 'message'
|
330
|
-
end
|
331
|
-
|
332
|
-
#---
|
333
|
-
|
334
|
-
it "returns with a prefix if resource and prefix option given" do
|
335
|
-
message = Util::Interface.new('component').format_message(:info, 'message', { :prefix => true })
|
336
|
-
message.should == '[component] message'
|
337
|
-
end
|
338
|
-
|
339
|
-
#-------------------------------------------------------------------------
|
340
|
-
# Color specifications
|
341
|
-
|
342
|
-
it "formats a error message in red if color enabled" do
|
343
|
-
message = Util::Interface.new({
|
344
|
-
:resource => 'component',
|
345
|
-
:color => true,
|
346
|
-
}).format_message(:error, 'message')
|
347
|
-
message.should match(/^\e\[31mmessage\e\[0m$/)
|
348
|
-
end
|
349
|
-
|
350
|
-
#---
|
351
|
-
|
352
|
-
it "formats a warning message in yellow if color enabled" do
|
353
|
-
message = Util::Interface.new({
|
354
|
-
:resource => 'component',
|
355
|
-
:color => true,
|
356
|
-
}).format_message(:warn, 'message')
|
357
|
-
message.should match(/^\e\[33mmessage\e\[0m$/)
|
358
|
-
end
|
359
|
-
|
360
|
-
#---
|
361
|
-
|
362
|
-
it "formats a success message in green if color enabled" do
|
363
|
-
message = Util::Interface.new({
|
364
|
-
:resource => 'component',
|
365
|
-
:color => true,
|
366
|
-
}).format_message(:success, 'message')
|
367
|
-
message.should match(/^\e\[32mmessage\e\[0m$/)
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
#---------------------------------------------------------------------------
|
372
|
-
|
373
|
-
describe "#safe_puts" do
|
374
|
-
|
375
|
-
#-------------------------------------------------------------------------
|
376
|
-
# Delegation
|
377
|
-
|
378
|
-
it "can delegate to another class that contains this method" do
|
379
|
-
output = double('output')
|
380
|
-
output.should_receive(:puts).with('message')
|
381
|
-
|
382
|
-
ui = Util::Interface.new({
|
383
|
-
:output => output,
|
384
|
-
:printer => :puts,
|
385
|
-
})
|
386
|
-
Util::Interface.new({ :ui_delegate => ui }).safe_puts('message')
|
387
|
-
end
|
388
|
-
|
389
|
-
#-------------------------------------------------------------------------
|
390
|
-
# Instance configuration
|
391
|
-
|
392
|
-
it "prints an empty string unless message given" do
|
393
|
-
output = double('output')
|
394
|
-
output.should_receive(:puts).with('')
|
395
|
-
|
396
|
-
Util::Interface.new({
|
397
|
-
:output => output,
|
398
|
-
:printer => :puts,
|
399
|
-
}).safe_puts()
|
400
|
-
end
|
401
|
-
|
402
|
-
#---
|
403
|
-
|
404
|
-
it "prints to different output channels if they are given" do
|
405
|
-
output1 = double('output1')
|
406
|
-
output1.should_receive(:puts).with('message')
|
407
|
-
|
408
|
-
test = Util::Interface.new({
|
409
|
-
:output => output1,
|
410
|
-
:printer => :puts,
|
411
|
-
})
|
412
|
-
test.safe_puts('message')
|
413
|
-
|
414
|
-
output2 = double('output2')
|
415
|
-
output2.should_receive(:puts).with('message')
|
416
|
-
|
417
|
-
test.output = output2
|
418
|
-
test.safe_puts('message')
|
419
|
-
end
|
420
|
-
|
421
|
-
#---
|
422
|
-
|
423
|
-
it "prints with puts if puts printer option given" do
|
424
|
-
output = double('output')
|
425
|
-
output.should_receive(:puts).with('message')
|
426
|
-
|
427
|
-
Util::Interface.new({
|
428
|
-
:output => output,
|
429
|
-
:printer => :puts,
|
430
|
-
}).safe_puts('message')
|
431
|
-
end
|
432
|
-
|
433
|
-
#---
|
434
|
-
|
435
|
-
it "prints with print if print printer option given" do
|
436
|
-
output = double('output')
|
437
|
-
output.should_receive(:print).with('message')
|
438
|
-
|
439
|
-
Util::Interface.new({
|
440
|
-
:output => output,
|
441
|
-
:printer => :print,
|
442
|
-
}).safe_puts('message')
|
443
|
-
end
|
444
|
-
|
445
|
-
#-------------------------------------------------------------------------
|
446
|
-
# Method configuration
|
447
|
-
|
448
|
-
it "can override the instance output channel" do
|
449
|
-
output1 = double('output1')
|
450
|
-
output1.should_not_receive(:puts).with('message')
|
451
|
-
|
452
|
-
output2 = double('output2')
|
453
|
-
output2.should_receive(:puts).with('message')
|
454
|
-
|
455
|
-
Util::Interface.new({
|
456
|
-
:output => output1,
|
457
|
-
:printer => :puts,
|
458
|
-
}).safe_puts('message', { :channel => output2 })
|
459
|
-
end
|
460
|
-
|
461
|
-
#---
|
462
|
-
|
463
|
-
it "can override the instance printer handler" do
|
464
|
-
output = double('output')
|
465
|
-
output.should_not_receive(:puts).with('message')
|
466
|
-
output.should_receive(:print).with('message')
|
467
|
-
|
468
|
-
Util::Interface.new({
|
469
|
-
:output => output,
|
470
|
-
:printer => :puts,
|
471
|
-
}).safe_puts('message', { :printer => :print })
|
472
|
-
end
|
473
|
-
end
|
474
|
-
|
475
|
-
#---------------------------------------------------------------------------
|
476
|
-
|
477
|
-
describe "#check_delegate" do
|
478
|
-
|
479
|
-
it "returns false if no delegate exists" do
|
480
|
-
Util::Interface.new.check_delegate('safe_puts').should be_false
|
481
|
-
end
|
482
|
-
it "returns true if a delegate exists and it implements given method" do
|
483
|
-
test = Util::Interface.new({ :ui_delegate => Util::Interface.new })
|
484
|
-
test.check_delegate('safe_puts').should be_true
|
485
|
-
test.check_delegate('nonexistent').should be_false
|
486
|
-
end
|
487
|
-
end
|
488
|
-
end
|
489
|
-
end
|