lib/berkshelf/source.rb in berkshelf-4.2.3 vs lib/berkshelf/source.rb in berkshelf-4.3.0
- old
+ new
@@ -3,19 +3,46 @@
module Berkshelf
class Source
include Comparable
# @return [Berkshelf::SourceURI]
- attr_reader :uri
+ attr_accessor :uri
+ # @return [Berkshelf::APIClient]
+ attr_accessor :api_client
+
+ attr_accessor :source
+
# @param [String, Berkshelf::SourceURI] uri
- def initialize(uri)
- @uri = SourceURI.parse(uri)
- @api_client = APIClient.new(uri, ssl: {verify: Berkshelf::Config.instance.ssl.verify})
+ def initialize(source)
+ @source = source
@universe = nil
end
+ def api_client
+ @api_client ||= begin
+ if source == :chef_server
+ APIClient.chef_server(
+ ssl: {verify: Berkshelf::Config.instance.ssl.verify},
+ client_name: Berkshelf::Config.instance.chef.node_name,
+ server_url: Berkshelf::Config.instance.chef.chef_server_url,
+ client_key: Berkshelf::Config.instance.chef.client_key,
+ )
+ else
+ APIClient.new(uri, ssl: {verify: Berkshelf::Config.instance.ssl.verify})
+ end
+ end
+ end
+
+ def uri
+ @uri ||= if source == :chef_server
+ SourceURI.parse(Berkshelf::Config.instance.chef.chef_server_url)
+ else
+ SourceURI.parse(source)
+ end
+ end
+
# Forcefully obtain the universe from the API endpoint and assign it to {#universe}. This
# will reload the value of {#universe} even if it has been loaded before.
#
# @return [Array<APIClient::RemoteCookbook>]
def build_universe
@@ -51,20 +78,20 @@
# @return [Array<APIClient::RemoteCookbook]
def search(name)
universe
.select { |cookbook| cookbook.name =~ Regexp.new(name) }
.group_by(&:name)
- .collect { |name, versions| versions.max_by { |v| Semverse::Version.new(v.version) } }
+ .collect { |_, versions| versions.max_by { |v| Semverse::Version.new(v.version) } }
end
# Determine if this source is a "default" source, as defined in the
# {Berksfile}.
#
# @return [true, false]
# true if this a default source, false otherwise
def default?
- @default_ ||= @uri.host == URI.parse(Berksfile::DEFAULT_API_URL).host
+ @default_ ||= uri.host == URI.parse(Berksfile::DEFAULT_API_URL).host
end
# @param [String] name
#
# @return [APIClient::RemoteCookbook]
@@ -94,11 +121,7 @@
def ==(other)
return false unless other.is_a?(self.class)
uri == other.uri
end
- private
-
- # @return [Berkshelf::APIClient]
- attr_reader :api_client
end
end