Class: Fetchers::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchers/url.rb

Direct Known Subclasses

InspecPlugins::Compliance::Fetcher

Constant Summary collapse

MIME_TYPES =
{
  'application/x-zip-compressed' => '.zip',
  'application/zip' => '.zip',
  'application/x-gzip' => '.tar.gz',
  'application/gzip' => '.tar.gz',
}.freeze
GITHUB_URL_REGEX =
%r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}
GITHUB_URL_WITH_TREE_REGEX =
%r{^https?://(www\.)?github\.com/(?<user>[\w-]+)/(?<repo>[\w-]+)/tree/(?<commit>[\w\.]+)(/)?$}
BITBUCKET_URL_REGEX =
%r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)(\.git)?(/)?$}
BITBUCKET_URL_BRANCH_REGEX =
%r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)/branch/(?<branch>[\w\.]+)(/)?$}
BITBUCKET_URL_COMMIT_REGEX =
%r{^https?://(www\.)?bitbucket\.org/(?<user>[\w-]+)/(?<repo>[\w-]+)/commits/(?<commit>[\w\.]+)(/)?$}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts) ⇒ Url

Returns a new instance of Url.



96
97
98
99
100
101
102
103
104
# File 'lib/fetchers/url.rb', line 96

def initialize(url, opts)
  @target = url
  @target_uri = parse_uri(@target)
  @insecure = opts['insecure']
  @token = opts['token']
  @config = opts
  @archive_path = nil
  @temp_archive_path = nil
end

Instance Attribute Details

#archive_pathObject (readonly)

Returns the value of attribute archive_path.



94
95
96
# File 'lib/fetchers/url.rb', line 94

def archive_path
  @archive_path
end

#filesObject (readonly)

Returns the value of attribute files.



94
95
96
# File 'lib/fetchers/url.rb', line 94

def files
  @files
end

Class Method Details

.resolve(target, opts = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/fetchers/url.rb', line 22

def self.resolve(target, opts = {})
  if target.is_a?(Hash) && target.key?(:url)
    resolve_from_string(target[:url], opts, target[:username], target[:password])
  elsif target.is_a?(String)
    resolve_from_string(target, opts)
  end
end

.resolve_from_string(target, opts, username = nil, password = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fetchers/url.rb', line 30

def self.resolve_from_string(target, opts, username = nil, password = nil)
  uri = URI.parse(target)
  return nil if uri.nil? or uri.scheme.nil?
  return nil unless %{ http https }.include? uri.scheme
  target = transform(target)
  opts[:username] = username if username
  opts[:password] = password if password
  new(target, opts)
rescue URI::Error
  nil
end

.transform(target) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fetchers/url.rb', line 73

def self.transform(target)
  transformed_target = if m = GITHUB_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
                         "https://github.com/#{m[:user]}/#{m[:repo]}/archive/master.tar.gz"
                       elsif m = GITHUB_URL_WITH_TREE_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
                         "https://github.com/#{m[:user]}/#{m[:repo]}/archive/#{m[:commit]}.tar.gz"
                       elsif m = BITBUCKET_URL_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
                         "https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/master.tar.gz"
                       elsif m = BITBUCKET_URL_BRANCH_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
                         "https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/#{m[:branch]}.tar.gz"
                       elsif m = BITBUCKET_URL_COMMIT_REGEX.match(target) # rubocop:disable Lint/AssignmentInCondition
                         "https://bitbucket.org/#{m[:user]}/#{m[:repo]}/get/#{m[:commit]}.tar.gz"
                       end

  if transformed_target
    Inspec::Log.warn("URL target #{target} transformed to #{transformed_target}. Consider using the git fetcher")
    transformed_target
  else
    target
  end
end

Instance Method Details

#cache_keyObject



114
115
116
# File 'lib/fetchers/url.rb', line 114

def cache_key
  @archive_shasum ||= sha256
end

#fetch(path) ⇒ Object



106
107
108
# File 'lib/fetchers/url.rb', line 106

def fetch(path)
  @archive_path ||= download_archive(path)
end

#resolved_sourceObject



110
111
112
# File 'lib/fetchers/url.rb', line 110

def resolved_source
  @resolved_source ||= { url: @target, sha256: sha256 }
end

#to_sObject



118
119
120
# File 'lib/fetchers/url.rb', line 118

def to_s
  @target
end