Skip to content

Commit

Permalink
Merge pull request #327 from HelenCampbell/questionname
Browse files Browse the repository at this point in the history
 (PDK-628) Addition of module_name question to interview
  • Loading branch information
DavidS committed Nov 8, 2017
2 parents ad600d6 + fc0e834 commit 34d3e63
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 309 deletions.
2 changes: 1 addition & 1 deletion lib/pdk/cli/module/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module PDK::CLI
answer = redirect.run

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

PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/cli/new/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module PDK::CLI
exit 1
end

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

PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
Expand Down
24 changes: 17 additions & 7 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def self.puppetlabs_template_url
end

def self.validate_options(opts)
unless PDK::CLI::Util::OptionValidator.valid_module_name?(opts[:name])
unless PDK::CLI::Util::OptionValidator.valid_module_name?(opts[: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: opts[:name] }
) % { module_name: opts[:module_name] }
raise PDK::CLI::ExitWithError, error_msg
end

Expand Down Expand Up @@ -98,7 +98,7 @@ def self.invoke(opts = {})

begin
if FileUtils.mv(temp_target_dir, target_dir)
PDK.logger.info(_('Module \'%{name}\' generated at path \'%{path}\', from template \'%{template_url}\'.') % { name: opts[:name], path: target_dir, template_url: template_url })
PDK.logger.info(_('Module \'%{name}\' generated at path \'%{path}\', from template \'%{template_url}\'.') % { name: opts[:module_name], path: target_dir, template_url: template_url })
PDK.logger.info(_('In your module directory, add classes with the \'pdk new class\' command.'))
end
rescue Errno::EACCES => e
Expand Down Expand Up @@ -128,7 +128,7 @@ def self.prepare_metadata(opts)
username = PDK.answers['forge-username'] || username_from_login

defaults = {
'name' => "#{username}-#{opts[:name]}",
'name' => "#{username}-#{opts[:module_name]}",
'version' => '0.1.0',
'dependencies' => [],
'requirements' => [
Expand Down Expand Up @@ -170,7 +170,16 @@ def self.prepare_module_directory(target_dir)
def self.module_interview(metadata, opts = {})
questions = [
{
name: 'name',
name: 'module_name',
question: _('If you have a name for your module, add it here.'),
help: _('This is the name that will be associated with your module, it should be relevant to the modules content.'),
required: true,
validate_pattern: %r{\A[a-z0-9]+\Z}i,
validate_message: _('Module names can only contain lowercase letters and numbers'),
default: metadata.data['name'],
},
{
name: 'forge_username',
question: _('If you have a Puppet Forge username, add it here.'),
help: _('We can use this to upload your module to the Forge when it\'s complete.'),
required: true,
Expand Down Expand Up @@ -290,6 +299,7 @@ def self.module_interview(metadata, opts = {})

interview = PDK::CLI::Util::Interview.new(prompt)

questions.reject! { |q| q[:name] == 'module_name' } if opts.key?(:module_name)
questions.reject! { |q| q[:name] == 'license' } if opts.key?(:license)

interview.add_questions(questions)
Expand All @@ -309,8 +319,8 @@ def self.module_interview(metadata, opts = {})
exit 0
end

forge_username = answers['name']
answers['name'] = "#{answers['name']}-#{opts[:name]}"
forge_username = answers['forge_username']
answers['name'] = "#{answers['forge_username']}-" + (opts[:module_name] || answers['module_name'])
answers['license'] = opts[:license] if opts.key?(:license)
answers['operatingsystem_support'].flatten!
metadata.update!(answers)
Expand Down
96 changes: 0 additions & 96 deletions spec/unit/cli/test/unit_spec.rb

This file was deleted.

191 changes: 0 additions & 191 deletions spec/unit/cli/validate_spec.rb

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
let(:module_name) { 'test123' }

it 'validates the module name' do
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(name: module_name))
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(module_name: module_name))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['new', 'module', module_name])
end
Expand All @@ -38,7 +38,7 @@
let(:target_dir) { 'target' }

it 'passes the target directory to PDK::Generate::Module.invoke' do
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(name: module_name, target_dir: target_dir))
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(module_name: module_name, target_dir: target_dir))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['new', 'module', module_name, target_dir])
end
Expand Down
File renamed without changes.
Loading

0 comments on commit 34d3e63

Please sign in to comment.