Merge pull request #6 from BraydonKains/StructureRefactor

Structural refactor, defers responsibility for actions to operators, …
pull/7/head
Braydon Kains 5 years ago committed by GitHub
commit 8e84139824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -1,6 +1,5 @@
require "option_parser" require "option_parser"
require "./ltnp/ltnp_operator" require "./ltnp/ltnp_operator"
require "./modules/command_runner"
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "Welcome to eznetstat!" parser.banner = "Welcome to eznetstat!"
@ -16,48 +15,20 @@ OptionParser.parse do |parser|
end end
parser.on "-c PORT", "--check-port=PORT", "Get the process name and ID of process on port" do |port| parser.on "-c PORT", "--check-port=PORT", "Get the process name and ID of process on port" do |port|
ltnp_operator = CommandRunner.run_ltnp operator = LtnpOperator.new
record = ltnp_operator.get_port_record port puts operator.check_port port
unless record.nil?
puts record.out_s
else
puts "No processes found on port %s" % port
end
exit exit
end end
parser.on "-p PNAME", "--find-process=PNAME", "Find the port and ID of a process by name" do |p_name| 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 operator = LtnpOperator.new
record = ltnp_operator.search_p_name p_name puts operator.find_process p_name
unless record.nil?
puts record.out_s
else
puts "No process found by the name %s" % p_name
end
exit exit
end end
parser.on "-k PORT", "--kill-port=PORT", "Kill process on port" do |port| parser.on "-k PORT", "--kill-port=PORT", "Kill process on port" do |port|
ltnp_operator = CommandRunner.run_ltnp operator = LtnpOperator.new
record = ltnp_operator.get_port_record port puts operator.kill_process_on_port 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
exit exit
end end
end end

@ -1,8 +1,8 @@
module CommandRunner module CommandRunner
def self.run_ltnp : LtnpOperator def self.run_ltnp : String
io = IO::Memory.new io = IO::Memory.new
Process.run("netstat -ltnp", shell: true, output: io) Process.run("netstat -ltnp", shell: true, output: io)
LtnpOperator.new io.to_s io.to_s
end end
def self.run_kill(pid : String) : String def self.run_kill(pid : String) : String

@ -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

@ -27,7 +27,7 @@ class LtnpRecord
] ]
end end
def out_s def output_s
"Port: %s, Process: %s, Process ID: %s" % [self.port, self.p_name, self.pid] "Port: %s, Process: %s, Process ID: %s" % [self.port, self.p_name, self.pid]
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 "spec"
require "../../ltnp/ltnp_record.cr" require "../../../records/ltnp_record.cr"
describe LtnpRecord do describe LtnpRecord do
sut_normal = LtnpRecord.new "tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 69/myproc" sut_normal = LtnpRecord.new "tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 69/myproc"
Loading…
Cancel
Save