Can check for process by name

pull/4/head
Braydon Kains 5 years ago
parent 8aee1e9788
commit b9cb6b6667

@ -15,4 +15,8 @@ class LtnpOperator
def get_port_record(port : String) : LtnpRecord | Nil
@ltnp_records.find { |r| r.port == port }
end
def search_p_name(p_name : String) : LtnpRecord | Nil
@ltnp_records.find { |r| r.p_name == p_name }
end
end

@ -27,6 +27,10 @@ class LtnpRecord
]
end
def out_s
"Port: %s, Process: %s, Process ID: %s" % [self.port, self.p_name, self.pid]
end
def ser_address_port(address_port : Array) : {String, String}
address = String.new
port = String.new

@ -20,7 +20,7 @@ OptionParser.parse do |parser|
record = ltnp_operator.get_port_record port
unless record.nil?
puts "Port: %s, Process: %s, Process ID: %s" % [record.port, record.p_name, record.pid]
puts record.out_s
else
puts "No processes found on port %s" % port
end
@ -28,12 +28,24 @@ OptionParser.parse do |parser|
exit
end
parser.on "-p PNAME", "--find-process=PNAME", "Find the port and ID of a process by name" do |p_name|
ltnp_operator = CommandRunner.run_ltnp
record = ltnp_operator.search_p_name p_name
unless record.nil?
puts record.out_s
else
puts "No process found by the name %s" % p_name
end
end
parser.on "-k PORT", "--kill-port=PORT", "Kill process on port" do |port|
ltnp_operator = CommandRunner.run_ltnp
record = ltnp_operator.get_port_record port
unless record.nil?
puts "Port: %s, Process: %s, Process ID: %s" % [record.port, record.p_name, record.pid]
puts record.out_s
puts "Would you like to kill %s? (y/n)" % [record.p_name]
input = gets
unless input.nil?

@ -16,4 +16,17 @@ describe LtnpOperator do
sut.get_port_record(nonsense_port).nil?.should be_true
end
end
describe "#search_p_name" do
existing_p_name = "polymer"
nonsense_p_name = "thisisnotathing"
it "should find record when process exists" do
sut.search_p_name(existing_p_name).nil?.should be_false
end
it "should not find record when process doesn't exist" do
sut.search_p_name(existing_p_name).nil?.should be_false
end
end
end

Loading…
Cancel
Save