Split the CLI implementation file

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2018-11-19 19:14:31 -05:00
parent e00582f0ec
commit 677542de02
4 changed files with 77 additions and 40 deletions

View file

@ -8,46 +8,8 @@ module InspecPlugins
class CLI < Inspec.plugin(2, :cli_command)
subcommand_desc 'init SUBCOMMAND', 'Generate InSpec code'
TEMPLATES_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'templates'))
#-------------------------------------------------------------------#
# inspec init profile
#-------------------------------------------------------------------#
def self.valid_profile_platforms
# Look in the 'template/profiles' directory and detect which platforms are available.
profile_templates_dir = File.join(TEMPLATES_PATH, 'profiles')
Dir.glob(File.join(profile_templates_dir, '*')).select { |p| File.directory?(p) }.map { |d| File.basename(d) }
end
no_commands do
def valid_profile_platforms
self.class.valid_profile_platforms
end
end
desc 'profile [OPTIONS] NAME', 'Generate a new profile'
option :platform, default: 'os', type: :string, aliases: [:p],
desc: "Which platform to generate a platform for: choose from #{valid_profile_platforms.join(', ')}"
option :overwrite, type: :boolean, default: false,
desc: 'Overwrites existing directory'
def profile(new_profile_name)
unless valid_profile_platforms.include?(options[:platform])
puts "Unable to generate profile: No template available for platform '#{options[:platform]}' (expected one of: #{valid_profile_platforms.join(', ')})"
exit 1
end
template_path = File.join('profiles', options[:platform])
render_opts = {
templates_path: TEMPLATES_PATH,
overwrite: options[:overwrite],
}
renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)
vars = {
name: new_profile_name,
}
renderer.render_with_values(template_path, 'profile', vars)
end
# require_relative 'cli_profile'
require_relative 'cli_plugin'
end
end
end

View file

@ -0,0 +1,23 @@
# encoding: utf-8
require 'pathname'
require_relative 'renderer'
module InspecPlugins
module Init
class CLI < Inspec.plugin(2, :cli_command)
#-------------------------------------------------------------------#
# inspec init plugin
#-------------------------------------------------------------------#
desc 'PLUGIN_NAME [options]', 'Generates an InSpec plugin, which can extend the functionality of InSpec itself.'
def plugin(plugin_name)
plugin_type = plugin_name.match(/^(inspec|train)\-/)
unless plugin_type
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
end
end
end
end

View file

@ -0,0 +1,52 @@
# encoding: utf-8
require 'pathname'
require_relative 'renderer'
module InspecPlugins
module Init
class CLI < Inspec.plugin(2, :cli_command)
TEMPLATES_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'templates'))
#-------------------------------------------------------------------#
# inspec init profile
#-------------------------------------------------------------------#
def self.valid_profile_platforms
# Look in the 'template/profiles' directory and detect which platforms are available.
profile_templates_dir = File.join(TEMPLATES_PATH, 'profiles')
Dir.glob(File.join(profile_templates_dir, '*')).select { |p| File.directory?(p) }.map { |d| File.basename(d) }
end
no_commands do
def valid_profile_platforms
self.class.valid_profile_platforms
end
end
desc 'profile [OPTIONS] NAME', 'Generate a new profile'
option :platform, default: 'os', type: :string, aliases: [:p],
desc: "Which platform to generate a platform for: choose from #{valid_profile_platforms.join(', ')}"
option :overwrite, type: :boolean, default: false,
desc: 'Overwrites existing directory'
def profile(new_profile_name)
unless valid_profile_platforms.include?(options[:platform])
ui.error "Unable to generate profile: No template available for platform '#{options[:platform]}' (expected one of: #{valid_profile_platforms.join(', ')})"
ui.exit(:usage_error)
end
template_path = File.join('profiles', options[:platform])
render_opts = {
templates_path: TEMPLATES_PATH,
overwrite: options[:overwrite],
}
renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)
vars = {
name: new_profile_name,
}
renderer.render_with_values(template_path, 'profile', vars)
end
end
end
end