Add interactive prompting

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2018-11-26 00:05:08 -05:00
parent 731b0705f5
commit fe7a683198
2 changed files with 96 additions and 36 deletions

View file

@ -10,17 +10,23 @@ module InspecPlugins
# inspec init plugin
#-------------------------------------------------------------------#
desc 'PLUGIN_NAME [options]', 'Generates an InSpec plugin, which can extend the functionality of InSpec itself.'
# General options
option :prompt, type: :boolean, default: true, desc: 'Interactively prompt for information to put in your generated plugin.'
# Templating vars
option :author_email, type: :string, default: 'you@example.com', desc: 'Author Email for gemspec'
option :author_name, type: :string, default: 'Your Name', desc: 'Author Name for gemspec'
option :copyright, type: :string, default: '', desc: 'A copyright statement, to be added to LICENSE'
option :description, type: :string, default: '', desc: 'Multi-paragraph description of the plugin.'
option :description, type: :string, default: '', desc: 'Multi-line description of the plugin'
option :summary, type: :string, default: 'A plugin with a default summary', desc: 'One-line summary of your plugin'
option :license_name, type: :string, default: 'Apache-2.0', desc: 'The name of a license'
option :hook, type: :array, default: ['cli_command:my_command'], desc: 'A list of plugin hooks, in the form type1:name1, type2:name2, etc'
# These vars have calculated defaults
option :homepage, type: :string, default: '', desc: 'A URL for your project, often a GitHub link'
option :module_name, type: :string, default: '', desc: 'Module Name for your plugin package. Will change plugin name to CamelCase by default.'
option :homepage, type: :string, default: nil, desc: 'A URL for your project, often a GitHub link'
option :module_name, type: :string, default: nil, desc: 'Module Name for your plugin package. Will change plugin name to CamelCase by default.'
option :license_text, type: :string, default: '', hide: true
option :plugin_name, type: :string, default: '', hide: true # This is here to give a uniform interface
option :copyright, type: :string, default: nil, desc: 'A copyright statement, to be added to LICENSE'
def plugin(plugin_name)
plugin_type = plugin_name.match(/^(inspec|train)\-/)
@ -28,15 +34,22 @@ module InspecPlugins
ui.error("Plugin names must begin with either " + ui.emphasis('inspec') + ' or ' + ui.emphasis('train') + ' - saw ' + ui.emphasis(plugin_name))
ui.exit(:usage_error)
end
options[:plugin_name] = plugin_name
plugin_type = plugin_type[1]
unless plugin_type == 'inspec'
ui.error('Sorry, only inspec plugins are supported at this time: train support is not implemented yet.')
ui.exit(:usage_error)
end
snake_case = plugin_name.tr('-', '_')
template_vars = {
name: plugin_name,
plugin_name: plugin_name,
snake_case: snake_case,
}.merge(plugin_vars_from_opts)
template_path = File.join('plugins', plugin_type + '-plugin-template')
snake_case = plugin_name.tr('-', '_')
rename_map = {
'inspec-plugin-template.gemspec' => plugin_name + '.gemspec',
@ -55,29 +68,69 @@ module InspecPlugins
}
renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)
vars = {
name: plugin_name,
plugin_name: plugin_name,
snake_case: snake_case,
}.merge(plugin_vars_from_opts(plugin_name))
renderer.render_with_values(template_path, plugin_type + ' plugin', vars)
renderer.render_with_values(template_path, plugin_type + ' plugin', template_vars)
end
private
def plugin_vars_from_opts(plugin_name)
{
module_name: options[:module_name].empty? ? plugin_name.sub(/^(inspec|train)\-/, '').split('-').map(&:capitalize).join('') : options[:module_name],
author_name: options[:author_name],
author_email: options[:author_email],
summary: options[:summary],
description: options[:description],
homepage: options[:homepage].empty? ? 'https://github.com/example-org/' + plugin_name : options[:homepage],
license_name: options[:license_name],
license_text: fetch_license_text(options[:license_name]),
copyright: options[:copyright],
activators: options[:activators],
}.merge(parse_hook_option(options[:hook]))
def plugin_vars_from_opts
# Set dynamic defaults
options[:module_name] ||= options[:plugin_name].sub(/^(inspec|train)\-/, '').split('-').map(&:capitalize).join('')
# Also set copyright and homepage, but that may be prompted
if options[:prompt] && ui.interactive?
vars = options.dup.merge(vars_from_prompts)
elsif !options[:prompt]
options[:copyright] ||= 'Copyright © ' + Date.today.year.to_s + ' ' + options[:author_name]
options[:homepage] ||= 'https://github.com/' + options[:author_email].split('@').first + '/' + options[:plugin_name]
options[:license_text] = fetch_license_text(options[:license_name])
vars = options.dup
else
ui.error('You requested interactive prompting for the template variables, but this does not seem to be an interactive terminal.')
ui.exit(:usage_error)
end
vars.merge(parse_hook_option(options[:hook]))
end
def vars_from_prompts
option_defs = self.class.all_commands['plugin'].options
order = {
:author_name => {},
:author_email => {},
:summary => {},
:description => { mode: :multiline },
:module_name => {},
:copyright => { default_setter: Proc.new { options[:copyright] ||= 'Copyright © ' + Date.today.year.to_s + ' ' + options[:author_name] } },
:license_name => {
mode: :select,
choices: [
{ name: 'Apache 2.0', value: 'Apache-2.0', default: true, },
{ name: 'Modified BSD', value: 'BSD-3-Clause', },
{ name: 'Proprietary (Closed Source)', value: 'Proprietary', },
{ name: 'Other (edit LICENSE yourself)', value: 'Other', },
]
},
:homepage => { default_setter: Proc.new { options[:homepage] ||= 'https://github.com/' + options[:author_email].split('@').first + '/' + options[:plugin_name] } }
# TODO: Handle hooks, when we ever have more than one type of plugin
}
order.each do |opt_name, prompt_options|
opt_def = option_defs[opt_name]
prompt_options[:default_setter].call if prompt_options[:default_setter]
case prompt_options[:mode]
when :select
options[opt_name] = ui.prompt.select('Choose ' + opt_def.description + ':', prompt_options[:choices])
when :multiline
options[opt_name] = ui.prompt.multiline('Enter ' + opt_def.description + '. Press Control-D to end.', default: options[opt_name])
else
# Assume plain ask
options[opt_name] = ui.prompt.ask('Enter ' + opt_def.description + ':', default: options[opt_name])
end
end
options[:license_text] = fetch_license_text(options[:license_name])
options
end
def parse_hook_option(raw_option)
@ -103,8 +156,14 @@ module InspecPlugins
def fetch_license_text(license_name)
case license_name
when 'Proprietary'
return <<~EOL
Proprietary software. All Rights Reserved.
EOL
when 'Apache-2.0'
return <<~EOL
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
@ -118,7 +177,7 @@ module InspecPlugins
limitations under the License.
EOL
when 'BSD-Modified'
when 'BSD-3-Clause'
return <<~EOL
Modified BSD License
@ -134,7 +193,7 @@ module InspecPlugins
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
EOL
else
''
'"Other" license selected at plugin generation time - please insert your license here.'
end
end
end

View file

@ -6,7 +6,7 @@ class InitPluginCli < MiniTest::Test
def test_generating_inspec_plugin_correct_prefix_required
Dir.mktmpdir do |dir|
plugin = 'wacky-name'
run_result = run_inspec_process("init plugin #{plugin}", prefix: "cd #{dir} &&")
run_result = run_inspec_process("init plugin --no-prompt #{plugin} ", prefix: "cd #{dir} &&")
assert_equal 1, run_result.exit_status
assert_empty run_result.stderr
assert_includes run_result.stdout, 'ERROR'
@ -20,8 +20,9 @@ class InitPluginCli < MiniTest::Test
snake_case = plugin.tr('-', '_')
module_name = plugin.sub(/^inspec\-/, '').split('-').map(&:capitalize).join('')
run_result = run_inspec_process("init plugin #{plugin}", prefix: "cd #{dir} &&")
run_result = run_inspec_process("init plugin --no-prompt #{plugin}", prefix: "cd #{dir} &&")
assert_empty run_result.stderr
assert_equal 0, run_result.exit_status
assert_includes run_result.stdout, 'Creating new inspec plugin at'
assert_includes run_result.stdout, plugin
@ -47,7 +48,7 @@ class InitPluginCli < MiniTest::Test
/spec\.email\s+=\s+\['you@example\.com'\]/,
/spec\.summary\s+=\s+'A plugin with a default summary'/,
/spec\.description\s+=\s+''/,
/spec\.homepage\s+=\s+'https:\/\/github.com\/example-org\/#{plugin}'/,
/spec\.homepage\s+=\s+'https:\/\/github.com\/you\/#{plugin}'/,
/spec\.license\s+=\s+'Apache-2\.0'/,
],
File.join(plugin, 'lib', plugin + '.rb') => [
@ -114,15 +115,15 @@ class InitPluginCli < MiniTest::Test
opts = ''
opts += ' --author-email bob@example.com '
opts += ' --author-name Bob '
opts += ' --copyright "Copyright (c) 2018 Bob" '
opts += ' --copyright "Copyright © 2018 Bob" '
opts += ' --description "That you will really like" '
opts += ' --license-name BSD-Modified '
opts += ' --license-name BSD-3-Clause '
opts += ' --summary "A fantastic plugin" '
opts += ' --homepage http://example.com '
opts += ' --module_name FunPlugin'
run_result = run_inspec_process("init plugin #{plugin} #{opts}", prefix: "cd #{dir} &&")
run_result = run_inspec_process("init plugin #{plugin} --no-prompt #{opts}", prefix: "cd #{dir} &&")
assert_empty run_result.stderr
assert_equal 0, run_result.exit_status
assert_includes run_result.stdout, 'Creating new inspec plugin at'
@ -133,7 +134,7 @@ class InitPluginCli < MiniTest::Test
{
File.join(plugin, 'README.md') => [],
File.join(plugin, 'LICENSE') => [
/Copyright \(c\) 2018 Bob/,
/Copyright © 2018 Bob/,
/used to endorse or promote/,
],
File.join(plugin, 'Gemfile') => [],
@ -145,7 +146,7 @@ class InitPluginCli < MiniTest::Test
/spec\.summary\s+=\s+'A fantastic plugin'/,
/spec\.description\s+=\s+'That you will really like'/,
/spec\.homepage\s+=\s+'http:\/\/example.com'/,
/spec\.license\s+=\s+'BSD-Modified'/,
/spec\.license\s+=\s+'BSD-3-Clause'/,
],
File.join(plugin, 'lib', plugin + '.rb') => [],
File.join(plugin, 'lib', plugin, 'plugin.rb') => [],