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! 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..1f82451 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,25 @@ 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 + + exit + 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..f641041 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(nonsense_p_name).nil?.should be_true + end + end end