Skip to content

Commit

Permalink
(PDK-370) Adds a 'pdk module generate' redirect to 'pdk new module'.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmjen committed Sep 6, 2017
1 parent 32eee98 commit e83dd19
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/pdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'pdk/cli/errors'
require 'pdk/cli/util'
require 'pdk/cli/util/command_redirector'
require 'pdk/cli/util/option_normalizer'
require 'pdk/cli/util/option_validator'
require 'pdk/cli/exec_group'
Expand Down Expand Up @@ -77,6 +78,7 @@ def self.template_url_option(dsl)
require 'pdk/cli/new'
require 'pdk/cli/test'
require 'pdk/cli/validate'
require 'pdk/cli/module'

@base_cmd.add_command Cri::Command.new_basic_help
end
13 changes: 13 additions & 0 deletions lib/pdk/cli/module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module PDK::CLI
@module_cmd = @base_cmd.define_command do
name 'module'
usage _('module [options]')
summary _('update your module metadata, etc.')
description _('Performs tasks on the module project.')
default_subcommand 'help'
end

@module_cmd.add_command Cri::Command.new_basic_help
end

require 'pdk/cli/module/generate'
40 changes: 40 additions & 0 deletions lib/pdk/cli/module/generate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'tty-prompt'

module PDK::CLI
@module_generate_cmd = @module_cmd.define_command do
name 'generate'
usage _('generate [options] <module_name>')
summary _('This command is now \'pdk new module\'.')

PDK::CLI.template_url_option(self)

flag nil, 'skip-interview', _('When specified, skips interactive querying of metadata.')

run do |opts, args, _cmd|
require 'pdk/generators/module'

module_name = args[0]

if module_name.nil? || module_name.empty?
puts command.help
exit 1
end

PDK.logger.info(_('New modules are created using the ‘pdk new module’ command.'))
prompt = TTY::Prompt.new(help_color: :cyan)
redirect = PDK::CLI::Util::CommandRedirector.new(prompt)
redirect.target_command('pdk new module')
answer = redirect.run

if answer
opts[:name] = module_name
opts[:target_dir] = module_name

PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
PDK::Generate::Module.invoke(opts)
else
exit 1
end
end
end
end
9 changes: 0 additions & 9 deletions lib/pdk/cli/new/module.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module PDK::CLI
@new_module_cmd = @new_cmd.define_command do
name 'module'
Expand All @@ -23,14 +22,6 @@ module PDK::CLI
exit 1
end

unless Util::OptionValidator.valid_module_name?(module_name)
error_msg = _(
"'%{module_name}' is not a valid module name.\n" \
'Module names must begin with a lowercase letter and can only include lowercase letters, digits, and underscores.',
) % { module_name: module_name }
raise PDK::CLI::FatalError, error_msg
end

opts[:name] = module_name
opts[:target_dir] = target_dir.nil? ? module_name : target_dir

Expand Down
24 changes: 24 additions & 0 deletions lib/pdk/cli/util/command_redirector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'tty-prompt'

module PDK
module CLI
module Util
class CommandRedirector < TTY::Prompt::AnswersCollector
def pastel
@pastel ||= Pastel.new
end

def target_command(cmd)
@command = cmd
end

def run
@prompt.puts _('Did you mean \'%{command}\'?') % { command: pastel.bold(@command) }
@prompt.yes?('-->')
rescue TTY::Prompt::Reader::InputInterrupt
nil
end
end
end
end
end
20 changes: 16 additions & 4 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'pdk/module/templatedir'
require 'pdk/cli/exec'
require 'pdk/cli/util/interview'
require 'pdk/cli/util/option_validator'
require 'pdk/util'
require 'pdk/util/version'

Expand All @@ -31,13 +32,24 @@ def self.puppetlabs_template_url
end
end

def self.invoke(opts = {})
def self.validate_options(opts)
unless PDK::CLI::Util::OptionValidator.valid_module_name?(opts[:name])
error_msg = _(
"'%{module_name}' is not a valid module name.\n" \
'Module names must begin with a lowercase letter and can only include lowercase letters, digits, and underscores.',
) % { module_name: opts[:name] }
raise PDK::CLI::FatalError, error_msg
end

target_dir = File.expand_path(opts[:target_dir])

if File.exist?(target_dir)
raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % { dir: target_dir }
end
raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % { dir: target_dir } if File.exist?(target_dir)
end

def self.invoke(opts = {})
validate_options(opts)

target_dir = File.expand_path(opts[:target_dir])
parent_dir = File.dirname(target_dir)

begin
Expand Down
1 change: 0 additions & 1 deletion spec/unit/cli/new/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
let(:module_name) { 'test123' }

it 'validates the module name' do
expect(PDK::CLI::Util::OptionValidator).to receive(:valid_module_name?).with(module_name).and_call_original
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(name: module_name))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['new', 'module', module_name])
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/generate/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
expect(logger).not_to receive(:info).with(a_string_matching(%r{In your new module directory, add classes with the 'pdk new class' command}i))

expect {
described_class.invoke(target_dir: target_dir)
described_class.invoke(name: 'foo', target_dir: target_dir)
}.to raise_error(PDK::CLI::FatalError, %r{destination directory '.+' already exists}i)
end
end
Expand Down

0 comments on commit e83dd19

Please sign in to comment.