From b9cb6b66679a32af881268cbf4b243ccb40a66f3 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Mon, 11 May 2020 23:06:00 -0400 Subject: [PATCH 1/4] 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 From 320c435ec255447f67dc2640b9e8b6c2fad0e721 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Mon, 11 May 2020 23:15:43 -0400 Subject: [PATCH 2/4] Format --- main.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/main.cr b/main.cr index 8bc9be9..af3590a 100644 --- a/main.cr +++ b/main.cr @@ -37,7 +37,6 @@ OptionParser.parse do |parser| 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| From d48d331e81745f1dfeb0835a7f9f37806d1655b8 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Mon, 11 May 2020 23:18:12 -0400 Subject: [PATCH 3/4] Putting the exit where it belongs --- main.cr | 2 ++ spec/ltnp/ltnp_operator_spec.cr | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/main.cr b/main.cr index af3590a..1f82451 100644 --- a/main.cr +++ b/main.cr @@ -37,6 +37,8 @@ OptionParser.parse do |parser| else puts "No process found by the name %s" % p_name end + + exit end parser.on "-k PORT", "--kill-port=PORT", "Kill process on port" do |port| diff --git a/spec/ltnp/ltnp_operator_spec.cr b/spec/ltnp/ltnp_operator_spec.cr index 281ef10..f641041 100644 --- a/spec/ltnp/ltnp_operator_spec.cr +++ b/spec/ltnp/ltnp_operator_spec.cr @@ -26,7 +26,7 @@ describe LtnpOperator do end it "should not find record when process doesn't exist" do - sut.search_p_name(existing_p_name).nil?.should be_false + sut.search_p_name(nonsense_p_name).nil?.should be_true end end end From 105e81c71e27b982c43a934ac72f70956933072c Mon Sep 17 00:00:00 2001 From: BraydonKains Date: Mon, 11 May 2020 23:21:25 -0400 Subject: [PATCH 4/4] README update --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b4b5d5d..3b70963 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,15 @@ Get the name and process ID of the process running on a given port Check the process running on a given port with the option to kill it if you'd like. ## TODO -* [x] Add the ability to kill process on port -* [] Maybe look into other cli operations? (getting IP?) -* [] Come up with other useful stuff -* [] Write up contributors guide -* [] Decide how to do documentation (probably Github wiki?) -* [] Write man page (figure out how to do that lol) -* [] Figure out distribution/release -* [x] Set up CI (probably Travis) +[x] Add the ability to kill process on port +[x] Add the ability to search for process by name +[] Maybe look into other cli operations? (getting IP?) +[] Come up with other useful stuff +[] Write up contributors guide +[] Decide how to do documentation (probably Github wiki?) +[] Write man page (figure out how to do that lol) +[] Figure out distribution/release +[x] Set up CI (probably Travis) ## Contributing I don't know that I'm ready for contributors yet because I don't have a good contributors guide. I do plan to eventually open this up to contributors. If you have any ideas for future operations, please feel free to open an issue, I am happy to discuss ideas!