Skip to content

Commit

Permalink
(PDK-596) Accept "forgeuser-modulename" as argument to new module
Browse files Browse the repository at this point in the history
This allows another pattern from `puppet module generate`, where the
forge user name was already specified in the initial command.

Like the last commit, this also adds downcasing for future-proofing against
the forge becoming stricter on lower case usernames.
  • Loading branch information
DavidS committed Nov 27, 2017
1 parent 9b11bc7 commit 3e4ef86
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
10 changes: 8 additions & 2 deletions lib/pdk/cli/module/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ module PDK::CLI
answer = redirect.run

if answer
opts[:module_name] = module_name
opts[:target_dir] = module_name
module_name_parts = module_name.split('-', 2)
if module_name_parts.size > 1
opts[:username] = module_name_parts[0]
opts[:module_name] = module_name_parts[1]
else
opts[:module_name] = module_name
end
opts[:target_dir] = opts[:module_name]

PDK.logger.info(_('Creating new module: %{modname}') % { modname: module_name })
PDK::Generate::Module.invoke(opts)
Expand Down
10 changes: 8 additions & 2 deletions lib/pdk/cli/new/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ module PDK::CLI
target_dir = args[1]

unless module_name.nil? || module_name.empty?
opts[:module_name] = module_name
opts[:target_dir] = target_dir.nil? ? module_name : target_dir
module_name_parts = module_name.split('-', 2)
if module_name_parts.size > 1
opts[:username] = module_name_parts[0]
opts[:module_name] = module_name_parts[1]
else
opts[:module_name] = module_name
end
opts[:target_dir] = target_dir.nil? ? opts[:module_name] : target_dir
end

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

def self.prepare_metadata(opts = {})
username = PDK.answers['forge-username'] || username_from_login
opts[:username] = (opts[:username] || PDK.answers['forge-username'] || username_from_login).downcase

defaults = {
'version' => '0.1.0',
Expand All @@ -132,7 +132,7 @@ def self.prepare_metadata(opts = {})
{ 'name' => 'puppet', 'version_requirement' => '>= 4.7.0 < 6.0.0' },
],
}
defaults['name'] = "#{username}-#{opts[:module_name]}" unless opts[:module_name].nil?
defaults['name'] = "#{opts[:username]}-#{opts[:module_name]}" unless opts[:module_name].nil?
defaults['author'] = PDK.answers['author'] unless PDK.answers['author'].nil?
defaults['license'] = PDK.answers['license'] unless PDK.answers['license'].nil?
defaults['license'] = opts[:license] if opts.key? :license
Expand Down Expand Up @@ -297,6 +297,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] == 'forge_username' } if opts.key?(:username)
questions.reject! { |q| q[:name] == 'license' } if opts.key?(:license)

interview.add_questions(questions)
Expand All @@ -316,10 +317,10 @@ def self.module_interview(metadata, opts = {})
exit 0
end

forge_username = answers['forge_username']
opts[:username] ||= answers['forge_username']
opts[:module_name] ||= answers['module_name']

answers['name'] = "#{answers['forge_username']}-" + (opts[:module_name])
answers['name'] = "#{opts[:username]}-" + (opts[:module_name])
answers['license'] = opts[:license] if opts.key?(:license)
answers['operatingsystem_support'].flatten!

Expand Down Expand Up @@ -347,7 +348,7 @@ def self.module_interview(metadata, opts = {})
end

PDK.answers.update!(
'forge-username' => forge_username,
'forge-username' => opts[:username],
'author' => answers['author'],
'license' => answers['license'],
)
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/pdk/cli/module/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
expect(PDK::Generate::Module).to receive(:invoke)
PDK::CLI.run(['module', 'generate', module_name])
end

context 'when passed a module name with a forge user' do
let(:module_name) { 'user-test123' }

it 'validates and parses the module name' do
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(module_name: 'test123', username: 'user'))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['module', 'generate', module_name])
end
end
end

# context 'with a no at the prompt' do
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/pdk/cli/new/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@
end
end
end

context 'when passed a module name with a forge user' do
let(:module_name) { 'user-test123' }

it 'validates and parses the module name' do
expect(PDK::Generate::Module).to receive(:invoke).with(hash_including(module_name: 'test123', username: 'user'))
expect(logger).to receive(:info).with("Creating new module: #{module_name}")
PDK::CLI.run(['new', 'module', module_name])
end
end
end

0 comments on commit 3e4ef86

Please sign in to comment.