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