Structural refactor, defers responsibility for actions to operators, organizing code differently
parent
4d9cabb476
commit
6ba3c67a43
@ -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
|
@ -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
|
@ -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
|
@ -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
|
@ -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"
|
Loading…
Reference in New Issue