pmng/src/pmng.cr

119 lines
3.1 KiB
Crystal
Raw Normal View History

2022-01-03 23:32:49 +03:00
require "option_parser"
2022-01-03 21:21:30 +03:00
require "yaml"
require "colorize"
2022-07-31 01:40:30 +03:00
require "./pmng/*"
2022-07-31 02:23:58 +03:00
require "./pmng/functions/*"
2022-01-03 23:32:49 +03:00
2022-07-31 02:23:58 +03:00
module Pmng
# check password file exists
if File.exists?(PASSWORD_FILE_PATH)
yaml = File.open(PASSWORD_FILE_PATH) do |file|
YAML.parse(file)
2022-01-03 23:25:07 +03:00
end
2022-03-13 13:46:29 +03:00
else
2022-07-31 02:23:58 +03:00
print "~/.pwd.yml".colorize(:red).mode(:bold)
puts ": No such file"
2022-04-08 16:38:03 +03:00
2022-07-31 02:23:58 +03:00
exit(1)
end
2022-05-02 10:51:25 +03:00
2022-07-31 02:23:58 +03:00
# check file ppermissions
password_file_permissions = File.info(PASSWORD_FILE_PATH).permissions.to_s
2022-03-13 13:46:29 +03:00
2022-07-31 02:23:58 +03:00
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)
2022-04-08 17:49:26 +03:00
2022-07-31 02:23:58 +03:00
exit(1)
end
2022-04-08 17:49:26 +03:00
2022-07-31 02:23:58 +03:00
# 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
2022-04-08 17:49:26 +03:00
2022-07-31 02:23:58 +03:00
# 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
2022-04-08 17:49:26 +03:00
2022-07-31 02:23:58 +03:00
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|
2023-04-14 21:50:32 +03:00
if item.url.includes?(password_string.to_s.downcase)
2022-07-31 02:23:58 +03:00
Functions.show_item(item)
puts "-----".colorize(:dark_gray).mode(:bold)
passwords_finded_array += 1
2022-03-13 13:46:29 +03:00
end
2022-01-03 21:21:30 +03:00
end
2022-07-31 02:23:58 +03:00
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)
2022-01-03 21:21:30 +03:00
end
2022-05-21 00:07:33 +03:00
puts
2022-01-03 21:21:30 +03:00
end
2022-07-31 02:23:58 +03:00
system "clear"
puts "Bye! 👋"
2022-01-03 21:21:30 +03:00
end