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 d26929d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 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
12 changes: 12 additions & 0 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,7 +32,18 @@ def self.puppetlabs_template_url
end
end

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
end

def self.invoke(opts = {})
validate_options(opts)
target_dir = File.expand_path(opts[:target_dir])

if File.exist?(target_dir)
Expand Down

0 comments on commit d26929d

Please sign in to comment.