lib/license_acceptance/acceptor.rb in license-acceptance-2.0.0 vs lib/license_acceptance/acceptor.rb in license-acceptance-2.1.1

- old
+ new

@@ -1,6 +1,6 @@ -require "forwardable" +require "forwardable" unless defined?(Forwardable) require "license_acceptance/config" require "license_acceptance/logger" require "license_acceptance/product_reader" require "license_acceptance/product_relationship" require "license_acceptance/strategy/environment" @@ -72,35 +72,45 @@ else output_persist_failed(errs) end end @acceptance_value = accepted_silent? ? ACCEPT_SILENT : ACCEPT - 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 # rubocop: disable Lint/AssignmentInCondition - file_strategy.persist(product_relationship, missing_licenses) - else - @acceptance_value = ACCEPT_NO_PERSIST # rubocop: disable Lint/AssignmentInCondition - [] - end + return true end - true - else + + if acceptance_value_provided? + value = provided_strategy.value || env_strategy.value || arg_strategy.value + output.puts("Unrecognized license acceptance value '#{value}', expected one of: '#{ACCEPT}', '#{ACCEPT_SILENT}', '#{ACCEPT_NO_PERSIST}'") raise LicenseNotAcceptedError.new(product_relationship.parent, missing_licenses) end + + return true if output.isatty && accepted_license_prompt?(product_relationship, missing_licenses) + + raise LicenseNotAcceptedError.new(product_relationship.parent, missing_licenses) end def self.check_and_persist!(product_id, version, opts = {}) new(opts).check_and_persist!(product_id, version) end def self.check_and_persist(product_id, version, opts = {}) new(opts).check_and_persist(product_id, version) end + def accepted_license_prompt?(product_relationship, missing_licenses) + 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 + 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? # If they don't pass a version we assume they want latest @@ -136,9 +146,13 @@ end # persist but be silent like no-persist def accepted_silent? provided_strategy.silent? || env_strategy.silent? || arg_strategy.silent? + end + + def acceptance_value_provided? + provided_strategy.value? || env_strategy.value? || arg_strategy.value? end # In the case where users accept with a command line argument or environment variable # we still want to output the fact that the filesystem was changed. def output_num_persisted(count)