diff --git a/.expeditor/config.yml b/.expeditor/config.yml index 2902e9213..7bd1e6926 100644 --- a/.expeditor/config.yml +++ b/.expeditor/config.yml @@ -261,3 +261,6 @@ subscriptions: - workload: ruby_gem_published:stove-* actions: - bash:.expeditor/update_dep.sh + - workload: ruby_gem_published:license-acceptance-* + actions: + - bash:.expeditor/update_dep.sh diff --git a/Gemfile.lock b/Gemfile.lock index f0ab9b62e..341b7185d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,6 +7,7 @@ PATH cookbook-omnifetch (~> 0.5) diff-lcs (~> 1.0) ffi-yajl (>= 1.0, < 3.0) + license-acceptance (~> 1.0, >= 1.0.11) minitar (~> 0.6) mixlib-cli (>= 1.7, < 3.0) mixlib-shellout (>= 2.0, < 4.0) diff --git a/bin/chef b/bin/chef index e8ee5848d..cd60a86bb 100755 --- a/bin/chef +++ b/bin/chef @@ -22,4 +22,4 @@ Kernel.trap(:INT) { print("\n"); exit 1 } $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))) require "chef-dk/cli" -ChefDK::CLI.new(ARGV.clone).run +ChefDK::CLI.new(ARGV.clone).run(enforce_license: true) diff --git a/chef-dk.gemspec b/chef-dk.gemspec index e2e77829c..bb2d3f069 100644 --- a/chef-dk.gemspec +++ b/chef-dk.gemspec @@ -49,6 +49,7 @@ Gem::Specification.new do |gem| gem.add_dependency "cookbook-omnifetch", "~> 0.5" gem.add_dependency "diff-lcs", "~> 1.0" gem.add_dependency "paint", "~> 1.0" + gem.add_dependency "license-acceptance", "~> 1.0", ">= 1.0.11" %w{rspec-core rspec-expectations rspec-mocks}.each do |dev_gem| gem.add_development_dependency dev_gem, "~> 3.0" diff --git a/lib/chef-dk/cli.rb b/lib/chef-dk/cli.rb index 4e0654b52..3f9910412 100644 --- a/lib/chef-dk/cli.rb +++ b/lib/chef-dk/cli.rb @@ -57,7 +57,7 @@ def initialize(argv) super() # mixlib-cli #initialize doesn't allow arguments end - def run + def run(enforce_license: false) sanity_check! subcommand_name, *subcommand_params = argv @@ -70,7 +70,7 @@ def run handle_options elsif have_command?(subcommand_name) subcommand = instantiate_subcommand(subcommand_name) - exit_code = subcommand.run_with_default_options(subcommand_params) + exit_code = subcommand.run_with_default_options(enforce_license, subcommand_params) exit normalized_exit_code(exit_code) else err "Unknown command `#{subcommand_name}'." diff --git a/lib/chef-dk/command/base.rb b/lib/chef-dk/command/base.rb index b6c443e34..5d4d74d5b 100644 --- a/lib/chef-dk/command/base.rb +++ b/lib/chef-dk/command/base.rb @@ -19,12 +19,15 @@ require "chef-dk/helpers" require "chef-dk/version" require "chef/exceptions" +require "license_acceptance/acceptor" +require "license_acceptance/cli_flags/mixlib_cli" module ChefDK module Command class Base include Mixlib::CLI include ChefDK::Helpers + include LicenseAcceptance::CLIFlags::MixlibCLI option :help, short: "-h", @@ -47,7 +50,7 @@ def initialize # In order to control this behavior, make sure the default options are # handled here. # - def run_with_default_options(params = [ ]) + def run_with_default_options(enforce_license, params = [ ]) if needs_help?(params) msg(opt_parser.to_s) 0 @@ -55,6 +58,7 @@ def run_with_default_options(params = [ ]) msg("Chef Development Kit Version: #{ChefDK::VERSION}") 0 else + check_license_acceptance if enforce_license run(params) end rescue Chef::Exceptions::ConfigurationError => e @@ -74,6 +78,10 @@ def needs_version?(params) params.include?("-v") || params.include?("--version") end + def check_license_acceptance + LicenseAcceptance::Acceptor.check_and_persist!("chef-dk", ChefDK::VERSION.to_s) + end + end end end diff --git a/spec/unit/command/base_spec.rb b/spec/unit/command/base_spec.rb index 0dc96be14..cac4937b8 100644 --- a/spec/unit/command/base_spec.rb +++ b/spec/unit/command/base_spec.rb @@ -42,6 +42,7 @@ def run(params) let(:stderr_io) { StringIO.new } let(:stdout_io) { StringIO.new } let(:command_instance) { TestCommand.new() } + let(:enforce_license) { false } def stdout stdout_io.string @@ -57,7 +58,7 @@ def stderr end def run_command(options) - command_instance.run_with_default_options(options) + command_instance.run_with_default_options(enforce_license, options) end it "should print the banner for -h" do @@ -95,6 +96,26 @@ def run_command(options) expect(stdout).to eq("thanks for passing me true\n") end + describe "when enforce_license is true" do + let(:enforce_license) { true } + + it "calls the license acceptance library" do + expect(LicenseAcceptance::Acceptor).to receive(:check_and_persist!).with("chef-dk", ChefDK::VERSION.to_s) + run_command([]) + expect(stdout).to eq("thanks for passing me \n") + end + end + + describe "when enforce_license is false" do + let(:enforce_license) { false } + + it "does not call the license acceptance library" do + expect(LicenseAcceptance::Acceptor).to_not receive(:check_and_persist!) + run_command([]) + expect(stdout).to eq("thanks for passing me \n") + end + end + describe "when given invalid options" do it "prints the help banner and exits gracefully" do @@ -105,6 +126,7 @@ def run_command(options) expected = <<~E use me please -a, --arg ARG An option with a required argument + --chef-license ACCEPTANCE Accept the license for this product and any contained products ('accept', 'accept-no-persist', or 'accept-silent') -h, --help Show this message -u, --user If the user exists -v, --version Show chef version @@ -125,6 +147,7 @@ def run_command(options) expected = <<~E use me please -a, --arg ARG An option with a required argument + --chef-license ACCEPTANCE Accept the license for this product and any contained products ('accept', 'accept-no-persist', or 'accept-silent') -h, --help Show this message -u, --user If the user exists -v, --version Show chef version diff --git a/spec/unit/command/env_spec.rb b/spec/unit/command/env_spec.rb index e1bb12955..d724cf700 100644 --- a/spec/unit/command/env_spec.rb +++ b/spec/unit/command/env_spec.rb @@ -36,7 +36,7 @@ end def run_command - command_instance.run_with_default_options(command_options) + command_instance.run_with_default_options(false, command_options) end it "has a usage banner" do diff --git a/spec/unit/command/exec_spec.rb b/spec/unit/command/exec_spec.rb index fa230e21f..98d0165a2 100644 --- a/spec/unit/command/exec_spec.rb +++ b/spec/unit/command/exec_spec.rb @@ -24,7 +24,7 @@ let(:command_options) { [] } def run_command - command_instance.run_with_default_options(command_options) + command_instance.run_with_default_options(false, command_options) end it "has a usage banner" do