lib/berkshelf/source.rb in berkshelf-6.0.1 vs lib/berkshelf/source.rb in berkshelf-6.1.0

- old
+ new

@@ -1,6 +1,7 @@ require "berkshelf/api-client" +require "berkshelf/chef_repo_universe" require "berkshelf/ssl_policies" require "openssl" module Berkshelf class Source @@ -8,13 +9,14 @@ attr_accessor :type attr_accessor :uri_string attr_accessor :options + # @param [Berkshelf::Berksfile] berksfile # @param [String, Berkshelf::SourceURI] source - def initialize(source, **options) - @options = {timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, ssl: {}} + def initialize(berksfile, source, **options) + @options = { timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, ssl: {} } @options.update(options) case source when String # source "https://supermarket.chef.io/" @type = :supermarket @@ -33,11 +35,17 @@ case @type when :chef_server @options[:client_name] ||= Berkshelf::Config.instance.chef.node_name @options[:client_key] ||= Berkshelf::Config.instance.chef.client_key when :artifactory - @options[:api_key] ||= Berkshelf::Config.instance.chef.artifactory_api_key || ENV['ARTIFACTORY_API_KEY'] + @options[:api_key] ||= Berkshelf::Config.instance.chef.artifactory_api_key || ENV["ARTIFACTORY_API_KEY"] + when :chef_repo + @options[:path] = uri_string + # If given a relative path, expand it against the Berksfile's folder. + @options[:path] = File.expand_path(@options[:path], File.dirname(berksfile ? berksfile.filepath : Dir.pwd)) + # Lie because this won't actually parse as a URI. + @uri_string = "file://#{@options[:path]}" end # Set some default SSL options. Berkshelf::Config.instance.ssl.each do |key, value| @options[:ssl][key.to_sym] = value unless @options[:ssl].include?(key.to_sym) end @@ -48,21 +56,23 @@ def ssl_policy @ssl_policy ||= SSLPolicy.new end def api_client - @api_client ||= case type - when :chef_server - APIClient.chef_server(server_url: uri.to_s, **options) - when :artifactory - # Don't accidentally mutate the options. - client_options = options.dup - api_key = client_options.delete(:api_key) - APIClient.new(uri, headers: {'X-Jfrog-Art-Api' => api_key}, **client_options) - else - APIClient.new(uri, **options) - end + @api_client ||= case type + when :chef_server + APIClient.chef_server(server_url: uri.to_s, **options) + when :artifactory + # Don't accidentally mutate the options. + client_options = options.dup + api_key = client_options.delete(:api_key) + APIClient.new(uri, headers: { "X-Jfrog-Art-Api" => api_key }, **client_options) + when :chef_repo + ChefRepoUniverse.new(uri_string, **options) + else + APIClient.new(uri, **options) + end end def uri @uri ||= SourceURI.parse(uri_string) end @@ -131,18 +141,21 @@ def versions(name) universe.select { |cookbook| cookbook.name == name } end def to_s - if type == :supermarket - "#{uri}" + case type + when :supermarket + uri.to_s + when :chef_repo + options[:path] else "#{type}: #{uri}" end end def inspect - "#<#{self.class.name} #{type}: #{uri.to_s.inspect}, #{options.map {|k, v| "#{k}: #{v.inspect}" }.join(', ')}>" + "#<#{self.class.name} #{type}: #{uri.to_s.inspect}, #{options.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>" end def hash [type, uri_string, options].hash end