From b9cb6b66679a32af881268cbf4b243ccb40a66f3 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Mon, 11 May 2020 23:06:00 -0400 Subject: [PATCH] Can check for process by name --- ltnp/ltnp_operator.cr | 4 ++++ ltnp/ltnp_record.cr | 4 ++++ main.cr | 16 ++++++++++++++-- spec/ltnp/ltnp_operator_spec.cr | 13 +++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ltnp/ltnp_operator.cr b/ltnp/ltnp_operator.cr index 7dd0b92..3088862 100644 --- a/ltnp/ltnp_operator.cr +++ b/ltnp/ltnp_operator.cr @@ -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 diff --git a/ltnp/ltnp_record.cr b/ltnp/ltnp_record.cr index dfb5736..a895af3 100644 --- a/ltnp/ltnp_record.cr +++ b/ltnp/ltnp_record.cr @@ -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 diff --git a/main.cr b/main.cr index 7f1a12f..8bc9be9 100644 --- a/main.cr +++ b/main.cr @@ -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? diff --git a/spec/ltnp/ltnp_operator_spec.cr b/spec/ltnp/ltnp_operator_spec.cr index a8a4c48..281ef10 100644 --- a/spec/ltnp/ltnp_operator_spec.cr +++ b/spec/ltnp/ltnp_operator_spec.cr @@ -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