mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-18 17:10:04 +00:00
fix update
This commit is contained in:
parent
9921d3057f
commit
446faf704e
3 changed files with 30 additions and 36 deletions
31
lib/Item.rb
31
lib/Item.rb
|
@ -24,8 +24,7 @@ module MPW
|
||||||
attr_accessor :created
|
attr_accessor :created
|
||||||
|
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
if not defined?(options[:id]) or not options[:id].to_s.empty? or
|
if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty?
|
||||||
not defined?(options[:created]) or not options[:created].to_s.empty?
|
|
||||||
@id = generate_id
|
@id = generate_id
|
||||||
@created = Time.now.to_i
|
@created = Time.now.to_i
|
||||||
else
|
else
|
||||||
|
@ -37,19 +36,19 @@ module MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(options={})
|
def update(options={})
|
||||||
if defined?(options[:name]) and options[:name].to_s.empty?
|
if options.has_key?(:name) and options[:name].to_s.empty?
|
||||||
@error_msg = I18n.t('error.update.name_empty')
|
@error_msg = I18n.t('error.update.name_empty')
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@name = options[:name] if defined?(options[:name])
|
@name = options[:name] if options.has_key?(:name)
|
||||||
@group = options[:group] if defined?(options[:group])
|
@group = options[:group] if options.has_key?(:group)
|
||||||
@host = options[:host] if defined?(options[:host])
|
@host = options[:host] if options.has_key?(:host)
|
||||||
@protocol = options[:protocol] if defined?(options[:protocol])
|
@protocol = options[:protocol] if options.has_key?(:protocol)
|
||||||
@user = options[:user] if defined?(options[:user])
|
@user = options[:user] if options.has_key?(:user)
|
||||||
@password = options[:password] if defined?(options[:password])
|
@password = options[:password] if options.has_key?(:password)
|
||||||
@port = options[:port].to_i if defined?(options[:port])
|
@port = options[:port].to_i if options.has_key?(:port) and not options[:port].to_s.empty?
|
||||||
@comment = options[:comment] if defined?(options[:comment])
|
@comment = options[:comment] if options.has_key?(:comment)
|
||||||
@last_edit = Time.now.to_i
|
@last_edit = Time.now.to_i
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -63,16 +62,6 @@ module MPW
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def set_name(name)
|
|
||||||
if name.to_s.empty?
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
@name = name
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def generate_id
|
def generate_id
|
||||||
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
|
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
|
||||||
|
|
|
@ -40,7 +40,7 @@ module MPW
|
||||||
group: d['group'],
|
group: d['group'],
|
||||||
host: d['host'],
|
host: d['host'],
|
||||||
protocol: d['protocol'],
|
protocol: d['protocol'],
|
||||||
user: d['login'],
|
user: d['user'],
|
||||||
password: d['password'],
|
password: d['password'],
|
||||||
port: d['port'],
|
port: d['port'],
|
||||||
comment: d['comment'],
|
comment: d['comment'],
|
||||||
|
@ -62,9 +62,10 @@ module MPW
|
||||||
# @rtrn: true if the file has been encrypted
|
# @rtrn: true if the file has been encrypted
|
||||||
def encrypt
|
def encrypt
|
||||||
FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg)
|
FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg)
|
||||||
|
|
||||||
|
data_to_encrypt = {}
|
||||||
|
|
||||||
@data.each do |item|
|
@data.each do |item|
|
||||||
puts item.class
|
|
||||||
next if item.empty?
|
next if item.empty?
|
||||||
|
|
||||||
data_to_encrypt.merge!(item.id => {'id' => item.id,
|
data_to_encrypt.merge!(item.id => {'id' => item.id,
|
||||||
|
|
|
@ -233,7 +233,7 @@ class Cli
|
||||||
options[:host] = ask(I18n.t('form.add.server')).to_s
|
options[:host] = ask(I18n.t('form.add.server')).to_s
|
||||||
options[:protocol] = ask(I18n.t('form.add.protocol')).to_s
|
options[:protocol] = ask(I18n.t('form.add.protocol')).to_s
|
||||||
options[:user] = ask(I18n.t('form.add.login')).to_s
|
options[:user] = ask(I18n.t('form.add.login')).to_s
|
||||||
options[:passwd] = ask(I18n.t('form.add.password')).to_s
|
options[:password] = ask(I18n.t('form.add.password')).to_s
|
||||||
options[:port] = ask(I18n.t('form.add.port')).to_s
|
options[:port] = ask(I18n.t('form.add.port')).to_s
|
||||||
options[:comment] = ask(I18n.t('form.add.comment')).to_s
|
options[:comment] = ask(I18n.t('form.add.comment')).to_s
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ class Cli
|
||||||
puts "#{I18n.t('display.error')} #12: #{@mpw.error_msg}".red
|
puts "#{I18n.t('display.error')} #12: #{@mpw.error_msg}".red
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')} #13: #{@mpw.error_msg}".red
|
puts "#{I18n.t('display.error')} #13: #{item.error_msg}".red
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -255,19 +255,23 @@ class Cli
|
||||||
def update(id)
|
def update(id)
|
||||||
item = @mpw.search_by_id(id)
|
item = @mpw.search_by_id(id)
|
||||||
|
|
||||||
if not row.empty?
|
if not item.nil?
|
||||||
|
options = {}
|
||||||
|
|
||||||
puts I18n.t('form.update.title')
|
puts I18n.t('form.update.title')
|
||||||
puts '--------------------'
|
puts '--------------------'
|
||||||
name = ask(I18n.t('form.update.name' , name: item.name)).to_s
|
options[:name] = ask(I18n.t('form.update.name' , name: item.name)).to_s
|
||||||
group = ask(I18n.t('form.update.group' , group: item.group)).to_s
|
options[:group] = ask(I18n.t('form.update.group' , group: item.group)).to_s
|
||||||
server = ask(I18n.t('form.update.server' , server: item.host)).to_s
|
options[:host] = ask(I18n.t('form.update.server' , server: item.host)).to_s
|
||||||
protocol = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s
|
options[:protocol] = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s
|
||||||
login = ask(I18n.t('form.update.login' , login: item.user)).to_s
|
options[:user] = ask(I18n.t('form.update.login' , login: item.user)).to_s
|
||||||
passwd = ask(I18n.t('form.update.password')).to_s
|
options[:password] = ask(I18n.t('form.update.password')).to_s
|
||||||
port = ask(I18n.t('form.update.port' , port: item.port)).to_s
|
options[:port] = ask(I18n.t('form.update.port' , port: item.port)).to_s
|
||||||
comment = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s
|
options[:comment] = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s
|
||||||
|
|
||||||
|
options.delete_if { |k,v| v.empty? }
|
||||||
|
|
||||||
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
|
if item.update(options)
|
||||||
if @mpw.encrypt
|
if @mpw.encrypt
|
||||||
sync
|
sync
|
||||||
puts "#{I18n.t('form.update.valid')}".green
|
puts "#{I18n.t('form.update.valid')}".green
|
||||||
|
@ -275,7 +279,7 @@ class Cli
|
||||||
puts "#{I18n.t('display.error')} #14: #{@mpw.error_msg}".red
|
puts "#{I18n.t('display.error')} #14: #{@mpw.error_msg}".red
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')} #15: #{@mpw.error_msg}".red
|
puts "#{I18n.t('display.error')} #15: #{item.error_msg}".red
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
puts I18n.t('display.nothing')
|
puts I18n.t('display.nothing')
|
||||||
|
|
Loading…
Add table
Reference in a new issue