2019-06-11 22:24:35 +00:00
|
|
|
require "inspec/base_cli"
|
2016-02-08 19:06:07 +00:00
|
|
|
|
|
|
|
module Supermarket
|
|
|
|
class SupermarketCLI < Inspec::BaseCLI
|
2019-06-11 22:24:35 +00:00
|
|
|
namespace "supermarket"
|
2016-02-08 19:06:07 +00:00
|
|
|
|
2016-08-18 18:10:09 +00:00
|
|
|
# TODO: find another solution, once https://github.com/erikhuda/thor/issues/261 is fixed
|
|
|
|
def self.banner(command, _namespace = nil, _subcommand = false)
|
|
|
|
"#{basename} #{subcommand_prefix} #{command.usage}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.subcommand_prefix
|
|
|
|
namespace
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "profiles", "list all available profiles in Chef Supermarket"
|
2016-02-08 19:06:07 +00:00
|
|
|
def profiles
|
|
|
|
# display profiles in format user/profile
|
|
|
|
supermarket_profiles = Supermarket::API.profiles
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
headline("Available profiles:")
|
|
|
|
supermarket_profiles.each do |p|
|
2019-07-09 00:20:30 +00:00
|
|
|
li("#{p["tool_name"]} #{mark_text(p["tool_owner"] + "/" + p["slug"])}")
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "exec PROFILE", "execute a Supermarket profile"
|
2016-03-06 14:07:12 +00:00
|
|
|
exec_options
|
2016-03-01 12:26:36 +00:00
|
|
|
def exec(*tests)
|
2019-01-08 22:12:42 +00:00
|
|
|
o = config
|
2018-02-17 15:49:52 +00:00
|
|
|
diagnose(o)
|
|
|
|
configure_logger(o)
|
|
|
|
|
2016-02-08 19:06:07 +00:00
|
|
|
# iterate over tests and add compliance scheme
|
2019-06-11 22:24:35 +00:00
|
|
|
tests = tests.map { |t| "supermarket://" + t }
|
2016-02-08 19:06:07 +00:00
|
|
|
|
2018-02-17 15:49:52 +00:00
|
|
|
runner = Inspec::Runner.new(o)
|
|
|
|
tests.each { |target| runner.add_target(target) }
|
|
|
|
|
|
|
|
exit runner.run
|
|
|
|
rescue ArgumentError, RuntimeError, Train::UserError => e
|
|
|
|
$stderr.puts e.message
|
|
|
|
exit 1
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
desc "info PROFILE", "display Supermarket profile details"
|
2016-02-08 19:06:07 +00:00
|
|
|
def info(profile)
|
2016-03-01 12:26:36 +00:00
|
|
|
# check that the profile is available
|
|
|
|
supermarket_profiles = Supermarket::API.profiles
|
2019-06-11 22:24:35 +00:00
|
|
|
found = supermarket_profiles.select do |p|
|
2019-07-09 00:20:30 +00:00
|
|
|
profile == "#{p["tool_owner"]}/#{p["slug"]}"
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|
2016-02-08 19:06:07 +00:00
|
|
|
|
2017-02-08 22:49:16 +00:00
|
|
|
if found.empty?
|
2016-03-01 12:26:36 +00:00
|
|
|
puts "#{mark_text(profile)} is not available on Supermarket"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# load details for the specific profile
|
|
|
|
info = Supermarket::API.info(profile)
|
2019-07-09 00:20:30 +00:00
|
|
|
puts "#{mark_text("name: ")} #{info["slug"]}"
|
|
|
|
puts "#{mark_text("owner:")} #{info["owner"]}"
|
|
|
|
puts "#{mark_text("url: ")} #{info["source_url"]}"
|
2016-02-08 19:06:07 +00:00
|
|
|
puts
|
2019-07-09 00:20:30 +00:00
|
|
|
puts "#{mark_text("description: ")} #{info["description"]}"
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-15 16:09:46 +00:00
|
|
|
# register the subcommand to InSpec CLI registry
|
2019-06-11 22:24:35 +00:00
|
|
|
Inspec::Plugins::CLI.add_subcommand(SupermarketCLI, "supermarket", "supermarket SUBCOMMAND ...", "Supermarket commands", {})
|
2016-02-08 19:06:07 +00:00
|
|
|
end
|