Class: InspecPlugins::PluginManager::CliCommand

Inherits:
Object
  • Object
show all
Includes:
Inspec::Dist
Defined in:
lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb

Constant Summary collapse

INSTALL_TYPE_LABELS =
{
  bundle: "core", # Calling this core, too - not much of a distinction
  core: "core",
  path: "path",
  user_gem: "gem (user)",
  system_gem: "gem (system)",
}.freeze

Constants included from Inspec::Dist

Inspec::Dist::AUTOMATE_PRODUCT_NAME, Inspec::Dist::COMPLIANCE_PRODUCT_NAME, Inspec::Dist::EXEC_NAME, Inspec::Dist::PRODUCT_NAME, Inspec::Dist::SERVER_PRODUCT_NAME

Instance Method Summary collapse

Instance Method Details

#install(plugin_id_arg) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb', line 119

def install(plugin_id_arg)
  if plugin_id_arg =~ /\.gem$/ # Does it end in .gem?
    install_from_gemfile(plugin_id_arg)
  elsif plugin_id_arg =~ %r{[\/\\]} || Dir.exist?(plugin_id_arg) # Does the argument have a slash, or exist as dir in the local directory?
    install_from_path(plugin_id_arg)
  else
    install_from_remote_gem(plugin_id_arg)
  end
end

#listObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb', line 31

def list
  plugin_statuses = Inspec::Plugin::V2::Registry.instance.plugin_statuses
  options[:all] = false if options[:core] || options[:user] || options[:system]
  plugin_statuses.select! do |status|
    type = status.installation_type
    options[:all] ||
      (options[:core] && %i{core bundle}.include?(type)) ||
      (options[:user] && %i{user_gem path}.include?(type)) ||
      (options[:system] && :system_gem == type)
  end

  unless plugin_statuses.empty?
    ui.table do |t|
      t.header = ["Plugin Name", "Version", "Via", "ApiVer", "Description"]
      plugin_statuses.sort_by { |s| s.name.to_s }.each do |status|
        t << [
          status.name,
          make_pretty_version(status),
          make_pretty_install_type(status),
          status.api_generation,
          status.description,
        ]
      end
    end
  end
  ui.plain_line(" #{plugin_statuses.count} plugin(s) total")
  puts
end

#search(search_term) ⇒ Object

Justification for disabling ABC: currently at 33.51/33



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb', line 75

def search(search_term) # rubocop: disable Metrics/AbcSize
  search_results = installer.search(search_term, exact: options[:exact], source: options[:source])
  # The search results have already been filtered by the reject list.  But the
  # RejectList doesn't filter {inspec, train}-test-fixture because we need those
  # for testing.  We want to hide those from users, so unless we know we're in
  # test mode, remove them.
  unless options[:'include-test-fixture']
    search_results.delete("inspec-test-fixture")
    search_results.delete("train-test-fixture")
  end

  puts
  ui.bold(format(" %-30s%-30s%-20s\n", "Plugin Name", "Versions Available", "Description"))
  ui.line_with_width(100)
  search_results.keys.sort.each do |plugin_name|
    versions = options[:all] ? search_results[plugin_name]["versions"] : [search_results[plugin_name]["versions"].first]
    versions = "(" + versions.join(", ") + ")"
    description = search_results[plugin_name]["description"]
    ui.plain_line(format(" %-30s%-30s%-20s", plugin_name, versions, description))
  end
  ui.line_with_width(100)
  ui.plain_line(" #{search_results.count} plugin(s) found")
  puts

  ui.exit Inspec::UI::EXIT_PLUGIN_ERROR if search_results.empty?
rescue Inspec::Plugin::V2::SearchError => ex
  Inspec::Log.error ex.message
  ui.exit Inspec::UI::EXIT_USAGE_ERROR
end

#uninstall(plugin_name) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb', line 170

def uninstall(plugin_name)
  status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]
  unless status
    ui.plain_line("#{ui.red("No such plugin installed:", print: false)} #{plugin_name} is not " \
           "installed - uninstall failed")
    ui.exit Inspec::UI::EXIT_USAGE_ERROR
  end
  installer = Inspec::Plugin::V2::Installer.instance

  pre_uninstall_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }
  old_version = pre_uninstall_versions.join(", ")

  installer.uninstall(plugin_name)

  if status.installation_type == :path
    ui.bold(plugin_name + " path-based plugin install has been " \
            "uninstalled\n")
  else
    ui.bold(plugin_name + " plugin, version #{old_version}, has " \
            "been uninstalled\n")
  end

  ui.exit Inspec::UI::EXIT_NORMAL
end

#update(plugin_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb', line 138

def update(plugin_name)
  pre_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }
  old_version = pre_update_versions.join(", ")

  update_preflight_check(plugin_name, pre_update_versions)

  begin
    installer.update(plugin_name)
  rescue Inspec::Plugin::V2::UpdateError => ex
    ui.plain_line("#{ui.red("Update error:", print: false)} #{ex.message} - update failed")
    ui.exit Inspec::UI::EXIT_USAGE_ERROR
  end
  post_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }
  new_version = (post_update_versions - pre_update_versions).first

  ui.bold(plugin_name + " plugin, version #{old_version} -> " \
          "#{new_version}, updated from rubygems.org\n")
end