Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the ability to run multiple commands #1752

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 22 additions & 59 deletions lib/logstash/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ def wait
class LogStash::Runner
include LogStash::Program

def initialize
@runners = []
end

def main(args)
require "logstash/util"
require "stud/trap"
Expand All @@ -89,24 +85,8 @@ def main(args)

Stud::untrap("INT", @startup_interruption_trap)

args = [nil] if args.empty?

while args != nil && !args.empty?
args = run(args)
end

status = []
@runners.each do |r|
#$stderr.puts "Waiting on #{r.wait.inspect}"
status << r.wait
end

# Avoid running test/unit's at_exit crap
if status.empty? || status.first.nil?
exit(0)
else
exit(status.first)
end
task = run(args)
exit(task.wait)
end # def self.main

def run(args)
Expand All @@ -118,14 +98,12 @@ def run(args)
if args.include?("--verbose")
agent_args << "--verbose"
end
LogStash::Agent.run($0, agent_args)
return []
return LogStash::Agent.run($0, agent_args)
end,
"web" => lambda do
# Give them kibana.
require "logstash/kibana"
kibana = LogStash::Kibana::Runner.new
@runners << kibana
return kibana.run(args)
end,
"rspec" => lambda do
Expand All @@ -136,18 +114,11 @@ def run(args)
require "test_utils"
all_specs = Dir.glob(File.join(spec_path, "/**/*.rb"))
rspec = LogStash::RSpecsRunner.new(args.empty? ? all_specs : args)
rspec.run
@runners << rspec
return []
return rspec.run
end,
"irb" => lambda do
require "irb"
IRB.start(__FILE__)
return []
end,
"ruby" => lambda do
require(args[0])
return []
return IRB.start(__FILE__)
end,
"pry" => lambda do
require "pry"
Expand All @@ -158,17 +129,11 @@ def run(args)
plugin_manager = LogStash::PluginManager::Main.new($0)
begin
plugin_manager.parse(args)
return plugin_manager.execute
rescue Clamp::HelpWanted => e
show_help(e.command)
return 0
end

begin
plugin_manager.execute
rescue Clamp::HelpWanted => e
show_help(e.command)
end

return []
end,
"agent" => lambda do
require "logstash/agent"
Expand All @@ -178,21 +143,20 @@ def run(args)
agent.parse(args)
rescue Clamp::HelpWanted => e
show_help(e.command)
return []
return 0
rescue Clamp::UsageError => e
# If 'too many arguments' then give the arguments to
# the next command. Otherwise it's a real error.
raise if e.message != "too many arguments"
remaining = agent.remaining_arguments
end
@runners << Stud::Task.new { agent.execute }

return remaining
return agent.execute
end
} # commands

if commands.include?(command)
args = commands[command].call
return Stud::Task.new { commands[command].call }
else
if command.nil?
$stderr.puts "No command given"
Expand All @@ -202,21 +166,20 @@ def run(args)
$stderr.puts "No such command #{command.inspect}"
end
end
$stderr.puts "Usage: logstash <command> [command args]"
$stderr.puts "Run a command with the --help flag to see the arguments."
$stderr.puts "For example: logstash agent --help"
$stderr.puts
# hardcode the available commands to reduce confusion.
$stderr.puts "Available commands:"
$stderr.puts " agent - runs the logstash agent"
$stderr.puts " version - emits version info about this logstash"
$stderr.puts " web - runs the logstash web ui (called Kibana)"
$stderr.puts " rspec - runs tests"
$stderr.puts %q[
Usage: logstash <command> [command args]
Run a command with the --help flag to see the arguments.
For example: logstash agent --help

Available commands:
agent - runs the logstash agent
version - emits version info about this logstash
web - runs the logstash web ui (called Kibana)
rspec - runs tests
]
#$stderr.puts commands.keys.map { |s| " #{s}" }.join("\n")
exit 1
return Stud::Task.new { 1 }
end

return args
end # def run

# @return true if this file is the main file being run and not via rspec
Expand Down
26 changes: 19 additions & 7 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,36 @@ def run(args); end
it "should run agent help" do
expect(subject).to receive(:show_help).once.and_return(nil)
args = ["agent", "-h"]
expect(subject.run(args)).to eq([])
expect(subject.run(args).wait).to eq(0)
end

it "should show help with no arguments" do
expect($stderr).to receive(:puts).once.and_return("No command given")
expect($stderr).to receive(:puts).once
args = []
expect(subject.run(args).wait).to eq(1)
end

it "should show help for unknown commands" do
expect($stderr).to receive(:puts).once.and_return("No such command welp")
expect($stderr).to receive(:puts).once
args = ["welp"]
expect(subject.run(args).wait).to eq(1)
end

it "should run agent help and not run following commands" do
expect(subject).to receive(:show_help).once.and_return(nil)
args = ["agent", "-h", "web"]
expect(subject.run(args)).to eq([])
expect(subject.run(args).wait).to eq(0)
end

it "should run agent and web" do
it "should not run agent and web" do
expect(Stud::Task).to receive(:new).once
args = ["agent", "-e", "", "web"]
args = subject.run(args)
expect(args).to eq(["web"])

expect(LogStash::Kibana::Runner).to receive(:new).once.and_return(NullRunner.new)
args = subject.run(args)
expect(args).to eq(nil)

expect(LogStash::Kibana::Runner).to_not receive(:new)
end
end
end