1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +00:00
mpw/mpw
2014-04-29 23:24:05 +02:00

164 lines
3.7 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}/MPW/UI/Cli"
require "#{APP_ROOT}/MPW/Config"
require "#{APP_ROOT}/MPW/MPW"
# --------------------------------------------------------- #
# Set local
# --------------------------------------------------------- #
lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
if defined? I18n.enforce_available_locales
I18n.enforce_available_locales = true
end
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.load_path = Dir["#{APP_ROOT}/i18n/cli/*.yml"]
I18n.default_locale = :en
I18n.locale = lang.to_sym
# --------------------------------------------------------- #
# Options
# --------------------------------------------------------- #
options = {}
options[:force] = false
options[:format] = false
options[:group] = nil
options[:config] = nil
OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw [options]"
opts.on('-d', '--display [SEARCH]', I18n.t('option.show')) do |search|
search.nil? ? (options[:display] = '') : (options[:display] = search)
end
opts.on('-A', '--show-all', I18n.t('option.show_all')) do
options[:type] = nil
options[:display] = ''
end
opts.on('-u', '--update ID', I18n.t('option.update')) do |id|
options[:update] = id
end
opts.on('-r', '--remove ID', I18n.t('option.remove')) do |id|
options[:remove] = id
end
opts.on('-g', '--group GROUP', I18n.t('option.group')) do |group|
options[:group] = group
end
opts.on('-a', '--add', I18n.t('option.add')) do
options[:add] = true
end
opts.on('-c', '--config CONFIG', I18n.t('option.config')) do |config|
options[:config] = config
end
opts.on('-S', '--setup', I18n.t('option.setup')) do
options[:setup] = true
end
opts.on('-p', '--protocol PROTOCOL', I18n.t('option.protocol')) do |type|
options[:type] = type
end
opts.on('-e', '--export FILE', I18n.t('option.export')) do |file|
options[:export] = file
end
opts.on('-i', '--import FILE', I18n.t('option.import')) do |file|
options[:import] = file
end
opts.on('-f', '--force', I18n.t('option.force')) do
options[:force] = true
end
opts.on('-F', '--format', I18n.t('option.format')) do
options[:format] = true
end
opts.on('-G', '--generate-password [LENGTH]', I18n.t('option.generate_password')) do |length|
puts MPW::MPW::password(length)
exit 0
end
opts.on('-h', '--help', I18n.t('option.help')) do
puts opts
exit 0
end
end.parse!
# --------------------------------------------------------- #
# Main
# --------------------------------------------------------- #
config = MPW::Config.new(options[:config])
check_error = config.checkconfig
cli = Cli.new(lang, config)
# Setup a new config
if !check_error || !options[:setup].nil?
cli.setup(lang)
elsif !config.check_gpg_key?
cli.setup_gpg_key
end
cli.decrypt
cli.sync
# Display the item's informations
if 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
begin
cli.interactive
rescue SystemExit, Interrupt
cli = nil
return 1
end
end
cli = nil
exit 0