Skip to content

Commit 772ce89

Browse files
plugin manager: add --no-expand flag for list command (#17124) (#17170)
* plugin manager: add --no-expand flag for list command Allows us to avoid expanding aliases and integration plugins * spec: escape expected output in regexp (cherry picked from commit 793e8c0) Co-authored-by: Ry Biesemeyer <[email protected]>
1 parent 32463ef commit 772ce89

File tree

4 files changed

+126
-12
lines changed

4 files changed

+126
-12
lines changed

lib/pluginmanager/list.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class LogStash::PluginManager::List < LogStash::PluginManager::Command
2323
parameter "[PLUGIN]", "Part of plugin name to search for, leave empty for all plugins"
2424

2525
option "--installed", :flag, "List only explicitly installed plugins using bin/logstash-plugin install ...", :default => false
26+
option "--[no-]expand", :flag, "Expand integration plugins and aliases", :default => true
2627
option "--verbose", :flag, "Also show plugin version number", :default => false
2728
option "--group", "NAME", "Filter plugins per group: input, output, filter, codec or integration" do |arg|
2829
raise(ArgumentError, "should be one of: input, output, filter, codec, integration") unless ['input', 'output', 'filter', 'codec', 'pack', 'integration'].include?(arg)
@@ -40,23 +41,29 @@ def execute
4041
line = "#{spec.name}"
4142
line += " (#{spec.version})" if verbose?
4243
puts(line)
43-
if LogStash::PluginManager::ALIASES.has_value?(spec.name)
44-
alias_plugin = LogStash::PluginManager::ALIASES.key(spec.name)
45-
puts("└── #{alias_plugin} (alias)") unless installed_plugin_names.include?(alias_plugin)
46-
end
47-
if spec.metadata.fetch("logstash_group", "") == "integration"
48-
integration_plugins = spec.metadata.fetch("integration_plugins", "").split(",")
49-
integration_plugins.each_with_index do |integration_plugin, i|
50-
if i == integration_plugins.size - 1
51-
puts(" └── #{integration_plugin}")
52-
else
53-
puts(" ├── #{integration_plugin}")
54-
end
44+
if expand?
45+
active_aliases = LogStash::PluginManager.find_aliases(spec.name)
46+
.reject {|alias_name| installed_plugin_names.include?(alias_name)}
47+
display_children(active_aliases.map {|alias_name| "#{alias_name} (alias)"})
48+
49+
if spec.metadata.fetch("logstash_group", "") == "integration"
50+
integration_plugins = spec.metadata.fetch("integration_plugins", "").split(",")
51+
display_children(integration_plugins)
5552
end
5653
end
5754
end
5855
end
5956

57+
def display_children(children)
58+
if children.any?
59+
most, last = children[0...-1], children[-1]
60+
most.each do |entry|
61+
puts(" ├── #{entry}")
62+
end
63+
puts(" └── #{last}")
64+
end
65+
end
66+
6067
def filtered_specs
6168
@filtered_specs ||= begin
6269
# start with all locally installed plugin gems regardless of the Gemfile content

lib/pluginmanager/util.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ def self.load_aliases_definitions(path = File.expand_path('plugin_aliases.yml',
4747
# Defines the plugin alias, must be kept in synch with Java class org.logstash.plugins.AliasRegistry
4848
ALIASES = load_aliases_definitions()
4949

50+
def self.resolve_alias(alias_name)
51+
ALIASES[alias_name]
52+
end
53+
54+
def self.find_aliases(plugin_name)
55+
ALIASES.each_with_object([]) do |(alias_name, candidate_plugin_name), aliases|
56+
aliases << alias_name if candidate_plugin_name == plugin_name
57+
end.compact.uniq
58+
end
59+
5060
class ValidationError < StandardError; end
5161

5262
# check for valid logstash plugin gem name & version or .gem file, logs errors to $stdout
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
services:
3+
- logstash
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require_relative '../../framework/fixture'
19+
require_relative '../../framework/settings'
20+
require_relative '../../services/logstash_service'
21+
require_relative '../../framework/helpers'
22+
require "logstash/devutils/rspec/spec_helper"
23+
24+
describe "CLI > logstash-plugin remove" do
25+
before(:all) do
26+
@fixture = Fixture.new(__FILE__)
27+
@logstash_plugin = @fixture.get_service("logstash").plugin_cli
28+
end
29+
30+
context "listing plugins" do
31+
32+
let(:sub_heading_pattern) do
33+
Regexp.union(" ├──"," └──")
34+
end
35+
36+
let(:version_pattern) do
37+
/[0-9]+[.][0-9][0-9a-z.]+/
38+
end
39+
40+
def parse_output(output)
41+
output.split(/\n(?! )/)
42+
end
43+
44+
context "--verbose" do
45+
it "successfully lists a single plugin" do
46+
list_command = @logstash_plugin.run("list --verbose logstash-integration-jdbc")
47+
expect(list_command.exit_code).to eq(0)
48+
expect(list_command.stderr_and_stdout).to match(/^logstash-integration-jdbc [(]#{version_pattern}[)]/)
49+
end
50+
it "successfully lists all plugins" do
51+
list_command = @logstash_plugin.run("list --verbose")
52+
expect(list_command.exit_code).to eq(0)
53+
expect(list_command.stderr_and_stdout).to match(/^logstash-integration-jdbc [(]#{version_pattern}[)]/)
54+
expect(list_command.stderr_and_stdout).to match(/^logstash-input-beats [(]#{version_pattern}[)]/)
55+
expect(list_command.stderr_and_stdout).to match(/^logstash-output-elasticsearch [(]#{version_pattern}[)]/)
56+
end
57+
end
58+
59+
it "expands integration plugins" do
60+
list_command = @logstash_plugin.run("list logstash-integration-jdbc")
61+
expect(list_command.exit_code).to eq(0)
62+
plugins = list_command.stderr_and_stdout.split(/\n(?! )/)
63+
64+
integration_plugin = plugins.find { |plugin_output| plugin_output.match(/^logstash-integration-jdbc\b/) }
65+
expect(integration_plugin).to match(/^#{sub_heading_pattern} #{Regexp.escape("logstash-input-jdbc")}$/)
66+
expect(integration_plugin).to match(/^#{sub_heading_pattern} #{Regexp.escape("logstash-filter-jdbc_static")}$/)
67+
end
68+
69+
it "expands plugin aliases" do
70+
list_command = @logstash_plugin.run("list logstash-input-beats")
71+
expect(list_command.exit_code).to eq(0)
72+
plugins = list_command.stderr_and_stdout.split(/\n(?! )/)
73+
74+
alias_plugin = plugins.find { |plugin_output| plugin_output.match(/^logstash-input-beats\b/) }
75+
expect(alias_plugin).to match(/^#{sub_heading_pattern} #{Regexp.escape("logstash-input-elastic_agent (alias)")}$/)
76+
end
77+
78+
context "--no-expand" do
79+
it "does not expand integration plugins" do
80+
list_command = @logstash_plugin.run("list --no-expand")
81+
expect(list_command.exit_code).to eq(0)
82+
expect(list_command.stderr_and_stdout).to match(/^logstash-integration-jdbc\b/)
83+
expect(list_command.stderr_and_stdout).to_not include("logstash-input-jdbc") # known integrated plugin
84+
expect(list_command.stderr_and_stdout).to_not include("logstash-filter-jdbc") # known integrated plugin
85+
end
86+
it "does not expand plugin aliases" do
87+
list_command = @logstash_plugin.run("list --no-expand")
88+
expect(list_command.exit_code).to eq(0)
89+
expect(list_command.stderr_and_stdout).to match(/^logstash-input-beats\b/)
90+
expect(list_command.stderr_and_stdout).to_not include("logstash-input-elastic_agent") # known alias
91+
end
92+
end
93+
end
94+
end

0 commit comments

Comments
 (0)