2013-07-16 19:42:49 +02:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
|
|
|
# info: a simple script who manage your passwords
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'optparse'
|
2013-07-18 21:56:45 +02:00
|
|
|
require 'pathname'
|
2013-12-25 18:51:41 +01:00
|
|
|
require 'locale'
|
|
|
|
require 'i18n'
|
2013-07-18 21:56:45 +02:00
|
|
|
|
|
|
|
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
|
2013-09-04 19:48:06 +02:00
|
|
|
require "#{APP_ROOT}/lib/Cli.rb"
|
2014-01-08 17:57:45 +01:00
|
|
|
require "#{APP_ROOT}/lib/MPW.rb"
|
2013-07-16 19:57:48 +02:00
|
|
|
|
2013-12-26 10:47:54 +01:00
|
|
|
lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
|
2013-12-25 18:51:41 +01:00
|
|
|
|
2013-12-30 22:14:29 +01:00
|
|
|
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
|
2013-12-30 21:00:07 +01:00
|
|
|
I18n.load_path = Dir["#{APP_ROOT}/i18n/*.yml"]
|
2013-12-30 22:14:29 +01:00
|
|
|
I18n.default_locale = :en
|
2013-12-25 18:51:41 +01:00
|
|
|
I18n.locale = lang.to_sym
|
|
|
|
|
2013-07-16 19:42:49 +02:00
|
|
|
options = {}
|
2013-09-02 19:50:40 +02:00
|
|
|
options[:force] = false
|
|
|
|
options[:format] = false
|
2013-09-09 21:48:42 +02:00
|
|
|
options[:group] = nil
|
2013-12-30 20:15:00 +01:00
|
|
|
options[:config] = nil
|
2013-09-02 19:50:40 +02:00
|
|
|
|
2013-07-16 19:42:49 +02:00
|
|
|
OptionParser.new do |opts|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.banner = "#{I18n.t('cli.option.usage')}: mpw [options]"
|
2013-07-16 19:42:49 +02:00
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-d', '--display [SEARCH]', I18n.t('cli.option.show')) do |search|
|
2013-07-16 19:42:49 +02:00
|
|
|
search.nil? ? (options[:display] = '') : (options[:display] = search)
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:57:45 +01:00
|
|
|
opts.on('-A', '--show-all', I18n.t('cli.option.show_all')) do
|
2013-09-19 22:18:59 +02:00
|
|
|
options[:type] = nil
|
2013-12-26 15:20:14 +01:00
|
|
|
options[:display] = ''
|
2013-07-16 19:42:49 +02:00
|
|
|
end
|
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-u', '--update ID', I18n.t('cli.option.update')) do |id|
|
2013-07-16 19:42:49 +02:00
|
|
|
options[:update] = id
|
|
|
|
end
|
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-r', '--remove ID', I18n.t('cli.option.remove')) do |id|
|
2013-07-16 19:42:49 +02:00
|
|
|
options[:remove] = id
|
|
|
|
end
|
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-g', '--group GROUP', I18n.t('cli.option.group')) do |group|
|
2013-09-09 21:48:42 +02:00
|
|
|
options[:group] = group
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:57:45 +01:00
|
|
|
opts.on('-a', '--add', I18n.t('cli.option.add')) do
|
2013-07-16 19:42:49 +02:00
|
|
|
options[:add] = true
|
|
|
|
end
|
|
|
|
|
2013-12-30 20:15:00 +01:00
|
|
|
opts.on('-c', '--config CONFIG', I18n.t('cli.option.config')) do |config|
|
|
|
|
options[:config] = config
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:57:45 +01:00
|
|
|
opts.on('-S', '--setup', I18n.t('cli.option.setup')) do
|
2013-07-18 22:16:53 +02:00
|
|
|
options[:setup] = true
|
|
|
|
end
|
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-p', '--protocol PROTOCOL', I18n.t('cli.option.protocol')) do |type|
|
2013-07-16 19:42:49 +02:00
|
|
|
options[:type] = type
|
|
|
|
end
|
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-e', '--export FILE', I18n.t('cli.option.export')) do |file|
|
2013-07-25 19:51:43 +02:00
|
|
|
options[:export] = file
|
|
|
|
end
|
|
|
|
|
2013-12-26 15:20:14 +01:00
|
|
|
opts.on('-i', '--import FILE', I18n.t('cli.option.import')) do |file|
|
2013-07-25 19:51:43 +02:00
|
|
|
options[:import] = file
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:57:45 +01:00
|
|
|
opts.on('-f', '--force', I18n.t('cli.option.force')) do
|
2013-07-17 22:31:28 +02:00
|
|
|
options[:force] = true
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:57:45 +01:00
|
|
|
opts.on('-F', '--format', I18n.t('cli.option.format')) do
|
2013-09-02 19:50:40 +02:00
|
|
|
options[:format] = true
|
|
|
|
end
|
|
|
|
|
2014-01-08 17:57:45 +01:00
|
|
|
opts.on('-G', '--generate-password [LENGTH]', I18n.t('cli.option.generate_password')) do |length|
|
|
|
|
puts MPW::generatePassword(length)
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-h', '--help', I18n.t('cli.option.help')) do
|
2013-07-16 19:42:49 +02:00
|
|
|
puts opts
|
|
|
|
exit 0
|
|
|
|
end
|
|
|
|
end.parse!
|
|
|
|
|
2014-01-15 23:11:39 +01:00
|
|
|
config = MPWConfig.new(options[:config])
|
|
|
|
check_error = config.checkconfig()
|
2013-09-02 19:50:40 +02:00
|
|
|
|
2014-01-15 23:11:39 +01:00
|
|
|
cli = Cli.new(lang, config)
|
|
|
|
cli.sync()
|
|
|
|
|
|
|
|
# Setup a new config
|
|
|
|
if !check_error || !options[:setup].nil?
|
|
|
|
cli.setup(lang)
|
2013-07-16 19:42:49 +02:00
|
|
|
|
|
|
|
# Display the item's informations
|
2013-07-18 22:16:53 +02:00
|
|
|
elsif not options[:display].nil?
|
2013-09-09 21:48:42 +02:00
|
|
|
cli.display(options[:display], options[:group], options[:type], options[:format])
|
2013-07-16 19:42:49 +02:00
|
|
|
|
|
|
|
# Remove an item
|
|
|
|
elsif not options[:remove].nil?
|
2013-09-02 19:50:40 +02:00
|
|
|
cli.remove(options[:remove], options[:force])
|
2013-07-16 19:42:49 +02:00
|
|
|
|
|
|
|
# Update an item
|
|
|
|
elsif not options[:update].nil?
|
2013-07-17 22:31:28 +02:00
|
|
|
cli.update(options[:update])
|
2013-07-16 19:42:49 +02:00
|
|
|
|
|
|
|
# Add a new item
|
|
|
|
elsif not options[:add].nil?
|
2013-07-17 22:31:28 +02:00
|
|
|
cli.add()
|
2013-07-16 19:42:49 +02:00
|
|
|
|
2013-07-25 19:51:43 +02:00
|
|
|
# Export
|
|
|
|
elsif not options[:export].nil?
|
|
|
|
cli.export(options[:export])
|
|
|
|
|
|
|
|
# Add a new item
|
|
|
|
elsif not options[:import].nil?
|
2013-09-02 19:50:40 +02:00
|
|
|
cli.import(options[:import], options[:force])
|
2013-07-16 19:42:49 +02:00
|
|
|
|
2013-09-08 17:56:10 +02:00
|
|
|
# Interactive mode
|
|
|
|
else
|
2014-01-15 23:11:39 +01:00
|
|
|
begin
|
|
|
|
cli.interactive()
|
|
|
|
rescue SystemExit, Interrupt
|
|
|
|
cli.sync()
|
|
|
|
cli = nil
|
|
|
|
return 1
|
|
|
|
end
|
2013-07-16 19:42:49 +02:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:11:39 +01:00
|
|
|
cli.sync()
|
|
|
|
cli = nil
|
|
|
|
|
2013-07-16 19:42:49 +02:00
|
|
|
exit 0
|