class InspecPlugins::ResourceLister::CliCommand
This class will provide the actual CLI implementation. Its superclass is provided by another call to Inspec.plugin, this time with two args. The first arg specifies we are requesting version 2 of the Plugins API. The second says we are making a CLI Command plugin component, so please make available any DSL needed for that.
In fact, aside from a some housekeeping DSL methods, most of the
DSL provided is that of Thor. Inspec.plugin(2, :cli_command) promises to return a class that is a subclass of Thor. So, to add commands, usage information, and options, use the Thor documentation.
Public Instance Methods
OK, now the actual method itself. If you provide params, you're telling Thor that you accept CLI arguments after all options have been consumed. Let's accept a pattern, assumed to be a wildcard substring. If we provide a default, the CLI arg becomes optional.
# File lib/inspec-resource-lister/cli_command.rb, line 38 def core(pattern = /.+/) # The code here will *only* be executed if someone actually runs # `inspec list-resources core`. So, we can lazily wait to load # expensive things here. However, InSpec has in fact already # loaded the Resources, so we don't have anything to load. # If we were passed a CLI arg, wrap the arg in Regexp matchers so # we will match anywhere in the name. unless pattern == /.+/ pattern = Regexp.new('.*' + pattern + '.*') end # This gets a bit into InSpec innards; but this is simply a Hash. registry = Inspec::Resource.default_registry resource_names = registry.keys.grep(pattern).sort # One day we'll have nice UI support. resource_names.each { |name| puts name } if options[:summary] puts '-' * 30 puts "#{resource_names.count} resources total" end end