lib/license_acceptance/acceptor.rb in license-acceptance-0.2.13 vs lib/license_acceptance/acceptor.rb in license-acceptance-0.2.15
- old
+ new
@@ -8,25 +8,32 @@
require "license_acceptance/strategy/argument"
require "license_acceptance/strategy/prompt"
require "license_acceptance/strategy/provided_value"
module LicenseAcceptance
+
+ ACCEPT = "accept"
+ ACCEPT_SILENT = "accept-silent"
+ ACCEPT_NO_PERSIST = "accept-no-persist"
+
class Acceptor
extend Forwardable
include Logger
attr_reader :config, :product_reader, :env_strategy, :file_strategy, :arg_strategy, :prompt_strategy, :provided_strategy
def initialize(opts={})
@config = Config.new(opts)
Logger.initialize(config.logger)
@product_reader = ProductReader.new
+ product_reader.read
@env_strategy = Strategy::Environment.new(ENV)
@file_strategy = Strategy::File.new(config)
@arg_strategy = Strategy::Argument.new(ARGV)
@prompt_strategy = Strategy::Prompt.new(config)
@provided_strategy = Strategy::ProvidedValue.new(opts.fetch(:provided, nil))
+ @acceptance_value = nil
end
def_delegator :@config, :output
# For applications that just need simple logic to handle a failed license acceptance flow we include this small
@@ -40,21 +47,22 @@
end
def check_and_persist(product_name, version)
if accepted_no_persist?
logger.debug("Chef License accepted with no persistence")
+ @acceptance_value = ACCEPT_NO_PERSIST
return true
end
- product_reader.read
product_relationship = product_reader.lookup(product_name, version)
missing_licenses = file_strategy.accepted?(product_relationship)
# They have already accepted all licenses and stored their acceptance in the persistent files
if missing_licenses.empty?
logger.debug("All licenses present")
+ @acceptance_value = ACCEPT
return true
end
if accepted? || accepted_silent?
if config.persist
@@ -63,15 +71,19 @@
output_num_persisted(missing_licenses.size) unless accepted_silent?
else
output_persist_failed(errs)
end
end
+ @acceptance_value = accepted_silent? ? ACCEPT_SILENT : ACCEPT
return true
elsif config.output.isatty && prompt_strategy.request(missing_licenses) do
+ # We have to infer the acceptance value if they use the prompt to accept
if config.persist
+ @acceptance_value = ACCEPT
file_strategy.persist(product_relationship, missing_licenses)
else
+ @acceptance_value = ACCEPT_NO_PERSIST
[]
end
end
return true
else
@@ -85,10 +97,33 @@
def self.check_and_persist(product_name, version, opts={})
new(opts).check_and_persist(product_name, version)
end
+ # Check whether the specified product requires license acceptance for the given version.
+ def license_required?(mixlib_name, version)
+ product = product_reader.lookup_by_mixlib(mixlib_name)
+ return false if product.nil?
+ return true if version == :latest
+ Gem::Version.new(version) >= Gem::Version.new(product.license_required_version)
+ end
+
+ # Some callers only know about mixlib names so we need a way for them to get the product
+ # name as this library knows it.
+ def name_from_mixlib(mixlib_name)
+ product = product_reader.lookup_by_mixlib(mixlib_name)
+ return nil if product.nil?
+ product.name
+ end
+
+ # Return the value that was matched ("accept", "accept-no-persist", etc.). Used by callers so they do not
+ # have to know the precedence order between provided/environment/argument. Can just get back the value
+ # that was used. Value is only guaranteed to be set after calling check_and_persist.
+ def acceptance_value
+ @acceptance_value
+ end
+
def accepted?
provided_strategy.accepted? || env_strategy.accepted? || arg_strategy.accepted?
end
# no-persist is silent too
@@ -123,10 +158,10 @@
end
class LicenseNotAcceptedError < RuntimeError
def initialize(missing_licenses)
- msg = "Missing licenses for the following:\n* " + missing_licenses.join("\n* ")
+ msg = "Missing licenses for the following:\n* " + missing_licenses.map(&:name).join("\n* ")
super(msg)
end
end
end