class Fetchers::Url

def self.resolve(target, opts = {})

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

def self.resolve_from_string(target, opts)

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

def self.transform(target)

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"
                       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

def cache_key

def cache_key
  @archive_shasum ||= sha256
end

def download_archive(path)

def download_archive(path)
  download_archive_to_temp
  final_path = "#{path}#{@archive_type}"
  FileUtils.mv(temp_archive_path, final_path)
  Inspec::Log.debug("Fetched archive moved to: #{final_path}")
  @temp_archive_path = nil
  final_path
end

def download_archive_to_temp

Downloads archive to temporary file with side effect :( of setting @archive_type
def download_archive_to_temp
  return @temp_archive_path if ! @temp_archive_path.nil?
  Inspec::Log.debug("Fetching URL: #{@target}")
  http_opts = {}
  http_opts['ssl_verify_mode'.to_sym] = OpenSSL::SSL::VERIFY_NONE if @insecure
  http_opts['Authorization'] = "Bearer #{@token}" if @token
  remote = open(@target, http_opts)
  @archive_type = file_type_from_remote(remote) # side effect :(
  archive = Tempfile.new(['inspec-dl-', @archive_type])
  archive.binmode
  archive.write(remote.read)
  archive.rewind
  archive.close
  Inspec::Log.debug("Archive stored at temporary location: #{archive.path}")
  @temp_archive_path = archive.path
end

def fetch(path)

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

def file_type_from_remote(remote)

def file_type_from_remote(remote)
  content_type = remote.meta['content-type']
  file_type = MIME_TYPES[content_type]
  if file_type.nil?
    Inspec::Log.warn("Unrecognized content type: #{content_type}. Assuming tar.gz")
    file_type = '.tar.gz'
  end
  file_type
end

def initialize(url, opts)

def initialize(url, opts)
  @target = url
  @insecure = opts['insecure']
  @token = opts['token']
  @config = opts
end

def resolved_source

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

def sha256

def sha256
  file = @archive_path || temp_archive_path
  Digest::SHA256.hexdigest File.read(file)
end

def temp_archive_path

def temp_archive_path
  @temp_archive_path ||= download_archive_to_temp
end

def to_s

def to_s
  @target
end