CFINSPEC-192: Alias inspec json command to inspec export --format json

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
Vasu1105 2022-05-06 12:40:24 +05:30
parent 637f6e263f
commit 29def55515
3 changed files with 80 additions and 8 deletions

View file

@ -121,6 +121,10 @@
"cli_option_target_id":{
"action": "warn",
"prefix": "The --target-id option is deprecated in InSpec 5. Its value will be ignored."
},
"renamed_to_inspec_export":{
"action": "ignore",
"prefix": "The `inspec json` command is deprecated in InSpec 5 and replaced with `inspec export` command."
}
}
}

View file

@ -69,25 +69,46 @@ class Inspec::InspecCLI < Inspec::BaseCLI
desc: "A list of tags to filter controls and include only those. Ignore all other tests."
profile_options
def json(target)
require "json" unless defined?(JSON)
# This deprecation warning is ignored currently.
Inspec.deprecate(:renamed_to_inspec_export)
export(target)
end
desc "export PATH", "read all tests in PATH and generate a summary in json/yaml format."
option :format, type: :string,
desc: "The output format to use json (default), yaml. If valid format is not provided then it will use the default."
option :output, aliases: :o, type: :string,
desc: "Save the created profile to a path"
option :controls, type: :array,
desc: "A list of controls to include. Ignore all other tests."
option :tags, type: :array,
desc: "A list of tags to filter controls and include only those. Ignore all other tests."
profile_options
def export(target)
o = config
diagnose(o)
o["log_location"] = $stderr
configure_logger(o)
# default is json
format = o[:format] || "json"
o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
o[:check_mode] = true
o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache])
profile = Inspec::Profile.for_target(target, o)
dst = o[:output].to_s
if format == "json"
require "json" unless defined?(JSON)
profile = Inspec::Profile.for_target(target, o)
dst = o[:output].to_s
# Write JSON
Inspec::Utils::JsonProfileSummary.produce_json(
info: profile.info,
write_path: dst
)
# Write JSON
Inspec::Utils::JsonProfileSummary.produce_json(
info: profile.info,
write_path: dst
)
elsif format == "yaml"
# TODO:
end
rescue StandardError => e
pretty_handle_exception(e)
end

View file

@ -0,0 +1,47 @@
require "functional/helper"
require "mixlib/shellout"
require "json_schemer"
describe "inspec export" do
include FunctionalHelper
parallelize_me!
# inspec_json_profile_test covers most of the test as inspec export is alias to inspec json.
it "exports the profile in default json format" do
out = inspec("export " + example_profile)
_(out.stderr).must_equal ""
assert_exit_code 0, out
_(JSON.load(out.stdout)).must_be_kind_of Hash
end
it "exports the iaf format profile to default json" do
prepare_examples do |dir|
skip_windows!
unique_key_name = SecureRandom.uuid
# create profile
profile = File.join(dir, "profile_a")
run_inspec_process("init profile profile_a", prefix: "cd #{dir};")
out = run_inspec_process("sign generate-keys --keyname #{unique_key_name}", prefix: "cd #{dir};")
assert_exit_code 0, out
out = run_inspec_process("sign profile --profile #{profile} --keyname #{unique_key_name}", prefix: "cd #{dir};")
assert_exit_code 0, out
out = run_inspec_process("export profile_a-0.1.0.iaf", prefix: "cd #{dir};")
assert_exit_code 0, out
_(out.stderr).must_equal ""
_(JSON.load(out.stdout)).must_be_kind_of Hash
delete_keys(unique_key_name)
end
end
def delete_keys(unique_key_name)
File.delete("#{Inspec.config_dir}/keys/#{unique_key_name}.pem.key") if File.exist?("#{Inspec.config_dir}/keys/#{unique_key_name}.pem.key")
File.delete("#{Inspec.config_dir}/keys/#{unique_key_name}.pem.pub") if File.exist?("#{Inspec.config_dir}/keys/#{unique_key_name}.pem.pub")
end
end