diff --git a/lib/pdk/cli.rb b/lib/pdk/cli.rb index 576a656d1..2157cb5c4 100644 --- a/lib/pdk/cli.rb +++ b/lib/pdk/cli.rb @@ -91,6 +91,7 @@ def self.full_interview_option(dsl) require 'pdk/cli/convert' require 'pdk/cli/new' require 'pdk/cli/test' + require 'pdk/cli/update' require 'pdk/cli/validate' require 'pdk/cli/module' diff --git a/lib/pdk/cli/update.rb b/lib/pdk/cli/update.rb new file mode 100644 index 000000000..f821f1fda --- /dev/null +++ b/lib/pdk/cli/update.rb @@ -0,0 +1,29 @@ +require 'pdk/cli/util' + +module PDK::CLI + @update_cmd = @base_cmd.define_command do + name 'update' + usage _('update [options]') + summary _('Update a module that has been created by or converted for use by PDK.') + + flag nil, :noop, _('Do not update the module, just output what would be done.') + flag nil, :force, _('Update the module automatically, with no prompts.') + + be_hidden + + run do |opts, _args, _cmd| + require 'pdk/module/update' + + PDK::CLI::Util.ensure_in_module!( + message: _('`pdk update` can only be run from inside a valid module directory.'), + log_level: :info, + ) + + if opts[:noop] && opts[:force] + raise PDK::CLI::ExitWithError, _('You can not specify --noop and --force when updating a module') + end + + PDK::Module::Update.invoke(opts) + end + end +end diff --git a/lib/pdk/module/update.rb b/lib/pdk/module/update.rb new file mode 100644 index 000000000..0991958d8 --- /dev/null +++ b/lib/pdk/module/update.rb @@ -0,0 +1,9 @@ +module PDK + module Module + class Update + def self.invoke(_opts = {}) + # TODO: do some updatey things + end + end + end +end diff --git a/spec/unit/pdk/cli/update_spec.rb b/spec/unit/pdk/cli/update_spec.rb new file mode 100644 index 000000000..b43e4bb04 --- /dev/null +++ b/spec/unit/pdk/cli/update_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper' + +describe 'PDK::CLI update' do + let(:help_text) { a_string_matching(%r{^USAGE\s+pdk update}m) } + + context 'when not run from inside a module' do + include_context 'run outside module' + + it 'exits with an error' do + expect(logger).to receive(:error).with(a_string_matching(%r{must be run from inside a valid module})) + + expect { + PDK::CLI.run(%w[update]) + }.to raise_error(SystemExit) { |error| + expect(error.status).not_to eq(0) + } + end + end + + context 'when run from inside a module' do + before(:each) do + allow(PDK::Util).to receive(:module_root).and_return('/path/to/test/module') + end + + context 'and provided no flags' do + it 'invokes the updater with no options' do + expect(PDK::Module::Update).to receive(:invoke).with({}) + + PDK::CLI.run(%w[update]) + end + end + + context 'and the --noop flag has been passed' do + it 'passes the noop option through to the updater' do + expect(PDK::Module::Update).to receive(:invoke).with(noop: true) + + PDK::CLI.run(%w[update --noop]) + end + end + + context 'and the --force flag has been passed' do + it 'passes the force option through to the updater' do + expect(PDK::Module::Update).to receive(:invoke).with(force: true) + + PDK::CLI.run(%w[update --force]) + end + end + + context 'and the --force and --noop flags have been passed' do + it 'exits with an error' do + expect(logger).to receive(:error).with(a_string_matching(%r{can not specify --noop and --force}i)) + + expect { + PDK::CLI.run(%w[update --noop --force]) + }.to raise_error(SystemExit) { |error| + expect(error.status).not_to eq(0) + } + end + end + end +end