add functions module
This commit is contained in:
parent
626e7cbe0b
commit
70a2e47727
230
src/pmng.cr
230
src/pmng.cr
@ -3,148 +3,116 @@ require "yaml"
|
|||||||
require "colorize"
|
require "colorize"
|
||||||
|
|
||||||
require "./pmng/*"
|
require "./pmng/*"
|
||||||
|
require "./pmng/functions/*"
|
||||||
|
|
||||||
# check password file exists
|
module Pmng
|
||||||
if File.exists?(PASSWORD_FILE_PATH)
|
# check password file exists
|
||||||
yaml = File.open(PASSWORD_FILE_PATH) do |file|
|
if File.exists?(PASSWORD_FILE_PATH)
|
||||||
YAML.parse(file)
|
yaml = File.open(PASSWORD_FILE_PATH) do |file|
|
||||||
end
|
YAML.parse(file)
|
||||||
else
|
|
||||||
print "~/.pwd.yml".colorize(:red).mode(:bold)
|
|
||||||
puts ": No such file"
|
|
||||||
|
|
||||||
exit(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
# check file ppermissions
|
|
||||||
password_file_permissions = File.info(PASSWORD_FILE_PATH).permissions.to_s
|
|
||||||
|
|
||||||
if /\d{3}/.match(password_file_permissions).try &.[0] != "600"
|
|
||||||
puts "Password file permissions is not RW for you.".colorize(:red)
|
|
||||||
print "Execute: ".colorize(:yellow)
|
|
||||||
print "(chmod 600 ~/.pwd.yml) ".colorize(:green).mode(:bold)
|
|
||||||
puts "for fix.".colorize(:yellow)
|
|
||||||
|
|
||||||
exit(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
# fill passwords array
|
|
||||||
passwords_array = [] of Password
|
|
||||||
count = 0
|
|
||||||
while count < yaml.size
|
|
||||||
passwords_array << Password.from_yaml(yaml[count].to_yaml)
|
|
||||||
count += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
# pmng title
|
|
||||||
system "clear"
|
|
||||||
puts ASCII_LOGO.colorize(:yellow)
|
|
||||||
puts "The very simple password manager for humans".colorize(:yellow).mode(:bold)
|
|
||||||
puts "-------------------------------------------".colorize(:yellow).mode(:bold)
|
|
||||||
|
|
||||||
# main loop
|
|
||||||
loop = true
|
|
||||||
while loop
|
|
||||||
# shell prompt
|
|
||||||
print "Enter URL (".colorize(:white).mode(:bold)
|
|
||||||
print ":h".colorize(:red).mode(:bold)
|
|
||||||
print " for help or ".colorize(:white).mode(:bold)
|
|
||||||
print ":q".colorize(:red).mode(:bold)
|
|
||||||
print " for exit)\n".colorize(:white).mode(:bold)
|
|
||||||
print "> ".colorize(:green).mode(:bold)
|
|
||||||
|
|
||||||
begin
|
|
||||||
STDIN.read_timeout = USER_INPUT_TIMEOUT
|
|
||||||
password_string = STDIN.gets
|
|
||||||
rescue IO::TimeoutError
|
|
||||||
loop = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if password_string.to_s == ":q"
|
|
||||||
# if ':q' to close program
|
|
||||||
loop = false
|
|
||||||
elsif password_string.to_s.size == 0
|
|
||||||
# if puts empty, retry prompt
|
|
||||||
puts
|
|
||||||
elsif password_string.to_s == ":h"
|
|
||||||
# if ':h' to view help
|
|
||||||
system "clear"
|
|
||||||
|
|
||||||
puts "Help\n----".colorize(:yellow).mode(:bold)
|
|
||||||
print ":s".colorize(:red).mode(:bold)
|
|
||||||
puts " - Return stats"
|
|
||||||
elsif password_string.to_s == ":s"
|
|
||||||
# if ':s' to view Statistics
|
|
||||||
system "clear"
|
|
||||||
|
|
||||||
puts "Statistics\n----------".colorize(:yellow).mode(:bold)
|
|
||||||
|
|
||||||
print "All elements: ".colorize(:yellow).mode(:bold)
|
|
||||||
puts passwords_array.size
|
|
||||||
|
|
||||||
print "Passwords outdated: ".colorize(:red).mode(:bold)
|
|
||||||
outdated_count = 0
|
|
||||||
current_time = Time.local.to_unix
|
|
||||||
a = [] of String
|
|
||||||
passwords_array.each do |item|
|
|
||||||
if item.update + (2629743 * 3) < current_time # 2629743 * 3 -- 3 month
|
|
||||||
outdated_count += 1
|
|
||||||
a << item.url
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
puts outdated_count
|
|
||||||
else
|
else
|
||||||
# list search password
|
print "~/.pwd.yml".colorize(:red).mode(:bold)
|
||||||
system "clear"
|
puts ": No such file"
|
||||||
|
|
||||||
passwords_finded_array = 0
|
exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
passwords_array.each do |item|
|
# check file ppermissions
|
||||||
if item.url.includes?(password_string.to_s)
|
password_file_permissions = File.info(PASSWORD_FILE_PATH).permissions.to_s
|
||||||
print "🌐 "
|
|
||||||
puts item.url.colorize(:magenta).mode(:bold).mode(:underline)
|
|
||||||
|
|
||||||
if !item.email.blank?
|
if /\d{3}/.match(password_file_permissions).try &.[0] != "600"
|
||||||
print "📧 "
|
puts "Password file permissions is not RW for you.".colorize(:red)
|
||||||
puts item.email.colorize(:red)
|
print "Execute: ".colorize(:yellow)
|
||||||
|
print "(chmod 600 ~/.pwd.yml) ".colorize(:green).mode(:bold)
|
||||||
|
puts "for fix.".colorize(:yellow)
|
||||||
|
|
||||||
|
exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
# fill passwords array
|
||||||
|
passwords_array = [] of Password
|
||||||
|
count = 0
|
||||||
|
while count < yaml.size
|
||||||
|
passwords_array << Password.from_yaml(yaml[count].to_yaml)
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
# pmng title
|
||||||
|
system "clear"
|
||||||
|
puts ASCII_LOGO.colorize(:yellow)
|
||||||
|
puts "The very simple password manager for humans".colorize(:yellow).mode(:bold)
|
||||||
|
puts "-------------------------------------------".colorize(:yellow).mode(:bold)
|
||||||
|
|
||||||
|
statistics = Functions::Statistics.new(passwords_array)
|
||||||
|
|
||||||
|
# main loop
|
||||||
|
loop = true
|
||||||
|
while loop
|
||||||
|
# shell prompt
|
||||||
|
print "Enter URL (".colorize(:white).mode(:bold)
|
||||||
|
print ":h".colorize(:red).mode(:bold)
|
||||||
|
print " for help or ".colorize(:white).mode(:bold)
|
||||||
|
print ":q".colorize(:red).mode(:bold)
|
||||||
|
print " for exit)\n".colorize(:white).mode(:bold)
|
||||||
|
print "> ".colorize(:green).mode(:bold)
|
||||||
|
|
||||||
|
begin
|
||||||
|
STDIN.read_timeout = USER_INPUT_TIMEOUT
|
||||||
|
password_string = STDIN.gets
|
||||||
|
rescue IO::TimeoutError
|
||||||
|
loop = false
|
||||||
|
end
|
||||||
|
|
||||||
|
if password_string.to_s == ":q"
|
||||||
|
# if ':q' to close program
|
||||||
|
loop = false
|
||||||
|
elsif password_string.to_s.size == 0
|
||||||
|
# if puts empty, retry prompt
|
||||||
|
puts
|
||||||
|
elsif password_string.to_s == ":h"
|
||||||
|
# if ':h' to view help
|
||||||
|
system "clear"
|
||||||
|
|
||||||
|
puts "Help\n----".colorize(:yellow).mode(:bold)
|
||||||
|
print ":s".colorize(:red).mode(:bold)
|
||||||
|
puts " - Return stats"
|
||||||
|
elsif password_string.to_s == ":s"
|
||||||
|
# if ':s' to view Statistics
|
||||||
|
system "clear"
|
||||||
|
puts "Statistics\n----------".colorize(:yellow).mode(:bold)
|
||||||
|
|
||||||
|
print "All elements: ".colorize(:yellow).mode(:bold)
|
||||||
|
puts statistics.size
|
||||||
|
|
||||||
|
print "Passwords outdated: ".colorize(:red).mode(:bold)
|
||||||
|
puts statistics.outdated
|
||||||
|
else
|
||||||
|
# list search password
|
||||||
|
system "clear"
|
||||||
|
|
||||||
|
passwords_finded_array = 0
|
||||||
|
|
||||||
|
passwords_array.each do |item|
|
||||||
|
if item.url.includes?(password_string.to_s)
|
||||||
|
Functions.show_item(item)
|
||||||
|
|
||||||
|
puts "-----".colorize(:dark_gray).mode(:bold)
|
||||||
|
|
||||||
|
passwords_finded_array += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if !item.login.blank?
|
|
||||||
print "🗿 "
|
|
||||||
puts item.login.colorize(:yellow)
|
|
||||||
end
|
|
||||||
|
|
||||||
if !item.password.blank?
|
|
||||||
print "🔐 "
|
|
||||||
puts item.password.colorize(:red).back(:red)
|
|
||||||
end
|
|
||||||
|
|
||||||
if !item.desc.blank?
|
|
||||||
print "📄 "
|
|
||||||
puts item.desc.colorize(:cyan)
|
|
||||||
end
|
|
||||||
|
|
||||||
if !item.profile_url.blank?
|
|
||||||
print "👦 "
|
|
||||||
puts item.profile_url.colorize(:green)
|
|
||||||
end
|
|
||||||
|
|
||||||
puts "-----".colorize(:dark_gray).mode(:bold)
|
|
||||||
|
|
||||||
passwords_finded_array += 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
puts
|
||||||
|
print "Finded ".colorize(:yellow).mode(:bold)
|
||||||
|
print passwords_finded_array.colorize(:red).mode(:bold)
|
||||||
|
puts " records".colorize(:yellow).mode(:bold)
|
||||||
|
puts "-----".colorize(:dark_gray).mode(:bold)
|
||||||
end
|
end
|
||||||
|
|
||||||
puts
|
puts
|
||||||
print "Finded ".colorize(:yellow).mode(:bold)
|
|
||||||
print passwords_finded_array.colorize(:red).mode(:bold)
|
|
||||||
puts " records".colorize(:yellow).mode(:bold)
|
|
||||||
puts "-----".colorize(:dark_gray).mode(:bold)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
puts
|
system "clear"
|
||||||
|
puts "Bye! 👋"
|
||||||
end
|
end
|
||||||
|
|
||||||
system "clear"
|
|
||||||
puts "Bye! 👋"
|
|
||||||
|
33
src/pmng/functions/item.cr
Normal file
33
src/pmng/functions/item.cr
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
module Pmng::Functions
|
||||||
|
extend self
|
||||||
|
|
||||||
|
def show_item(password)
|
||||||
|
print "🌐 "
|
||||||
|
puts password.url.colorize(:magenta).mode(:bold).mode(:underline)
|
||||||
|
|
||||||
|
if !password.email.blank?
|
||||||
|
print "📧 "
|
||||||
|
puts password.email.colorize(:red)
|
||||||
|
end
|
||||||
|
|
||||||
|
if !password.login.blank?
|
||||||
|
print "🗿 "
|
||||||
|
puts password.login.colorize(:yellow)
|
||||||
|
end
|
||||||
|
|
||||||
|
if !password.password.blank?
|
||||||
|
print "🔐 "
|
||||||
|
puts password.password.colorize(:red).back(:red)
|
||||||
|
end
|
||||||
|
|
||||||
|
if !password.desc.blank?
|
||||||
|
print "📄 "
|
||||||
|
puts password.desc.colorize(:cyan)
|
||||||
|
end
|
||||||
|
|
||||||
|
if !password.profile_url.blank?
|
||||||
|
print "👦 "
|
||||||
|
puts password.profile_url.colorize(:green)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
31
src/pmng/functions/statistics.cr
Normal file
31
src/pmng/functions/statistics.cr
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
module Pmng::Functions
|
||||||
|
class Statistics
|
||||||
|
getter passwords : Array(Password)
|
||||||
|
getter outdated : Int32
|
||||||
|
getter size : Int32
|
||||||
|
|
||||||
|
def initialize(@passwords)
|
||||||
|
@size = passwords.size
|
||||||
|
|
||||||
|
outdated_count = 0
|
||||||
|
current_time = Time.local.to_unix
|
||||||
|
a = [] of String
|
||||||
|
@passwords.each do |item|
|
||||||
|
if item.update + (2629743 * 3) < current_time # 2629743 * 3 -- 3 month
|
||||||
|
outdated_count += 1
|
||||||
|
a << item.url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@outdated = outdated_count
|
||||||
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
return @size
|
||||||
|
end
|
||||||
|
|
||||||
|
def outdated
|
||||||
|
return @outdated
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user