Best Inspec_ruby code snippet using InspecPlugins.PluginManager.make_pretty_version
cli_command.rb
Source:cli_command.rb
...19 puts20 puts(bold { format(' %-30s%-10s%-8s%-6s', 'Plugin Name', 'Version', 'Via', 'ApiVer') })21 puts '-' * 5522 plugin_statuses.sort_by(&:name).each do |status|23 puts(format(' %-30s%-10s%-8s%-6s', status.name, make_pretty_version(status), status.installation_type, status.api_generation.to_s))24 end25 puts '-' * 5526 puts(" #{plugin_statuses.count} plugin(s) total")27 puts28 end29 #==================================================================#30 # inspec plugin search31 #==================================================================#32 desc 'search [options] PATTERN', 'Searches rubygems.org for plugins.'33 long_desc <<~EOLD34 Searches rubygems.org for InSpec plugins. Exits 0 on a search hit, 1 on user error,35 2 on a search miss. PATTERN is a simple string; a wildcard will be added as36 a suffix, unless -e is used.37 EOLD38 option :all, desc: 'List all available versions, not just the latest one.', type: :boolean, aliases: [:a]39 option :exact, desc: 'Assume PATTERN is exact; do not add a wildcard to the end', type: :boolean, aliases: [:e]40 option :'include-test-fixture', type: :boolean, desc: 'Internal use', hide: true41 # Justification for disabling ABC: currently at 33.51/3342 def search(search_term) # rubocop: disable Metrics/AbcSize43 search_results = installer.search(search_term, exact: options[:exact])44 # The search results have already been filtered by the reject list. But the45 # RejectList doesn't filter {inspec, train}-test-fixture because we need those46 # for testing. We want to hide those from users, so unless we know we're in47 # test mode, remove them.48 unless options[:'include-test-fixture']49 search_results.delete('inspec-test-fixture')50 search_results.delete('train-test-fixture')51 end52 # TODO: ui object support53 puts54 puts(bold { format(' %-30s%-50s', 'Plugin Name', 'Versions Available') })55 puts '-' * 5556 search_results.keys.sort.each do |plugin_name|57 versions = options[:all] ? search_results[plugin_name] : [search_results[plugin_name].first]58 versions = '(' + versions.join(', ') + ')'59 puts(format(' %-30s%-50s', plugin_name, versions))60 end61 puts '-' * 5562 puts(" #{search_results.count} plugin(s) found")63 puts64 exit 2 if search_results.empty?65 rescue Inspec::Plugin::V2::SearchError => ex66 Inspec::Log.error ex.message67 exit 168 end69 #==================================================================#70 # inspec plugin install71 #==================================================================#72 desc 'install [-v VERSION] PLUGIN', 'Installs a plugin from rubygems.org, a gemfile, or a path to local source.'73 long_desc <<~EOLD74 PLUGIN may be the name of a gem on rubygems.org that begins with inspec- or train-.75 PLUGIN may also be the path to a local gemfile, which will then be installed like76 any other gem. Finally, if PLUGIN is a path ending in .rb, it is taken to be a77 local file that will act as athe entry point for a plugin (this mode is provided78 for local plugin development). Exit codes are 0 on success, 2 if the plugin is79 already installed, and 1 if any other error occurs.80 EOLD81 option :version, desc: 'When installing from rubygems.org, specifies a specific version to install.', aliases: [:v]82 def install(plugin_id_arg)83 if plugin_id_arg =~ /\.gem$/ # Does it end in .gem?84 install_from_gemfile(plugin_id_arg)85 elsif plugin_id_arg =~ %r{[\/\\]} || Dir.exist?(plugin_id_arg) # Does the argument have a slash, or exist as dir in the local directory?86 install_from_path(plugin_id_arg)87 else88 install_from_remote_gem(plugin_id_arg)89 end90 end91 #--------------------------92 # update93 #--------------------------94 desc 'update PLUGIN', 'Updates a plugin to the latest from from rubygems.org'95 long_desc <<~EOLD96 PLUGIN may be the name of a gem on rubygems.org that begins with inspec- or train-.97 Exit codes are 0 on success, 2 if the plugin is already up to date, and 1 if any98 other error occurs.99 EOLD100 def update(plugin_name)101 pre_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }102 old_version = pre_update_versions.join(', ')103 update_preflight_check(plugin_name, pre_update_versions)104 begin105 installer.update(plugin_name)106 rescue Inspec::Plugin::V2::UpdateError => ex107 puts(red { 'Update error: ' } + ex.message + ' - update failed')108 exit 1109 end110 post_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }111 new_version = (post_update_versions - pre_update_versions).first112 puts(bold { plugin_name } + " plugin, version #{old_version} -> #{new_version}, updated from rubygems.org")113 end114 #--------------------------115 # uninstall116 #--------------------------117 desc 'uninstall PLUGIN_NAME', 'Uninstalls a gem- or path- based plugin'118 long_desc <<~EOLD119 Removes a plugin from the users configuration.120 In the case of a gem plugin (by far the most common), the plugin gem is removed, along121 with any of its dependencies that are no longer needed by anything else. Finally, the122 plugin configuration file is updated to reflect that the plugin is no longer present.123 In the case of a path-based plugin (often used for plugin development), no changes124 are made to the referenced plugin source code. Rather, the plugin's entry is simply removed125 from the plugin config file.126 EOLD127 def uninstall(plugin_name)128 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]129 unless status130 puts(red { 'No such plugin installed: ' } + "#{plugin_name} is not installed - uninstall failed")131 exit 1132 end133 installer = Inspec::Plugin::V2::Installer.instance134 pre_uninstall_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }135 old_version = pre_uninstall_versions.join(', ')136 installer.uninstall(plugin_name)137 if status.installation_type == :path138 puts(bold { plugin_name } + ' path-based plugin install has been uninstalled')139 else140 puts(bold { plugin_name } + " plugin, version #{old_version}, has been uninstalled")141 end142 exit 0143 end144 private145 #==================================================================#146 # install breakdown147 #==================================================================#148 # These are broken down because rubocop complained.149 def install_from_gemfile(gem_file)150 unless File.exist? gem_file151 puts(red { 'No such plugin gem file ' } + gem_file + ' - installation failed.')152 exit 1153 end154 plugin_name_parts = File.basename(gem_file, '.gem').split('-')155 version = plugin_name_parts.pop156 plugin_name = plugin_name_parts.join('-')157 check_plugin_name(plugin_name, 'installation')158 installer.install(plugin_name, gem_file: gem_file)159 puts(bold { plugin_name } + " plugin, version #{version}, installed from local .gem file")160 exit 0161 end162 def install_from_path(path)163 unless File.exist? path164 puts(red { 'No such source code path ' } + path + ' - installation failed.')165 exit 1166 end167 plugin_name = File.basename(path, '.rb')168 # While installer.install does some rudimentary checking,169 # this file has good UI access, so we promise to validate the170 # 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 #3317369 # In fact, the logic to determine "what version would be used" belongs in the Loader.370 Inspec::Plugin::V2::Loader.list_installed_plugin_gems371 .select { |spec| spec.name == status.name.to_s }372 .sort_by(&:version)373 .last.version374 when :path375 'src'376 end377 end...
make_pretty_version
Using AI Code Generation
1InspecPlugins::PluginManager.make_pretty_version('1.2.3')2InspecPlugins::PluginManager.make_pretty_version('1.2.3')3InspecPlugins::PluginManager::CLI.make_pretty_version('1.2.3')4InspecPlugins::PluginManager::CLI::Install.make_pretty_version('1.2.3')5InspecPlugins::PluginManager::CLI::Uninstall.make_pretty_version('1.2.3')6InspecPlugins::PluginManager::CLI::Update.make_pretty_version('1.2.3')7InspecPlugins::PluginManager::CLI::List.make_pretty_version('1.2.3')8InspecPlugins::PluginManager::CLI::Search.make_pretty_version('1.2.3')9InspecPlugins::PluginManager::CLI::Version.make_pretty_version('1.2.3')
make_pretty_version
Using AI Code Generation
1InspecPlugins::PluginManager.make_pretty_version('1.2.3')2InspecPlugins::PluginManager::Util.make_pretty_version('1.2.3')3InspecPlugins::PluginManager::Util.make_pretty_version('1.2.3')4InspecPlugins::PluginManager.make_pretty_version('1.2.3')5InspecPlugins::PluginManager::Util.make_pretty_version('1.2.3')6InspecPlugins::PluginManager.make_pretty_version('1.2.3')7InspecPlugins::PluginManager::Util.make_pretty_version('1.2.3')8InspecPlugins::PluginManager.make_pretty_version('1.2.3')9InspecPlugins::PluginManager::Util.make_pretty_version('1.2.3')
make_pretty_version
Using AI Code Generation
1 def make_pretty_version(version)2 version = version.split('.')3 version.join('.')4 def make_pretty_version(version)5 version = version.split('.')6 version.join('.')7 def make_pretty_version(version)8 version = version.split('.')9 version.join('.')10 def make_pretty_version(version)11 version = version.split('.')12 version.join('.')13 def make_pretty_version(version)14 version = version.split('.')15 version.join('.')
make_pretty_version
Using AI Code Generation
1InspecPlugins::PluginManager.make_pretty_version("1.2.3")2InspecPlugins::PluginManager::CLI.make_pretty_version("1.2.3")3InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")4InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")5InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")6InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")7InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")8InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")
make_pretty_version
Using AI Code Generation
1InspecPlugins::PluginManager.make_pretty_version('1.2.3')2 def self.make_pretty_version(version)3 def self.make_pretty_version(version)4 def self.make_pretty_version(version)5 def self.make_pretty_version(version)6 def self.make_pretty_version(version)7 def self.make_pretty_version(version)8 def self.make_pretty_version(version)
make_pretty_version
Using AI Code Generation
1InspecPlugins::PluginManager.make_pretty_version('1.2.3')2InspecPlugins::PluginManager.make_pretty_version('1.2.3')3InspecPlugins::PluginManager::CLI.make_pretty_version('1.2.3')4InspecPlugins::PluginManager::CLI::Install.make_pretty_version('1.2.3')5InspecPlugins::PluginManager::CLI::Uninstall.make_pretty_version('1.2.3')6InspecPlugins::PluginManager::CLI::Update.make_pretty_version('1.2.3')7InspecPlugins::PluginManager::CLI::List.make_pretty_version('1.2.3')8InspecPlugins::PluginManager::CLI::Search.make_pretty_version('1.2.3')9InspecPlugins::PluginManager::CLI::Version.make_pretty_version('1.2.3')
make_pretty_version
Using AI Code Generation
1InspecPlugins::PluginManager.make_pretty_version("1.2.3")2InspecPlugins::PluginManager::CLI.make_pretty_version("1.2.3")3InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")4InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")5InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")6InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")7InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")8InspecPlugins::PluginManager::CLI.new.make_pretty_version("1.2.3")
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!