lint utils

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2015-09-10 03:39:43 +02:00
parent 753e7775ef
commit f1cc7cbf9b
2 changed files with 34 additions and 32 deletions

View file

@ -3,7 +3,6 @@
# license: All rights reserved # license: All rights reserved
class FindFiles class FindFiles
TYPES = { TYPES = {
block: 'b', block: 'b',
character: 'c', character: 'c',
@ -12,7 +11,7 @@ class FindFiles
file: 'f', file: 'f',
link: 'l', link: 'l',
socket: 's', socket: 's',
door: 'D' door: 'D',
} }
attr_reader :error, :files attr_reader :error, :files
@ -27,7 +26,8 @@ class FindFiles
@result = Specinfra::Runner.run_command(cmd) @result = Specinfra::Runner.run_command(cmd)
exit_status = @result.exit_status.to_i exit_status = @result.exit_status.to_i
if exit_status == 0 if exit_status == 0
@files = @result.stdout.split("\n").map{ |x| x.strip }.find_all { |x| !x.empty? } @files = @result.stdout.split("\n").
map(&:strip).find_all { |x| !x.empty? }
else else
@files = [] @files = []
end end

View file

@ -28,45 +28,42 @@ class SimpleConfig
start_idx = 2 start_idx = 2
i = 0 i = 0
count = values - 1 count = values - 1
if (values == 1) return match[start_idx] if (values == 1)
return match[start_idx]
else # iterate over expected parameters
# iterate over expected parameters values = []
values = [] loop do
begin values.push(match[start_idx + i])
values.push(match[start_idx + i]) i += 1
i += 1 break if i > count
end until i > count
return values
end end
values
end end
def parse_rest(rest, opts) def parse_comment_line(rest, opts)
idx_nl = rest.index("\n") idx_nl = rest.index("\n")
idx_comment = rest.index(opts[:comment_char]) idx_comment = rest.index(opts[:comment_char])
idx_nl = rest.length if idx_nl.nil? idx_nl = rest.length if idx_nl.nil?
idx_comment = idx_nl + 1 if idx_comment.nil? idx_comment = idx_nl + 1 if idx_comment.nil?
line = ''
# is a comment inside this line # is a comment inside this line
if idx_comment < idx_nl if idx_comment < idx_nl && idx_comment != 0
if idx_comment == 0 line = rest[0..(idx_comment - 1)]
line = '' # in case we don't allow comments at the end
else # of an assignment/statement, ignore it and fall
line = rest[0..(idx_comment - 1)] # back to treating this as a regular line
# in case we don't allow comments at the end if opts[:standalone_comments] && !is_empty_line(line)
# of an assignment/statement, ignore it and fall
# back to treating this as a regular line
if opts[:standalone_comments] && !is_empty_line(line)
line = rest[0..(idx_nl - 1)]
end
end
# if there is no comment in this line
else
if idx_nl == 0
line = ''
else
line = rest[0..(idx_nl - 1)] line = rest[0..(idx_nl - 1)]
end end
# if there is no comment in this line
elsif idx_comment > idx_nl && idx_nl != 0
line = rest[0..(idx_nl - 1)]
end end
[line, idx_nl]
end
def parse_line_params(line, opts)
# now line contains what we are interested in parsing # now line contains what we are interested in parsing
# check if it is an assignment # check if it is an assignment
m = opts[:assignment_re].match(line) m = opts[:assignment_re].match(line)
@ -84,6 +81,11 @@ class SimpleConfig
@params[line.strip] = '' @params[line.strip] = ''
end end
end end
end
def parse_rest(rest, opts)
line, idx_nl = parse_comment_line(rest, opts)
parse_line_params(line, opts)
# return whatever is left # return whatever is left
rest[(idx_nl + 1)..-1] || '' rest[(idx_nl + 1)..-1] || ''
@ -101,7 +103,7 @@ class SimpleConfig
assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/, assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
key_vals: 1, # default for key=value, may require for 'key val1 val2 val3' key_vals: 1, # default for key=value, may require for 'key val1 val2 val3'
standalone_comments: false, standalone_comments: false,
multiple_values: false multiple_values: false,
} }
end end
end end