mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-23 13:57:52 +00:00
add sync information in cli setup
This commit is contained in:
parent
961117338c
commit
d65316896e
3 changed files with 47 additions and 25 deletions
46
lib/Cli.rb
46
lib/Cli.rb
|
@ -21,21 +21,6 @@ class Cli
|
||||||
# config_file -> a specify config file
|
# config_file -> a specify config file
|
||||||
def initialize(lang, config)
|
def initialize(lang, config)
|
||||||
@config = config
|
@config = config
|
||||||
|
|
||||||
@mpw = MPW.new(@config.file_gpg, @config.key)
|
|
||||||
if not decrypt()
|
|
||||||
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
|
||||||
exit 2
|
|
||||||
end
|
|
||||||
|
|
||||||
@sync = Sync.new()
|
|
||||||
if @config.sync_host.nil? || @config.sync_port.nil?
|
|
||||||
@sync.disable()
|
|
||||||
elsif !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix)
|
|
||||||
puts "#{I18n.t('cli.sync.not_connect')}:\n#{@sync.error_msg}"
|
|
||||||
end
|
|
||||||
|
|
||||||
sync()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Destructor
|
# Destructor
|
||||||
|
@ -45,6 +30,16 @@ class Cli
|
||||||
|
|
||||||
# Sync the data with the server
|
# Sync the data with the server
|
||||||
def sync()
|
def sync()
|
||||||
|
if !defined?(@sync)
|
||||||
|
@sync = Sync.new()
|
||||||
|
|
||||||
|
if @config.sync_host.nil? || @config.sync_port.nil?
|
||||||
|
@sync.disable()
|
||||||
|
elsif !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix)
|
||||||
|
puts "#{I18n.t('cli.sync.not_connect')}:\n#{@sync.error_msg}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
if !@mpw.sync(@sync.get(@passwd), @config.last_update)
|
if !@mpw.sync(@sync.get(@passwd), @config.last_update)
|
||||||
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
||||||
|
@ -71,13 +66,22 @@ class Cli
|
||||||
key = ask(I18n.t('cli.form.setup.gpg_key')).to_s
|
key = ask(I18n.t('cli.form.setup.gpg_key')).to_s
|
||||||
file_gpg = ask(I18n.t('cli.form.setup.gpg_file', :home => Dir.home())).to_s
|
file_gpg = ask(I18n.t('cli.form.setup.gpg_file', :home => Dir.home())).to_s
|
||||||
timeout_pwd = ask(I18n.t('cli.form.setup.timeout')).to_s
|
timeout_pwd = ask(I18n.t('cli.form.setup.timeout')).to_s
|
||||||
|
sync_host = ask(I18n.t('cli.form.setup.sync_host')).to_s
|
||||||
|
sync_port = ask(I18n.t('cli.form.setup.sync_port')).to_s
|
||||||
|
sync_pwd = ask(I18n.t('cli.form.setup.sync_pwd')).to_s
|
||||||
|
sync_suffix = ask(I18n.t('cli.form.setup.sync_suffix')).to_s
|
||||||
|
|
||||||
if !File.exist?("#{APP_ROOT}/i18n/#{language}.yml")
|
if !File.exist?("#{APP_ROOT}/i18n/#{language}.yml")
|
||||||
language= 'en'
|
language= 'en'
|
||||||
end
|
end
|
||||||
I18n.locale = language.to_sym
|
I18n.locale = language.to_sym
|
||||||
|
|
||||||
if @config.setup(key, language, file_gpg, timeout_pwd)
|
sync_host.empty? ? (sync_host = nil) : (sync_host = sync_host)
|
||||||
|
sync_port.empty? ? (sync_port = nil) : (sync_port = sync_port.to_i)
|
||||||
|
sync_pwd.empty? ? (sync_pwd = nil) : (sync_pwd = sync_pwd)
|
||||||
|
sync_suffix.empty? ? (sync_suffix = nil) : (sync_suffix = sync_suffix)
|
||||||
|
|
||||||
|
if @config.setup(key, language, file_gpg, timeout_pwd, sync_host, sync_port, sync_pwd, sync_suffix)
|
||||||
puts I18n.t('cli.form.setup.valid')
|
puts I18n.t('cli.form.setup.valid')
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
|
puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
|
||||||
|
@ -85,13 +89,21 @@ class Cli
|
||||||
|
|
||||||
if not @config.checkconfig()
|
if not @config.checkconfig()
|
||||||
puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
|
puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
|
||||||
|
exit 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Request the GPG password and decrypt the file
|
# Request the GPG password and decrypt the file
|
||||||
def decrypt()
|
def decrypt()
|
||||||
|
if !defined?(@mpw)
|
||||||
|
@mpw = MPW.new(@config.file_gpg, @config.key)
|
||||||
|
end
|
||||||
|
|
||||||
@passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false}
|
@passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false}
|
||||||
return @mpw.decrypt(@passwd)
|
if !@mpw.decrypt(@passwd)
|
||||||
|
puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
|
||||||
|
exit 2
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Display the query's result
|
# Display the query's result
|
||||||
|
|
|
@ -38,8 +38,12 @@ class MPWConfig
|
||||||
# lang -> the software language
|
# lang -> the software language
|
||||||
# file_gpg -> the file who is encrypted
|
# file_gpg -> the file who is encrypted
|
||||||
# timeout_pwd -> time to save the password
|
# timeout_pwd -> time to save the password
|
||||||
|
# sync_host -> the server host for synchronization
|
||||||
|
# sync_port -> the server port for synchronization
|
||||||
|
# sync_pwd -> the password for synchronization
|
||||||
|
# sync_suffix -> the suffix file (optionnal)
|
||||||
# @rtrn: true if le config file is create
|
# @rtrn: true if le config file is create
|
||||||
def setup(key, lang, file_gpg, timeout_pwd)
|
def setup(key, lang, file_gpg, timeout_pwd, sync_host=nil, sync_port=nil, sync_pwd=nil, sync_suffix=nil)
|
||||||
|
|
||||||
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
|
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
|
||||||
@error_msg = I18n.t('error.config.key_bad_format')
|
@error_msg = I18n.t('error.config.key_bad_format')
|
||||||
|
@ -56,10 +60,10 @@ class MPWConfig
|
||||||
'lang' => lang,
|
'lang' => lang,
|
||||||
'file_gpg' => file_gpg,
|
'file_gpg' => file_gpg,
|
||||||
'timeout_pwd' => timeout_pwd,
|
'timeout_pwd' => timeout_pwd,
|
||||||
'sync_host' => host,
|
'sync_host' => sync_host,
|
||||||
'sync_port' => port,
|
'sync_port' => sync_port,
|
||||||
'sync_pwd' => password,
|
'sync_pwd' => sync_pwd,
|
||||||
'sync_suffix' => suffix,
|
'sync_suffix' => sync_suffix,
|
||||||
'last_update' => 0 }}
|
'last_update' => 0 }}
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -86,7 +90,7 @@ class MPWConfig
|
||||||
@sync_host = config['config']['sync_host']
|
@sync_host = config['config']['sync_host']
|
||||||
@sync_port = config['config']['sync_port']
|
@sync_port = config['config']['sync_port']
|
||||||
@sync_pwd = config['config']['sync_pwd']
|
@sync_pwd = config['config']['sync_pwd']
|
||||||
@sync_suffix = config['config']['sync_suffix']
|
@sync_suffix = config['config']['sync_suffix']
|
||||||
@last_update = config['config']['last_update'].to_i
|
@last_update = config['config']['last_update'].to_i
|
||||||
|
|
||||||
if @key.empty? || @file_gpg.empty?
|
if @key.empty? || @file_gpg.empty?
|
||||||
|
@ -104,6 +108,8 @@ class MPWConfig
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Set the last update when there is a sync
|
||||||
|
# @rtrn: true is the file has been updated
|
||||||
def setLastUpdate()
|
def setLastUpdate()
|
||||||
config = {'config' => {'key' => @key,
|
config = {'config' => {'key' => @key,
|
||||||
'lang' => @lang,
|
'lang' => @lang,
|
||||||
|
|
6
mpw
6
mpw
|
@ -101,9 +101,13 @@ cli = Cli.new(lang, config)
|
||||||
# Setup a new config
|
# Setup a new config
|
||||||
if !check_error || !options[:setup].nil?
|
if !check_error || !options[:setup].nil?
|
||||||
cli.setup(lang)
|
cli.setup(lang)
|
||||||
|
end
|
||||||
|
|
||||||
|
cli.decrypt()
|
||||||
|
cli.sync()
|
||||||
|
|
||||||
# Display the item's informations
|
# Display the item's informations
|
||||||
elsif not options[:display].nil?
|
if not options[:display].nil?
|
||||||
cli.display(options[:display], options[:group], options[:type], options[:format])
|
cli.display(options[:display], options[:group], options[:type], options[:format])
|
||||||
|
|
||||||
# Remove an item
|
# Remove an item
|
||||||
|
|
Loading…
Reference in a new issue