Rework apt.rb's read_debs.

This goes a step further and relaxes the parsing quite a bit while
making it more readable and flexible.

Signed-off-by: Ryan Davis <zenspider@chef.io>
This commit is contained in:
Ryan Davis 2019-08-29 16:20:20 -07:00
parent bb41b4fa8c
commit 1aebd53314

View file

@ -71,9 +71,9 @@ module Inspec::Resources
read_debs.select { |repo| repo[:url] == @deb_url && repo[:type] == "deb" }
end
# TODO: remove this. just see if it is valid w/ URI.parse
HTTP_URL_RE = /\A#{URI::DEFAULT_PARSER.make_regexp(%w{http https})}\z/.freeze
# read
def read_debs
return @repo_cache if defined?(@repo_cache)
@ -88,23 +88,26 @@ module Inspec::Resources
line = raw_line.gsub(/^(#\s*)*/, "")
active = false if raw_line != line
# eg.: deb http://archive.ubuntu.com/ubuntu/ wily main restricted
# or : deb [trusted=yes] http://archive.ubuntu.com/ubuntu/ wily main restricted
parse_repo = /^\s*(\S+)\s+(?:\[\S+\])?\s*"?([^ "\t\r\n\f]+)"?\s+(\S+)\s+(.*)$/.match(line)
# formats:
# deb http://archive.ubuntu.com/ubuntu/ wily main restricted ...
# deb [trusted=yes] http://archive.ubuntu.com/ubuntu/ wily main restricted ...
# check if we got any result and the second param is an url
next if parse_repo.nil?
next if parse_repo[2] && parse_repo[2] !~ HTTP_URL_RE
words = line.split
words.delete 1 if words[1] && words[1].start_with?("[")
type, url, distro, *components = words
next if components.empty?
next unless URI::HTTP === URI.parse(url)
next unless %w{deb deb-src}.include? type
# map data
repo = {
type: parse_repo[1],
url: parse_repo[2],
distro: parse_repo[3],
components: parse_repo[4].chomp.split(" "),
type: type,
url: url,
distro: distro,
components: components,
active: active,
}
next unless %w{deb deb-src}.include? repo[:type]
lines.push(repo)
end