How to use registry method of InspecPlugins.PluginManager Package

Best Inspec_ruby code snippet using InspecPlugins.PluginManager.registry

cli_command.rb

Source:cli_command.rb Github

copy

Full Screen

...170 # input a lot and hand the installer a sure-thing.171 # Name OK?172 check_plugin_name(plugin_name, 'installation')173 # Already installed?174 if registry.known_plugin?(plugin_name.to_sym)175 puts(red { 'Plugin already installed' } + " - #{plugin_name} - Use 'inspec plugin list' to see previously installed plugin - installation failed.")176 exit 2177 end178 # Can we figure out how to load it?179 entry_point = install_from_path__apply_entry_point_heuristics(path)180 # If you load it, does it act like a plugin?181 install_from_path__probe_load(entry_point, plugin_name)182 # OK, install it!183 installer.install(plugin_name, path: entry_point)184 puts(bold { plugin_name } + ' plugin installed via source path reference, resolved to entry point ' + entry_point)185 exit 0186 end187 # Rationale for rubocop variances: It's a heuristics method, and will be full of188 # conditionals. The code is well-commented; refactoring into sub-methods would189 # reduce clarity.190 def install_from_path__apply_entry_point_heuristics(path) # rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity191 given = Pathname.new(path)192 given = given.expand_path # Resolve any relative paths193 name_regex = /^(inspec|train)-/194 versioned_regex = /^(inspec|train)-[a-z0-9\-\_]+-\d+\.\d+\.\d+$/195 # What are the last four things like?196 parts = [197 given.parent.parent.basename,198 given.parent.basename,199 given.basename('.rb'),200 given.extname,201 ].map(&:to_s)202 # Case 1: Simplest case: it was a full entry point, as presented.203 # /home/you/projects/inspec-something/lib/inspec-something.rb204 # parts index: ^0^ ^1^ ^2^ ^3^205 if parts[0] =~ name_regex && parts[1] == 'lib' && parts[2] == parts[0] && parts[3] == '.rb'206 return given.to_s207 end208 # Case 2: Also easy: they either referred to the internal library directory,209 # or left the extansion off. Those are the same to us.210 # /home/you/projects/inspec-something/lib/inspec-something211 # parts index: ^0^ ^1^ ^2^ (3 is empty)212 if parts[0] =~ name_regex && parts[1] == 'lib' && parts[2] == parts[0] && parts[3].empty?213 return given.to_s + '.rb'214 end215 # Case 3: Maybe they were refering to a path that is inside a gem installation, or an exploded gem?216 # In that case, we'll have a version on the plugin name in part 0217 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something.rb218 # parts index: ^0^ ^1^ ^2^ ^3^219 if parts[0] =~ versioned_regex && parts[1] == 'lib' && parts[0].start_with?(parts[2]) && parts[3] == '.rb'220 return given.to_s221 end222 # Case 4: Like case 3, but missing the .rb223 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something224 # parts index: ^0^ ^1^ ^2^ ^3^ (empty)225 if parts[0] =~ versioned_regex && parts[1] == 'lib' && parts[0].start_with?(parts[2]) && parts[3].empty?226 return given.to_s + '.rb'227 end228 # Case 5: Easy to recognize, but harder to handle: they referred to the project root.229 # /home/you/projects/inspec-something230 # parts index: ^0^ ^1^ ^2^ (3 is empty)231 # 0 and 1 are not meaningful to us, but we hope to find a parts[2]/lib/inspec-something.rb.232 entry_point_guess = File.join(given.to_s, 'lib', parts[2] + '.rb')233 if parts[2] =~ name_regex && File.exist?(entry_point_guess)234 return entry_point_guess235 end236 # Well, if we got here, parts[2] matches an inspec/train prefix, but we have no idea about anything.237 # Give up.238 puts(red { 'Unrecognizable plugin structure' } + " - #{parts[2]} - When installing from a path, please provide the path of the entry point file - installation failed.")239 exit 1240 end241 def install_from_path__probe_load(entry_point, plugin_name)242 # Brazenly attempt to load a file, and see if it registers a plugin.243 begin244 require entry_point245 rescue LoadError => ex246 puts(red { 'Plugin contains errors' } + " - #{plugin_name} - Encountered errors while trying to test load the plugin entry point, resolved to #{entry_point} - installation failed")247 puts ex.message248 exit 1249 end250 # OK, the wheels didn't fall off. But is it a plugin?251 if plugin_name.to_s.start_with?('train')252 # Train internal names do not include the prix in their registry entries253 # And the registry is keyed on Strings254 registry_key = plugin_name.to_s.sub(/^train-/, '')255 unless Train::Plugins.registry.key?(registry_key)256 puts(red { 'Does not appear to be a plugin' } + " - #{plugin_name} - After probe-loading the supposed plugin, it did not register itself to Train. Ensure something inherits from 'Train.plugin(1)' - installation failed.")257 exit 1258 end259 else260 unless registry.known_plugin?(plugin_name.to_sym)261 puts(red { 'Does not appear to be a plugin' } + " - #{plugin_name} - After probe-loading the supposed plugin, it did not register itself to InSpec. Ensure something inherits from 'Inspec.plugin(2)' - installation failed.")262 exit 1263 end264 end265 end266 def install_from_remote_gem(plugin_name)267 requested_version = options[:version]268 check_plugin_name(plugin_name, 'installation')269 # Version pre-flighting270 pre_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }271 install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)272 install_attempt_install(plugin_name)273 # Success messaging. What did we actually install?274 post_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }275 new_version = (post_installed_versions - pre_installed_versions).first276 puts(bold { plugin_name } + " plugin, version #{new_version}, installed from rubygems.org")277 exit 0278 end279 def install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)280 return if pre_installed_versions.empty?281 # Everything past here in the block is a code 2 error282 # If they didn't ask for a specific version, they implicitly ask for the latest.283 # Do an expensive search to determine the latest version.284 unless requested_version285 latest_version = installer.search(plugin_name, exact: true, scope: :latest)286 latest_version = latest_version[plugin_name]&.last287 if latest_version && !requested_version288 requested_version = latest_version289 end290 end291 # Check for already-installed at desired version conditions292 they_explicitly_asked_for_a_version = !options[:version].nil?293 what_we_would_install_is_already_installed = pre_installed_versions.include?(requested_version)294 if what_we_would_install_is_already_installed && they_explicitly_asked_for_a_version295 puts(red { 'Plugin already installed at requested version' } + " - plugin #{plugin_name} #{requested_version} - refusing to install.")296 elsif what_we_would_install_is_already_installed && !they_explicitly_asked_for_a_version297 puts(red { 'Plugin already installed at latest version' } + " - plugin #{plugin_name} #{requested_version} - refusing to install.")298 else299 # There are existing versions installed, but none of them are what was requested300 puts(red { 'Update required' } + " - plugin #{plugin_name}, requested #{requested_version}, have #{pre_installed_versions.join(', ')}; use `inspec plugin update` - refusing to install.")301 end302 exit 2303 end304 # Rationale for RuboCop variance: This is a one-line method with heavy UX-focused error handling.305 def install_attempt_install(plugin_name) # rubocop: disable Metrics/AbcSize306 installer.install(plugin_name, version: options[:version])307 rescue Inspec::Plugin::V2::PluginExcludedError => ex308 puts(red { 'Plugin on Exclusion List' } + " - #{plugin_name} is listed as an incompatible gem - refusing to install.")309 puts "Rationale: #{ex.details.rationale}"310 puts 'Exclusion list location: ' + File.join(Inspec.src_root, 'etc', 'plugin_filters.json')311 puts 'If you disagree with this determination, please accept our apologies for the misunderstanding, and open an issue at https://github.com/inspec/inspec/issues/new'312 exit 2313 rescue Inspec::Plugin::V2::InstallError314 raise if Inspec::Log.level == :debug315 results = installer.search(plugin_name, exact: true)316 if results.empty?317 puts(red { 'No such plugin gem ' } + plugin_name + ' could be found on rubygems.org - installation failed.')318 elsif options[:version] && !results[plugin_name].include?(options[:version])319 puts(red { 'No such version' } + ' - ' + plugin_name + " exists, but no such version #{options[:version]} found on rubygems.org - installation failed.")320 else321 puts(red { 'Unknown error occured ' } + ' - installation failed.')322 end323 exit 1324 end325 #==================================================================#326 # update breakdown327 #==================================================================#328 def update_preflight_check(plugin_name, pre_update_versions)329 if pre_update_versions.empty?330 # Check for path install331 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]332 if !status333 puts(red { 'No such plugin installed: ' } + "#{plugin_name} - update failed")334 exit 1335 elsif status.installation_type == :path336 puts(red { 'Cannot update path-based install: ' } + "#{plugin_name} is installed via path reference; use `inspec plugin uninstall` to remove - refusing to update")337 exit 2338 end339 end340 # Check for latest version (and implicitly, existance)341 latest_version = installer.search(plugin_name, exact: true, scope: :latest)342 latest_version = latest_version[plugin_name]&.last343 if pre_update_versions.include?(latest_version)344 puts(red { 'Already installed at latest version: ' } + "#{plugin_name} is at #{latest_version}, which the latest - refusing to update")345 exit 2346 end347 end348 #==================================================================#349 # utilities350 #==================================================================#351 def installer352 Inspec::Plugin::V2::Installer.instance353 end354 def registry355 Inspec::Plugin::V2::Registry.instance356 end357 def check_plugin_name(plugin_name, action)358 unless plugin_name =~ /^(inspec|train)-/359 puts(red { 'Invalid plugin name' } + " - #{plugin_name} - All inspec plugins must begin with either 'inspec-' or 'train-' - #{action} failed.")360 exit 1361 end362 end363 def make_pretty_version(status)364 case status.installation_type365 when :core, :bundle366 Inspec::VERSION367 when :gem368 # TODO: this is naive, and assumes the latest version is the one that will be used. Logged on #3317...

Full Screen

Full Screen

registry

Using AI Code Generation

copy

Full Screen

1 class PluginManagerCLI < Inspec.plugin(2, :cli_command)2 class PluginManagerCLI < Inspec.plugin(2, :cli_command)3 class PluginManagerCLI < Inspec.plugin(2, :cli_command)4 class PluginManagerCLI < Inspec.plugin(2, :cli_command)

Full Screen

Full Screen

registry

Using AI Code Generation

copy

Full Screen

1 @registry = {}2 def add_plugin(plugin)3 def get_plugin(plugin_name)4 @registry = {}5 def add_plugin(plugin)6 def get_plugin(plugin_name)7 @registry = {}8 def add_plugin(plugin)9 def get_plugin(plugin_name)10 @registry = {}11 def add_plugin(plugin)12 def get_plugin(plugin_name)13 @registry = {}14 def add_plugin(plugin)15 def get_plugin(plugin_name)

Full Screen

Full Screen

registry

Using AI Code Generation

copy

Full Screen

1registry.list.values.map(&:class)2registry.list.values.map(&:instance_variable_get, :@options)3registry.list.values.map(&:instance_variable_get, :@options).map(&:class)4registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq5registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq.map(&:name)6registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq.map(&:name).uniq7registry.list.values.map(&:class)8registry.list.values.map(&:instance_variable_get, :@options)9registry.list.values.map(&:instance_variable_get, :@options).map(&:class)10registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq11registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq.map(&:name)12registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq.map(&:name).uniq13registry.list.values.map(&:class)14registry.list.values.map(&:instance_variable_get, :@options)15registry.list.values.map(&:instance_variable_get, :@options).map(&:class)16registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq17registry.list.values.map(&:instance_variable_get, :@options).map(&:class).uniq.map(&:name)

Full Screen

Full Screen

registry

Using AI Code Generation

copy

Full Screen

1 class Plugin < Inspec.plugin(2, :cli_command)2 class Plugin < Inspec.plugin(2, :cli_command)3 class Plugin < Inspec.plugin(2, :cli_command)4 class Plugin < Inspec.plugin(2, :cli_command)5 class Plugin < Inspec.plugin(2, :cli_command)

Full Screen

Full Screen

registry

Using AI Code Generation

copy

Full Screen

1InspecPlugins::PluginManager.register_plugin(2InspecPlugins::PluginManager.register_plugin(3InspecPlugins::PluginManager.register_plugin(4InspecPlugins::PluginManager.register_plugin(5InspecPlugins::PluginManager.register_plugin(6InspecPlugins::PluginManager.register_plugin(7InspecPlugins::PluginManager.register_plugin(

Full Screen

Full Screen

registry

Using AI Code Generation

copy

Full Screen

1plugin = InspecPlugins::PluginManager.registry.get_plugin('inspec-test-fixture', '0.1.0')2InspecPlugins::PluginManager.registry.list_plugins_by_interface(:reporter)3plugin = InspecPlugins::PluginManager.registry.get_plugin_by_interface(:reporter, 'inspec-test-fixture', '0.1.0')

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful