From 6ba3c67a433138b6580578729aa1c728bd8af345 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Thu, 14 May 2020 22:03:43 -0400 Subject: [PATCH 1/2] Structural refactor, defers responsibility for actions to operators, organizing code differently --- ltnp/ltnp_operator.cr | 22 ------ main.cr | 41 ++---------- modules/command_runner.cr | 4 +- operators/ltnp_operator.cr | 74 +++++++++++++++++++++ {ltnp => records}/ltnp_record.cr | 2 +- spec/ltnp/ltnp_operator_spec.cr | 32 --------- spec/ltnp/operators/ltnp_operator_spec.cr | 52 +++++++++++++++ spec/ltnp/{ => records}/ltnp_record_spec.cr | 2 +- 8 files changed, 136 insertions(+), 93 deletions(-) delete mode 100644 ltnp/ltnp_operator.cr create mode 100644 operators/ltnp_operator.cr rename {ltnp => records}/ltnp_record.cr (98%) delete mode 100644 spec/ltnp/ltnp_operator_spec.cr create mode 100644 spec/ltnp/operators/ltnp_operator_spec.cr rename spec/ltnp/{ => records}/ltnp_record_spec.cr (96%) diff --git a/ltnp/ltnp_operator.cr b/ltnp/ltnp_operator.cr deleted file mode 100644 index 3088862..0000000 --- a/ltnp/ltnp_operator.cr +++ /dev/null @@ -1,22 +0,0 @@ -require "./ltnp_record" - -class LtnpOperator - def initialize(cmd_output : String) - records = cmd_output.each_line - records = records.each.select(/^tcp/) - @ltnp_records = Array(LtnpRecord).new - records.each { |s| @ltnp_records << LtnpRecord.new s } - end - - def say_hi - @ltnp_records.each { |r| puts r.to_s } - end - - 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/main.cr b/main.cr index 1f82451..66fb5f7 100644 --- a/main.cr +++ b/main.cr @@ -1,6 +1,5 @@ require "option_parser" require "./ltnp/ltnp_operator" -require "./modules/command_runner" OptionParser.parse do |parser| parser.banner = "Welcome to eznetstat!" @@ -16,48 +15,20 @@ OptionParser.parse do |parser| end parser.on "-c PORT", "--check-port=PORT", "Get the process name and ID of process on port" do |port| - ltnp_operator = CommandRunner.run_ltnp - record = ltnp_operator.get_port_record port - - unless record.nil? - puts record.out_s - else - puts "No processes found on port %s" % port - end - + operator = LtnpOperator.new + puts operator.check_port port 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 - + operator = LtnpOperator.new + puts operator.find_process p_name 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 record.out_s - puts "Would you like to kill %s? (y/n)" % [record.p_name] - input = gets - unless input.nil? - if input.downcase == "y" - CommandRunner.run_kill record.pid - end - end - else - puts "No processes found on port %s" % port - end - + operator = LtnpOperator.new + puts operator.kill_process_on_port port exit end end diff --git a/modules/command_runner.cr b/modules/command_runner.cr index 5d7551c..597971b 100644 --- a/modules/command_runner.cr +++ b/modules/command_runner.cr @@ -1,8 +1,8 @@ module CommandRunner - def self.run_ltnp : LtnpOperator + def self.run_ltnp : String io = IO::Memory.new Process.run("netstat -ltnp", shell: true, output: io) - LtnpOperator.new io.to_s + io.to_s end def self.run_kill(pid : String) : String diff --git a/operators/ltnp_operator.cr b/operators/ltnp_operator.cr new file mode 100644 index 0000000..1798c2b --- /dev/null +++ b/operators/ltnp_operator.cr @@ -0,0 +1,74 @@ +require "../modules/command_runner" +require "../records/ltnp_record" + +class LtnpOperator + def initialize(ltnp_data = "") + if ltnp_data.nil? + ltnp_data = CommandRunner.run_ltnp + end + records = ltnp_data.each_line + records = records.each.select(/^tcp/) + @ltnp_records = Array(LtnpRecord).new + records.each { |s| @ltnp_records << LtnpRecord.new s } + end + + # Data methods + + def to_s + @ltnp_records.each { |r| puts r.to_s } + end + + 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 + + #Actions + + # -c, --check-port + def check_port(port : String) : String + record = get_port_record port + + unless record.nil? + result = record.output_s + else + result = "No processes found on port %s" % port + end + + result + end + + # -p, --find-process + def find_process(p_name : String) + record = search_p_name p_name + + unless record.nil? + result = record.output_s + else + result = "No process found by the name %s" % p_name + end + + result + end + + # -k, --kill-port + def kill_process_on_port(port : String) + record = get_port_record port + + unless record.nil? + puts record.output_s + puts "Would you like to kill %s? (y/n)" % [record.p_name] + input = gets + unless input.nil? + if input.downcase == "y" + CommandRunner.run_kill record.pid + end + end + else + puts "No processes found on port %s" % port + end + end +end diff --git a/ltnp/ltnp_record.cr b/records/ltnp_record.cr similarity index 98% rename from ltnp/ltnp_record.cr rename to records/ltnp_record.cr index a895af3..64e1c80 100644 --- a/ltnp/ltnp_record.cr +++ b/records/ltnp_record.cr @@ -27,7 +27,7 @@ class LtnpRecord ] end - def out_s + def output_s "Port: %s, Process: %s, Process ID: %s" % [self.port, self.p_name, self.pid] end diff --git a/spec/ltnp/ltnp_operator_spec.cr b/spec/ltnp/ltnp_operator_spec.cr deleted file mode 100644 index f641041..0000000 --- a/spec/ltnp/ltnp_operator_spec.cr +++ /dev/null @@ -1,32 +0,0 @@ -require "spec" -require "../../ltnp/ltnp_operator.cr" - -describe LtnpOperator do - sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt") - - describe "#get_port_record" do - existing_port = "8081" - nonsense_port = "1234" - - it "should find record when port exists" do - sut.get_port_record(existing_port).nil?.should be_false - end - - it "should not find record when port doesn't exist" 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 diff --git a/spec/ltnp/operators/ltnp_operator_spec.cr b/spec/ltnp/operators/ltnp_operator_spec.cr new file mode 100644 index 0000000..4c3fa9a --- /dev/null +++ b/spec/ltnp/operators/ltnp_operator_spec.cr @@ -0,0 +1,52 @@ +require "spec" +require "../../../operators/ltnp_operator.cr" + +describe LtnpOperator do + sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt") + + existing_port = "8081" + nonsense_port = "1234" + + existing_p_name = "polymer" + nonsense_p_name = "thisisnotathing" + + describe "#get_port_record" do + it "should find record when port exists" do + sut.get_port_record(existing_port).nil?.should be_false + end + + it "should not find record when port doesn't exist" do + sut.get_port_record(nonsense_port).nil?.should be_true + end + end + + describe "#search_p_name" do + 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 + + describe "#check_port" do + it "should output successful message if found" do + sut.check_port(existing_port).should contain("Process:") + end + + it "should output failure message if not found" do + sut.check_port(nonsense_port).should contain("No") + end + end + + describe "#find_process" do + it "should output successful message if found" do + sut.find_process(existing_p_name).should contain("Process:") + end + + it "should output failure message if not found" do + sut.find_process(nonsense_p_name).should contain("No") + end + end +end diff --git a/spec/ltnp/ltnp_record_spec.cr b/spec/ltnp/records/ltnp_record_spec.cr similarity index 96% rename from spec/ltnp/ltnp_record_spec.cr rename to spec/ltnp/records/ltnp_record_spec.cr index 28d6c73..0397672 100644 --- a/spec/ltnp/ltnp_record_spec.cr +++ b/spec/ltnp/records/ltnp_record_spec.cr @@ -1,5 +1,5 @@ require "spec" -require "../../ltnp/ltnp_record.cr" +require "../../../records/ltnp_record.cr" describe LtnpRecord do sut_normal = LtnpRecord.new "tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 69/myproc" From f524b9864d39b15f8b83ddc3c6603c4b688cace3 Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Thu, 14 May 2020 22:06:27 -0400 Subject: [PATCH 2/2] did a format --- operators/ltnp_operator.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operators/ltnp_operator.cr b/operators/ltnp_operator.cr index 1798c2b..de3d826 100644 --- a/operators/ltnp_operator.cr +++ b/operators/ltnp_operator.cr @@ -26,7 +26,7 @@ class LtnpOperator @ltnp_records.find { |r| r.p_name == p_name } end - #Actions + # Actions # -c, --check-port def check_port(port : String) : String