mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-22 02:40:04 +00:00
restruc the code
This commit is contained in:
parent
1a9fb07950
commit
a77266daf7
2 changed files with 37 additions and 21 deletions
23
lib/Cli.rb
23
lib/Cli.rb
|
@ -19,12 +19,8 @@ class Cli
|
||||||
# Constructor
|
# Constructor
|
||||||
# @args: lang -> the operating system language
|
# @args: lang -> the operating system language
|
||||||
# config_file -> a specify config file
|
# config_file -> a specify config file
|
||||||
def initialize(lang, config_file=nil)
|
def initialize(lang, config)
|
||||||
@config = MPWConfig.new(config_file)
|
@config = config
|
||||||
|
|
||||||
if not @config.checkconfig()
|
|
||||||
setup(lang)
|
|
||||||
end
|
|
||||||
|
|
||||||
@mpw = MPW.new(@config.file_gpg, @config.key)
|
@mpw = MPW.new(@config.file_gpg, @config.key)
|
||||||
if not decrypt()
|
if not decrypt()
|
||||||
|
@ -37,16 +33,23 @@ class Cli
|
||||||
@sync.disable()
|
@sync.disable()
|
||||||
elsif !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix)
|
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}"
|
puts "#{I18n.t('cli.sync.not_connect')}:\n#{@sync.error_msg}"
|
||||||
else
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Destructor
|
||||||
|
def finalize()
|
||||||
|
@sync.close()
|
||||||
|
end
|
||||||
|
|
||||||
|
# Sync the data with the server
|
||||||
|
def sync()
|
||||||
begin
|
begin
|
||||||
@mpw.sync(@sync.get(@passwd), @config.last_update)
|
@mpw.sync(@sync.get(@passwd), @config.last_update)
|
||||||
@sync.update(File.open(@config.file_gpg).read)
|
@sync.update(File.open(@config.file_gpg).read)
|
||||||
@config.setLastUpdate()
|
@config.setLastUpdate()
|
||||||
|
puts @sync.error_msg
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
puts "#{I18n.t('cli.sync.error')}:\n#{e}"
|
puts "#{I18n.t('cli.sync.error')}:\n#{e}"
|
||||||
else
|
|
||||||
@sync.close()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
23
mpw
23
mpw
|
@ -93,13 +93,17 @@ OptionParser.new do |opts|
|
||||||
end
|
end
|
||||||
end.parse!
|
end.parse!
|
||||||
|
|
||||||
|
config = MPWConfig.new(options[:config])
|
||||||
|
check_error = config.checkconfig()
|
||||||
|
|
||||||
cli = Cli.new(lang, options[:config])
|
cli = Cli.new(lang, config)
|
||||||
|
cli.sync()
|
||||||
|
|
||||||
|
# Setup a new config
|
||||||
|
if !check_error || !options[:setup].nil?
|
||||||
|
cli.setup(lang)
|
||||||
|
|
||||||
# Display the item's informations
|
# Display the item's informations
|
||||||
if not options[:setup].nil?
|
|
||||||
cli.setup()
|
|
||||||
|
|
||||||
elsif not options[:display].nil?
|
elsif not options[:display].nil?
|
||||||
cli.display(options[:display], options[:group], options[:type], options[:format])
|
cli.display(options[:display], options[:group], options[:type], options[:format])
|
||||||
|
|
||||||
|
@ -125,7 +129,16 @@ elsif not options[:import].nil?
|
||||||
|
|
||||||
# Interactive mode
|
# Interactive mode
|
||||||
else
|
else
|
||||||
cli.interactive
|
begin
|
||||||
|
cli.interactive()
|
||||||
|
rescue SystemExit, Interrupt
|
||||||
|
cli.sync()
|
||||||
|
cli = nil
|
||||||
|
return 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
cli.sync()
|
||||||
|
cli = nil
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Add table
Reference in a new issue