Using files from crystal init and reformatting everything to new editorconfig

pull/2/head
BraydonKains 5 years ago
parent f0b5a855ad
commit 1410190390

@ -0,0 +1,9 @@
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

9
.gitignore vendored

@ -1,2 +1,9 @@
# vim files
*.swp *.swp
eznet
# stuff from crystal init
/docs/
/lib/
/bin/
/.shards/
*.dwarf

@ -0,0 +1,5 @@
language: crystal
script:
- crystal spec
- crystal tool format --check

@ -2,17 +2,17 @@ require "./ltnp_record"
class LtnpOperator class LtnpOperator
def initialize(cmd_output : String) def initialize(cmd_output : String)
records = cmd_output.each_line records = cmd_output.each_line
records = records.each.select(/^tcp/) records = records.each.select(/^tcp/)
@ltnp_records = Array(LtnpRecord).new @ltnp_records = Array(LtnpRecord).new
records.each { |s| @ltnp_records << LtnpRecord.new s } records.each { |s| @ltnp_records << LtnpRecord.new s }
end end
def say_hi def say_hi
@ltnp_records.each { |r| puts r.to_s } @ltnp_records.each { |r| puts r.to_s }
end end
def get_port_record(port : String) : LtnpRecord | Nil def get_port_record(port : String) : LtnpRecord | Nil
@ltnp_records.find { |r| r.port == port } @ltnp_records.find { |r| r.port == port }
end end
end end

@ -1,7 +1,7 @@
require "../modules/record_helpers" require "../modules/record_helpers"
class LtnpRecord class LtnpRecord
getter proto : String getter proto : String
getter state : String getter state : String
getter address : String getter address : String
getter port : String getter port : String
@ -12,42 +12,41 @@ class LtnpRecord
getter p_name : String getter p_name : String
def initialize(record : String) def initialize(record : String)
ser_record = RecordHelpers.clean_record(record) ser_record = RecordHelpers.clean_record(record)
@proto = ser_record[0] @proto = ser_record[0]
@address, @port = ser_address_port ser_record[3].split(':') @address, @port = ser_address_port ser_record[3].split(':')
@f_address, @f_port = ser_address_port ser_record[4].split(':') @f_address, @f_port = ser_address_port ser_record[4].split(':')
@state = ser_record[5] @state = ser_record[5]
@pid, @p_name = ser_pid_p_name ser_record[6] @pid, @p_name = ser_pid_p_name ser_record[6]
end end
def to_s def to_s
puts "%s, " * 8 % [ puts "%s, " * 8 % [
@proto, @state, @address, @port, @proto, @state, @address, @port,
@f_address, @f_port, @pid, @p_name @f_address, @f_port, @pid, @p_name,
] ]
end end
def ser_address_port(address_port : Array) : {String, String} def ser_address_port(address_port : Array) : {String, String}
address = String.new address = String.new
port = String.new port = String.new
unless address_port.size > 2 unless address_port.size > 2
address = address_port[0] address = address_port[0]
port = address_port[1] port = address_port[1]
else else
port = address_port[-1] port = address_port[-1]
end end
{address, port} {address, port}
end end
def ser_pid_p_name(pid_pro_str : String) : {String, String} def ser_pid_p_name(pid_pro_str : String) : {String, String}
pid = String.new pid = String.new
p_name = String.new p_name = String.new
unless pid_pro_str == "-" unless pid_pro_str == "-"
pid_pro = pid_pro_str.split('/') pid_pro = pid_pro_str.split('/')
pid = pid_pro[0] pid = pid_pro[0]
p_name = pid_pro[1] p_name = pid_pro[1]
end end
{pid, p_name} {pid, p_name}
end end
end end

@ -6,29 +6,29 @@ OptionParser.parse do |parser|
parser.banner = "Welcome to eznetstat!" parser.banner = "Welcome to eznetstat!"
parser.on "-v", "--version", "Show version" do parser.on "-v", "--version", "Show version" do
puts "0.1" puts "0.1"
exit exit
end end
parser.on "-h", "--help", "Show help" do parser.on "-h", "--help", "Show help" do
puts parser puts parser
exit exit
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 = NetstatRunner.run_ltnp ltnp_operator = NetstatRunner.run_ltnp
record = ltnp_operator.get_port_record port record = ltnp_operator.get_port_record port
unless record.nil? unless record.nil?
puts "Port: %s, Process: %s" % [record.port, record.p_name] puts "Port: %s, Process: %s" % [record.port, record.p_name]
else else
puts "No processes found on port %s" % port puts "No processes found on port %s" % port
end end
exit exit
end end
parser.on "-k", "--kill-port", "Kill process on port" do parser.on "-k", "--kill-port", "Kill process on port" do
ltnp_operator = NetstatRunner.run_ltnp ltnp_operator = NetstatRunner.run_ltnp
ltnp_operator.say_hi ltnp_operator.say_hi
exit exit
end end
end end

@ -1,7 +1,7 @@
module NetstatRunner module NetstatRunner
def self.run_ltnp() : LtnpOperator def self.run_ltnp : LtnpOperator
io = IO::Memory.new io = IO::Memory.new
Process.run("sudo netstat -ltnp", shell: true, output: io) Process.run("sudo netstat -ltnp", shell: true, output: io)
LtnpOperator.new io.to_s LtnpOperator.new io.to_s
end end
end end

@ -1,7 +1,7 @@
module RecordHelpers module RecordHelpers
def self.clean_record(record : String) : Array(String) def self.clean_record(record : String) : Array(String)
tokenized_record = record.split(" ") tokenized_record = record.split(" ")
tokenized_record = tokenized_record.reject { |s| s == "" } tokenized_record = tokenized_record.reject { |s| s == "" }
tokenized_record tokenized_record
end end
end end

@ -0,0 +1,13 @@
name: eznet-cli
version: 0.0.1
authors:
- BraydonKains <kainsbraydon@gmail.com>
targets:
eznet-cli:
eznet: ./main.cr
crystal: 0.33.0
license: MIT

@ -5,15 +5,15 @@ describe LtnpOperator do
sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt") sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt")
describe "#get_port_record" do describe "#get_port_record" do
existing_port = "8081" existing_port = "8081"
nonsense_port = "1234" nonsense_port = "1234"
it "should find record when port exists" do it "should find record when port exists" do
sut.get_port_record(existing_port).nil?.should be_false sut.get_port_record(existing_port).nil?.should be_false
end end
it "should not find record when port doesn't exist" do it "should not find record when port doesn't exist" do
sut.get_port_record(nonsense_port).nil?.should be_true sut.get_port_record(nonsense_port).nil?.should be_true
end end
end end
end end

@ -2,28 +2,28 @@ require "spec"
require "../../ltnp/ltnp_record.cr" require "../../ltnp/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"
sut_address = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN 69/myproc" sut_address = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN 69/myproc"
sut_process = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN -" sut_process = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN -"
it "should have expected properties under normal circumstance" do it "should have expected properties under normal circumstance" do
sut_normal.proto.should eq("tcp") sut_normal.proto.should eq("tcp")
sut_normal.address.should eq("127.0.0.1") sut_normal.address.should eq("127.0.0.1")
sut_normal.port.should eq("8081") sut_normal.port.should eq("8081")
sut_normal.f_address.should eq("0.0.0.0") sut_normal.f_address.should eq("0.0.0.0")
sut_normal.f_port.should eq("*") sut_normal.f_port.should eq("*")
sut_normal.state.should eq("LISTEN") sut_normal.state.should eq("LISTEN")
sut_normal.pid.should eq("69") sut_normal.pid.should eq("69")
sut_normal.p_name.should eq("myproc") sut_normal.p_name.should eq("myproc")
end end
it "should have expected properties when Local Address column is edge case" do it "should have expected properties when Local Address column is edge case" do
sut_address.address.empty?.should be_true sut_address.address.empty?.should be_true
sut_address.port.should eq("8081") sut_address.port.should eq("8081")
end end
it "should have expected properties when PID/ProcessName column is edge case" do it "should have expected properties when PID/ProcessName column is edge case" do
sut_process.pid.empty?.should be_true sut_process.pid.empty?.should be_true
sut_process.p_name.empty?.should be_true sut_process.p_name.empty?.should be_true
end end
end end

Loading…
Cancel
Save