Adapt to new UI module

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2018-11-16 15:46:06 -05:00
parent 1ef8cc9916
commit e00582f0ec
3 changed files with 39 additions and 34 deletions

View file

@ -2,7 +2,6 @@
require 'pathname'
require_relative 'renderer'
require 'byebug'
module InspecPlugins
module Init
@ -32,7 +31,6 @@ module InspecPlugins
option :overwrite, type: :boolean, default: false,
desc: 'Overwrites existing directory'
def profile(new_profile_name)
byebug
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
@ -43,12 +41,12 @@ module InspecPlugins
templates_path: TEMPLATES_PATH,
overwrite: options[:overwrite],
}
renderer = InspecPlugins::Init::Renderer.new(self, render_opts)
renderer = InspecPlugins::Init::Renderer.new(ui, render_opts)
vars = {
name: new_profile_name,
}
renderer.render_with_values(template_path, vars)
renderer.render_with_values(template_path, 'profile', vars)
end
end
end

View file

@ -17,49 +17,56 @@ module InspecPlugins
end
# rubocop: disable Metrics/AbcSize
def render_with_values(template_subdir_path, template_values = {})
def render_with_values(template_subdir_path, template_type, template_values = {})
# look for template directory
base_dir = File.join(templates_path, template_subdir_path)
# prepare glob for all subdirectories and files
template_glob = File.join(base_dir, '**', '{*,.*}')
# Use the name attribute to define the path to the profile.
profile_path = template_values[:name]
# Use slashes (\, /) to split up the name into an Array then use the last entry
# to reset the name of the profile.
template_values[:name] = template_values[:name].split(%r{\\|\/}).last
# Generate the full full_destination_root_path path on disk
full_destination_root_path = Pathname.new(Dir.pwd).join(profile_path)
source_dir = File.join(templates_path, template_subdir_path)
# This is a bit gross
generator_type = template_subdir_path.split(%r{[\/]}).first.sub(/s$/, '')
ui.plain_text "Create new #{generator_type} at #{ui.mark_text(full_destination_root_path)}"
# prepare glob for all subdirectories and files
template_glob = File.join(source_dir, '**', '{*,.*}')
# Use the name attribute to define the path to the new thing.
# May contain slashes.
relative_destination_path = template_values[:name]
# Now reset the :name variable to be the basename.
# This is important in profiles, for example.
template_values[:name] = File.basename(template_values[:name])
# Generate the full full_destination_path path on disk
full_destination_path = Pathname.new(Dir.pwd).join(relative_destination_path)
# check that the directory does not exist
if File.exist?(full_destination_root_path) && !overwrite_mode
ui.plain_text "#{ui.mark_text(full_destination_root_path)} exists already, use --overwrite"
ui.exit(1)
if File.exist?(full_destination_path) && !overwrite_mode
ui.plain_line "#{ui.emphasis(full_destination_path)} exists already, use --overwrite"
ui.exit(:usage_error)
end
ui.headline('InSpec Code Generator')
ui.plain_line "Creating new #{template_type} at #{ui.emphasis(full_destination_path)}"
# ensure that full_destination_root_path directory is available
FileUtils.mkdir_p(full_destination_root_path)
FileUtils.mkdir_p(full_destination_path)
# iterate over files and write to full_destination_root_path
Dir.glob(template_glob) do |file|
relative_destination_item_path = Pathname.new(file).relative_path_from(Pathname.new(base_dir))
full_destination_item_path = Pathname.new(full_destination_root_path).join(relative_destination_item_path)
if File.directory?(file)
ui.li "Create directory #{ui.mark_text(relative_destination_item_path)}"
Dir.glob(template_glob) do |source_file|
relative_destination_item_path = Pathname.new(source_file).relative_path_from(Pathname.new(source_dir))
full_destination_item_path = Pathname.new(full_destination_path).join(relative_destination_item_path)
if File.directory?(source_file)
ui.list_item "Creating directory #{ui.emphasis(relative_destination_item_path)}"
FileUtils.mkdir_p(full_destination_item_path)
elsif File.file?(file)
ui.li "Create file #{ui.mark_text(relative_destination_item_path)}"
elsif File.file?(source_file)
ui.list_item "Creating file #{ui.emphasis(relative_destination_item_path)}"
# read & render content
content = render(File.read(file), template_values)
content = render(File.read(source_file), template_values)
# write file content
File.write(full_destination_item_path, content)
else
ui.plain_text "Ignore #{file}, because its not an file or directoy"
ui.warning "Ignoring #{ui.emphasis(source_file)}, because its not an file or directoy"
end
end
ui.plain_line
end
# rubocop: enable Metrics/AbcSize

View file

@ -11,7 +11,7 @@ class InitCli < MiniTest::Test
profile = File.join(dir, 'test-profile')
out = run_inspec_process("init profile test-profile", prefix: "cd #{dir} &&")
assert_equal 0, out.exit_status
assert_includes out.stdout, 'Create new profile at'
assert_includes out.stdout, 'Creating new profile at'
assert_includes out.stdout, profile
assert_includes Dir.entries(profile).join, 'inspec.yml'
assert_includes Dir.entries(profile).join, 'README.md'
@ -23,7 +23,7 @@ class InitCli < MiniTest::Test
profile = File.join(dir, 'test-profile')
out = run_inspec_process("init profile --platform os test-profile", prefix: "cd #{dir} &&")
assert_equal 0, out.exit_status
assert_includes out.stdout, 'Create new profile at'
assert_includes out.stdout, 'Creating new profile at'
assert_includes out.stdout, profile
assert_includes Dir.entries(profile).join, 'inspec.yml'
assert_includes Dir.entries(profile).join, 'README.md'
@ -55,7 +55,7 @@ class InitCli < MiniTest::Test
profile = File.join(dir, 'test-gcp-profile')
out = run_inspec_process("init profile --platform gcp test-gcp-profile", prefix: "cd #{dir} &&")
assert_equal 0, out.exit_status
assert_includes out.stdout, 'Create new profile at'
assert_includes out.stdout, 'Creating new profile at'
assert_includes out.stdout, profile
assert_includes Dir.entries(profile).join, 'inspec.yml'
assert_includes Dir.entries(profile).join, 'README.md'