linebook 0.2.0 → 0.7.0
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/History +32 -0
- data/License.txt +1 -1
- data/README +4 -9
- data/attributes/linebook/shell.rb +2 -0
- data/cookbook +9 -0
- data/lib/linebook/os/linux.rb +245 -0
- data/lib/linebook/os/posix.rb +390 -0
- data/lib/linebook/os/ubuntu.rb +23 -0
- data/lib/linebook/os/unix.rb +462 -0
- data/lib/linebook/shell/bash.rb +11 -0
- data/lib/linebook/shell.rb +147 -0
- data/lib/linebook/version.rb +1 -1
- metadata +32 -10
@@ -0,0 +1,390 @@
|
|
1
|
+
# Generated by Linecook
|
2
|
+
|
3
|
+
module Linebook
|
4
|
+
module Os
|
5
|
+
module Posix
|
6
|
+
# Returns true if the obj converts to a string which is whitespace or empty.
|
7
|
+
def blank?(obj)
|
8
|
+
# shortcut for nil...
|
9
|
+
obj.nil? || obj.to_s.strip.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
# Encloses the arg in quotes if the arg is not quoted and is quotable.
|
13
|
+
# Stringifies arg using to_s.
|
14
|
+
def quote(arg)
|
15
|
+
arg = arg.to_s
|
16
|
+
quoted?(arg) || !quote?(arg) ? arg : "\"#{arg}\""
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns true if the str is not an option (ie it begins with - or +), and is
|
20
|
+
# not already quoted (either by quotes or apostrophes). The intention is to
|
21
|
+
# check whether a string _should_ be quoted.
|
22
|
+
def quote?(str)
|
23
|
+
c = str[0]
|
24
|
+
c == ?- || c == ?+ || quoted?(str) ? false : true
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns true if the str is quoted (either by quotes or apostrophes).
|
28
|
+
def quoted?(str)
|
29
|
+
str =~ /\A".*"\z/ || str =~ /\A'.*'\z/ ? true : false
|
30
|
+
end
|
31
|
+
|
32
|
+
# Formats a command line command. Arguments are quoted. If the last arg is a
|
33
|
+
# hash, then it will be formatted into options using format_options and
|
34
|
+
# prepended to args.
|
35
|
+
def format_cmd(command, *args)
|
36
|
+
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
37
|
+
args.compact!
|
38
|
+
args.collect! {|arg| quote(arg) }
|
39
|
+
|
40
|
+
args = format_options(opts) + args
|
41
|
+
args.unshift(command)
|
42
|
+
args.join(' ')
|
43
|
+
end
|
44
|
+
|
45
|
+
# Formats a hash key-value string into command line options using the
|
46
|
+
# following heuristics:
|
47
|
+
#
|
48
|
+
# * Prepend '--' to mulit-char keys and '-' to single-char keys (unless they
|
49
|
+
# already start with '-').
|
50
|
+
# * For true values return the '--key'
|
51
|
+
# * For false/nil values return nothing
|
52
|
+
# * For all other values, quote (unless already quoted) and return '--key
|
53
|
+
# "value"'
|
54
|
+
#
|
55
|
+
# In addition, key formatting is performed on non-string keys (typically
|
56
|
+
# symbols) such that underscores are converted to dashes, ie :some_key =>
|
57
|
+
# 'some-key'. Note that options are sorted, such that short options appear
|
58
|
+
# after long options, and so should 'win' given typical option processing.
|
59
|
+
def format_options(opts)
|
60
|
+
options = []
|
61
|
+
|
62
|
+
opts.each do |(key, value)|
|
63
|
+
unless key.kind_of?(String)
|
64
|
+
key = key.to_s.gsub('_', '-')
|
65
|
+
end
|
66
|
+
|
67
|
+
unless key[0] == ?-
|
68
|
+
prefix = key.length == 1 ? '-' : '--'
|
69
|
+
key = "#{prefix}#{key}"
|
70
|
+
end
|
71
|
+
|
72
|
+
case value
|
73
|
+
when true
|
74
|
+
options << key
|
75
|
+
when false, nil
|
76
|
+
next
|
77
|
+
else
|
78
|
+
options << "#{key} #{quote(value.to_s)}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
options.sort
|
83
|
+
end
|
84
|
+
|
85
|
+
# An array of functions defined for self.
|
86
|
+
def functions
|
87
|
+
@functions ||= []
|
88
|
+
end
|
89
|
+
|
90
|
+
# Defines a function from the block. The block content is indented and
|
91
|
+
# cleaned up some to make a nice function definition. To avoid formatting,
|
92
|
+
# provide the body directly.
|
93
|
+
#
|
94
|
+
# A body and block given together raises an error. Raises an error if the
|
95
|
+
# function is already defined with a different body.
|
96
|
+
def function(name, body=nil)
|
97
|
+
if body && block_given?
|
98
|
+
raise "define functions with body or block"
|
99
|
+
end
|
100
|
+
|
101
|
+
if body.nil?
|
102
|
+
str = capture_str { indent { yield } }
|
103
|
+
body = "\n#{str.chomp("\n")}\n"
|
104
|
+
end
|
105
|
+
|
106
|
+
function = "#{name}() {#{body}}"
|
107
|
+
|
108
|
+
if current = functions.find {|func| func.index("#{name}()") == 0 }
|
109
|
+
if current != function
|
110
|
+
raise "function already defined: #{name.inspect}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
functions << function
|
115
|
+
writeln function
|
116
|
+
|
117
|
+
name
|
118
|
+
end
|
119
|
+
|
120
|
+
# Returns true if the function is defined.
|
121
|
+
def function?(name)
|
122
|
+
declaration = "#{name}()"
|
123
|
+
functions.any? {|func| func.index(declaration) == 0 }
|
124
|
+
end
|
125
|
+
|
126
|
+
CHECK_STATUS = /(\s*(?:\ncheck_status.*?\n\s*)?)\z/
|
127
|
+
|
128
|
+
# Adds a redirect to append stdout to a file.
|
129
|
+
def append(path=nil)
|
130
|
+
redirect(nil, path || '/dev/null', '>>')
|
131
|
+
chain_proxy
|
132
|
+
end
|
133
|
+
|
134
|
+
def _append(*args, &block) # :nodoc:
|
135
|
+
str = capture_str { append(*args, &block) }
|
136
|
+
str.strip!
|
137
|
+
str
|
138
|
+
end
|
139
|
+
|
140
|
+
# Adds a check that ensures the last exit status is as indicated. Note that no
|
141
|
+
# check will be added unless check_status_function is added beforehand.
|
142
|
+
def check_status(expect_status=0, fail_status='$?')
|
143
|
+
# <% if function?('check_status') %>
|
144
|
+
# check_status <%= expect_status %> $? <%= fail_status %> $LINENO
|
145
|
+
#
|
146
|
+
# <% end %>
|
147
|
+
if function?('check_status')
|
148
|
+
write "check_status "; write(( expect_status ).to_s); write " $? "; write(( fail_status ).to_s); write " $LINENO\n"
|
149
|
+
write "\n"
|
150
|
+
end
|
151
|
+
chain_proxy
|
152
|
+
end
|
153
|
+
|
154
|
+
def _check_status(*args, &block) # :nodoc:
|
155
|
+
str = capture_str { check_status(*args, &block) }
|
156
|
+
str.strip!
|
157
|
+
str
|
158
|
+
end
|
159
|
+
|
160
|
+
# Adds the check status function.
|
161
|
+
def check_status_function()
|
162
|
+
function 'check_status', ' if [ $2 -ne $1 ]; then echo "[$2] $0:${4:-?}"; exit $3; else return $2; fi '
|
163
|
+
chain_proxy
|
164
|
+
end
|
165
|
+
|
166
|
+
def _check_status_function(*args, &block) # :nodoc:
|
167
|
+
str = capture_str { check_status_function(*args, &block) }
|
168
|
+
str.strip!
|
169
|
+
str
|
170
|
+
end
|
171
|
+
|
172
|
+
# Writes a comment.
|
173
|
+
def comment(str)
|
174
|
+
# # <%= str %>
|
175
|
+
#
|
176
|
+
write "# "; write(( str ).to_s); write "\n"
|
177
|
+
|
178
|
+
chain_proxy
|
179
|
+
end
|
180
|
+
|
181
|
+
def _comment(*args, &block) # :nodoc:
|
182
|
+
str = capture_str { comment(*args, &block) }
|
183
|
+
str.strip!
|
184
|
+
str
|
185
|
+
end
|
186
|
+
|
187
|
+
# Executes a command and checks the output status. Quotes all non-option args
|
188
|
+
# that aren't already quoted. Accepts a trailing hash which will be transformed
|
189
|
+
# into command line options.
|
190
|
+
def execute(command, *args)
|
191
|
+
if chain?
|
192
|
+
rewrite(CHECK_STATUS)
|
193
|
+
write ' | '
|
194
|
+
end
|
195
|
+
# <%= format_cmd(command, *args) %>
|
196
|
+
#
|
197
|
+
# <% check_status %>
|
198
|
+
write(( format_cmd(command, *args) ).to_s)
|
199
|
+
write "\n"
|
200
|
+
check_status
|
201
|
+
chain_proxy
|
202
|
+
end
|
203
|
+
|
204
|
+
def _execute(*args, &block) # :nodoc:
|
205
|
+
str = capture_str { execute(*args, &block) }
|
206
|
+
str.strip!
|
207
|
+
str
|
208
|
+
end
|
209
|
+
|
210
|
+
# Exports a variable.
|
211
|
+
def export(key, value)
|
212
|
+
# export <%= key %>=<%= quote(value) %>
|
213
|
+
#
|
214
|
+
write "export "; write(( key ).to_s); write "="; write(( quote(value) ).to_s); write "\n"
|
215
|
+
|
216
|
+
chain_proxy
|
217
|
+
end
|
218
|
+
|
219
|
+
def _export(*args, &block) # :nodoc:
|
220
|
+
str = capture_str { export(*args, &block) }
|
221
|
+
str.strip!
|
222
|
+
str
|
223
|
+
end
|
224
|
+
|
225
|
+
# Assigns stdin to the file.
|
226
|
+
def from(path)
|
227
|
+
redirect(nil, path, '<')
|
228
|
+
chain_proxy
|
229
|
+
end
|
230
|
+
|
231
|
+
def _from(*args, &block) # :nodoc:
|
232
|
+
str = capture_str { from(*args, &block) }
|
233
|
+
str.strip!
|
234
|
+
str
|
235
|
+
end
|
236
|
+
|
237
|
+
# Makes a heredoc statement surrounding the contents of the block. Options:
|
238
|
+
#
|
239
|
+
# delimiter the delimiter used, by default HEREDOC_n where n increments
|
240
|
+
# outdent add '-' before the delimiter
|
241
|
+
# quote quotes the delimiter
|
242
|
+
def heredoc(options={})
|
243
|
+
tail = chain? ? rewrite(CHECK_STATUS) {|m| write ' '; m[1].lstrip } : nil
|
244
|
+
|
245
|
+
unless options.kind_of?(Hash)
|
246
|
+
options = {:delimiter => options}
|
247
|
+
end
|
248
|
+
|
249
|
+
delimiter = options[:delimiter] || begin
|
250
|
+
@heredoc_count ||= -1
|
251
|
+
"HEREDOC_#{@heredoc_count += 1}"
|
252
|
+
end
|
253
|
+
# <<<%= options[:outdent] ? '-' : ' '%><%= options[:quote] ? "\"#{delimiter}\"" : delimiter %><% outdent(" # :#{delimiter}:") do %>
|
254
|
+
# <% yield %>
|
255
|
+
# <%= delimiter %><% end %>
|
256
|
+
#
|
257
|
+
# <%= tail %>
|
258
|
+
write "<<"; write(( options[:outdent] ? '-' : ' ').to_s); write(( options[:quote] ? "\"#{delimiter}\"" : delimiter ).to_s); outdent(" # :#{delimiter}:") do ; write "\n"
|
259
|
+
yield
|
260
|
+
write(( delimiter ).to_s); end
|
261
|
+
write "\n"
|
262
|
+
write(( tail ).to_s)
|
263
|
+
chain_proxy
|
264
|
+
end
|
265
|
+
|
266
|
+
def _heredoc(*args, &block) # :nodoc:
|
267
|
+
str = capture_str { heredoc(*args, &block) }
|
268
|
+
str.strip!
|
269
|
+
str
|
270
|
+
end
|
271
|
+
|
272
|
+
# Executes the block when the expression evaluates to zero.
|
273
|
+
def if_(expression)
|
274
|
+
# if <%= expression %>
|
275
|
+
# then
|
276
|
+
# <% indent { yield } %>
|
277
|
+
# fi
|
278
|
+
#
|
279
|
+
#
|
280
|
+
write "if "; write(( expression ).to_s); write "\n"
|
281
|
+
write "then\n"
|
282
|
+
indent { yield }
|
283
|
+
write "fi\n"
|
284
|
+
write "\n"
|
285
|
+
|
286
|
+
chain_proxy
|
287
|
+
end
|
288
|
+
|
289
|
+
def _if_(*args, &block) # :nodoc:
|
290
|
+
str = capture_str { if_(*args, &block) }
|
291
|
+
str.strip!
|
292
|
+
str
|
293
|
+
end
|
294
|
+
|
295
|
+
# Makes a redirect statement.
|
296
|
+
def redirect(source, target, redirection='>')
|
297
|
+
source = source.nil? || source.kind_of?(Fixnum) ? source : "#{source} "
|
298
|
+
target = target.nil? || target.kind_of?(Fixnum) ? "&#{target}" : " #{target}"
|
299
|
+
|
300
|
+
match = chain? ? rewrite(CHECK_STATUS) : nil
|
301
|
+
write " #{source}#{redirection}#{target}"
|
302
|
+
write match[1] if match
|
303
|
+
chain_proxy
|
304
|
+
end
|
305
|
+
|
306
|
+
def _redirect(*args, &block) # :nodoc:
|
307
|
+
str = capture_str { redirect(*args, &block) }
|
308
|
+
str.strip!
|
309
|
+
str
|
310
|
+
end
|
311
|
+
|
312
|
+
# Sets the options to on (true) or off (false) as specified.
|
313
|
+
def set(options)
|
314
|
+
# <% options.keys.sort_by {|opt| opt.to_s }.each do |opt| %>
|
315
|
+
# set <%= options[opt] ? '-' : '+' %>o <%= opt %>
|
316
|
+
# <% end %>
|
317
|
+
#
|
318
|
+
options.keys.sort_by {|opt| opt.to_s }.each do |opt|
|
319
|
+
write "set "; write(( options[opt] ? '-' : '+' ).to_s); write "o "; write(( opt ).to_s); write "\n"
|
320
|
+
end
|
321
|
+
|
322
|
+
chain_proxy
|
323
|
+
end
|
324
|
+
|
325
|
+
def _set(*args, &block) # :nodoc:
|
326
|
+
str = capture_str { set(*args, &block) }
|
327
|
+
str.strip!
|
328
|
+
str
|
329
|
+
end
|
330
|
+
|
331
|
+
# Adds a redirect of stdout to a file.
|
332
|
+
def to(path=nil)
|
333
|
+
redirect(nil, path || '/dev/null')
|
334
|
+
chain_proxy
|
335
|
+
end
|
336
|
+
|
337
|
+
def _to(*args, &block) # :nodoc:
|
338
|
+
str = capture_str { to(*args, &block) }
|
339
|
+
str.strip!
|
340
|
+
str
|
341
|
+
end
|
342
|
+
|
343
|
+
# Executes the block when the expression evaluates to a non-zero value.
|
344
|
+
def unless_(expression)
|
345
|
+
if_("! #{expression}") { yield }
|
346
|
+
chain_proxy
|
347
|
+
end
|
348
|
+
|
349
|
+
def _unless_(*args, &block) # :nodoc:
|
350
|
+
str = capture_str { unless_(*args, &block) }
|
351
|
+
str.strip!
|
352
|
+
str
|
353
|
+
end
|
354
|
+
|
355
|
+
# Unsets a list of variables.
|
356
|
+
def unset(*keys)
|
357
|
+
# <% keys.each do |key| %>
|
358
|
+
# unset <%= key %>
|
359
|
+
# <% end %>
|
360
|
+
keys.each do |key|
|
361
|
+
write "unset "; write(( key ).to_s); write "\n"
|
362
|
+
end
|
363
|
+
chain_proxy
|
364
|
+
end
|
365
|
+
|
366
|
+
def _unset(*args, &block) # :nodoc:
|
367
|
+
str = capture_str { unset(*args, &block) }
|
368
|
+
str.strip!
|
369
|
+
str
|
370
|
+
end
|
371
|
+
|
372
|
+
# Set a variable.
|
373
|
+
def variable(key, value)
|
374
|
+
# <%= key %>=<%= quote(value) %>
|
375
|
+
#
|
376
|
+
#
|
377
|
+
write(( key ).to_s); write "="; write(( quote(value) ).to_s)
|
378
|
+
write "\n"
|
379
|
+
|
380
|
+
chain_proxy
|
381
|
+
end
|
382
|
+
|
383
|
+
def _variable(*args, &block) # :nodoc:
|
384
|
+
str = capture_str { variable(*args, &block) }
|
385
|
+
str.strip!
|
386
|
+
str
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Generated by Linecook
|
2
|
+
|
3
|
+
module Linebook
|
4
|
+
module Os
|
5
|
+
module Ubuntu
|
6
|
+
require 'linebook/os/linux'
|
7
|
+
include Linux
|
8
|
+
|
9
|
+
# Installs a package using apt-get.
|
10
|
+
def package(name, version=nil, options={:q => true, :y => true})
|
11
|
+
name = "#{name}=#{version}" unless blank?(version)
|
12
|
+
execute "apt-get install", name, options
|
13
|
+
chain_proxy
|
14
|
+
end
|
15
|
+
|
16
|
+
def _package(*args, &block) # :nodoc:
|
17
|
+
str = capture_str { package(*args, &block) }
|
18
|
+
str.strip!
|
19
|
+
str
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|