1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-23 13:57:52 +00:00
mpw/mpw
2014-01-08 17:57:45 +01:00

131 lines
3 KiB
Ruby
Executable file

#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
# info: a simple script who manage your passwords
require 'rubygems'
require 'optparse'
require 'pathname'
require 'locale'
require 'i18n'
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
require "#{APP_ROOT}/lib/Cli.rb"
require "#{APP_ROOT}/lib/MPW.rb"
lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.load_path = Dir["#{APP_ROOT}/i18n/*.yml"]
I18n.default_locale = :en
I18n.locale = lang.to_sym
options = {}
options[:force] = false
options[:format] = false
options[:group] = nil
options[:config] = nil
OptionParser.new do |opts|
opts.banner = "#{I18n.t('cli.option.usage')}: mpw [options]"
opts.on('-d', '--display [SEARCH]', I18n.t('cli.option.show')) do |search|
search.nil? ? (options[:display] = '') : (options[:display] = search)
end
opts.on('-A', '--show-all', I18n.t('cli.option.show_all')) do
options[:type] = nil
options[:display] = ''
end
opts.on('-u', '--update ID', I18n.t('cli.option.update')) do |id|
options[:update] = id
end
opts.on('-r', '--remove ID', I18n.t('cli.option.remove')) do |id|
options[:remove] = id
end
opts.on('-g', '--group GROUP', I18n.t('cli.option.group')) do |group|
options[:group] = group
end
opts.on('-a', '--add', I18n.t('cli.option.add')) do
options[:add] = true
end
opts.on('-c', '--config CONFIG', I18n.t('cli.option.config')) do |config|
options[:config] = config
end
opts.on('-S', '--setup', I18n.t('cli.option.setup')) do
options[:setup] = true
end
opts.on('-p', '--protocol PROTOCOL', I18n.t('cli.option.protocol')) do |type|
options[:type] = type
end
opts.on('-e', '--export FILE', I18n.t('cli.option.export')) do |file|
options[:export] = file
end
opts.on('-i', '--import FILE', I18n.t('cli.option.import')) do |file|
options[:import] = file
end
opts.on('-f', '--force', I18n.t('cli.option.force')) do
options[:force] = true
end
opts.on('-F', '--format', I18n.t('cli.option.format')) do
options[:format] = true
end
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
puts opts
exit 0
end
end.parse!
cli = Cli.new(lang, options[:config])
# 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])
# Remove an item
elsif not options[:remove].nil?
cli.remove(options[:remove], options[:force])
# Update an item
elsif not options[:update].nil?
cli.update(options[:update])
# Add a new item
elsif not options[:add].nil?
cli.add()
# Export
elsif not options[:export].nil?
cli.export(options[:export])
# Add a new item
elsif not options[:import].nil?
cli.import(options[:import], options[:force])
# Interactive mode
else
cli.interactive
end
exit 0