1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-10-27 10:43:20 +00:00
mpw/bin/mpw
2016-05-08 10:13:25 +02:00

184 lines
4.2 KiB
Ruby
Executable file

#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
require 'rubygems'
require 'optparse'
require 'pathname'
require 'locale'
require 'set'
require 'i18n'
# --------------------------------------------------------- #
# 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
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
# TODO
require "#{APP_ROOT}/../lib/mpw/mpw.rb"
require "#{APP_ROOT}/../lib/mpw/config.rb"
require "#{APP_ROOT}/../lib/mpw/ui/cli.rb"
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[:sync] = true
options[:group] = nil
options[:config] = nil
options[:wallet] = nil
OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw [options]"
opts.on('-a', '--add', I18n.t('option.add')) do
options[:add] = true
end
opts.on('-A', '--show-all', I18n.t('option.show_all')) do
options[:type] = nil
options[:show] = ''
end
opts.on('-c', '--config CONFIG', I18n.t('option.config')) do |config|
options[:config] = config
end
opts.on('-d', '--delete', I18n.t('option.remove')) do
options[:delete] = true
end
opts.on('-e', '--export', I18n.t('option.export')) do
options[:export] = true
end
opts.on('-f', '--file FILE', I18n.t('option.file')) do |file|
options[:file] = file
end
opts.on('-F', '--force', I18n.t('option.force')) do
options[:force] = true
end
opts.on('-g', '--group GROUP', I18n.t('option.group')) do |group|
options[:group] = group
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
opts.on('-i', '--id ID', I18n.t('option.id')) do |id|
options[:id] = id
end
opts.on('-I', '--import', I18n.t('option.import')) do
options[:import] = true
end
opts.on('-k', '--key KEY', I18n.t('option.key')) do |key|
options[:key] = key
end
opts.on('-N', '--no-sync', I18n.t('option.no_sync')) do
options[:sync] = false
end
opts.on('-s', '--show [SEARCH]', I18n.t('option.show')) do |search|
search.nil? ? (options[:show] = '') : (options[:show] = search)
end
opts.on('-S', '--setup', I18n.t('option.setup')) do
options[:setup] = true
end
opts.on('-u', '--update', I18n.t('option.update')) do
options[:update] = true
end
opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet
end
end.parse!
# --------------------------------------------------------- #
# Main
# --------------------------------------------------------- #
config = MPW::Config.new(options[:config])
check_error = config.checkconfig
cli = MPW::Cli.new(config)
# Setup a new config
if not check_error or not options[:setup].nil?
cli.setup(lang)
elsif not config.check_gpg_key?
cli.setup_gpg_key
end
cli.get_wallet(options[:wallet])
cli.decrypt
# Display the item's informations
if not options[:show].nil?
opts = {search: options[:show],
group: options[:group],
protocol: options[:protocol],
}
cli.display(opts)
# Remove an item
elsif not options[:delete].nil? and not options[:id].nil?
cli.delete(options[:id], options[:force])
# Update an item
elsif not options[:update].nil? and not options[:id].nil?
cli.update(options[:id])
# Add a new item
elsif not options[:add].nil? and options[:key].nil?
cli.add
# Add a new public key in wallet
elsif not options[:add].nil? and not options[:key].nil?
cli.add_key(options[:key], options[:file])
# Delete a public key in wallet
elsif not options[:delete].nil? and not options[:key].nil?
cli.delete_key(options[:key])
# Export
elsif not options[:export].nil? and not options[:file].nil?
cli.export(options[:file])
# Add a new item
elsif not options[:import].nil? and not options[:file].nil?
cli.import(options[:file])
end
cli = nil
exit 0