1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-05-15 13:25:20 +00:00
mpw/bin/mpw

192 lines
4.4 KiB
Text
Raw Normal View History

2013-07-16 19:42:49 +02:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
require 'optparse'
2013-07-18 21:56:45 +02:00
require 'pathname'
2013-12-25 18:51:41 +01:00
require 'locale'
2014-11-15 19:14:42 +01:00
require 'set'
2013-12-25 18:51:41 +01:00
require 'i18n'
2013-07-16 19:57:48 +02:00
2014-02-01 20:17:55 +01:00
# --------------------------------------------------------- #
# Set local
# --------------------------------------------------------- #
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
2014-11-16 19:39:38 +01:00
if defined?(I18n.enforce_available_locales)
2014-02-01 20:17:55 +01:00
I18n.enforce_available_locales = true
end
2015-02-08 22:54:04 +01:00
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"
2013-12-30 22:14:29 +01:00
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
2015-06-21 23:45:25 +02:00
I18n.load_path = Dir["#{APP_ROOT}/../i18n/cli/*.yml"]
2013-12-30 22:14:29 +01:00
I18n.default_locale = :en
2014-11-16 19:39:38 +01:00
I18n.locale = lang.to_sym
2013-12-25 18:51:41 +01:00
2014-02-01 20:17:55 +01:00
# --------------------------------------------------------- #
# Options
# --------------------------------------------------------- #
2013-07-16 19:42:49 +02:00
options = {}
options[:force] = false
2015-09-27 09:13:43 +02:00
options[:sync] = true
2013-09-09 21:48:42 +02:00
options[:group] = nil
options[:config] = nil
2016-05-05 23:43:34 +02:00
options[:wallet] = nil
2013-07-16 19:42:49 +02:00
OptionParser.new do |opts|
2014-01-26 11:39:53 +01:00
opts.banner = "#{I18n.t('option.usage')}: mpw [options]"
2013-07-16 19:42:49 +02:00
2016-05-07 12:20:46 +02:00
opts.on('-a', '--add', I18n.t('option.add')) do
options[:add] = true
2013-07-16 19:42:49 +02:00
end
2014-01-26 11:39:53 +01:00
opts.on('-A', '--show-all', I18n.t('option.show_all')) do
2015-02-01 12:35:42 +01:00
options[:type] = nil
options[:show] = ''
2013-07-16 19:42:49 +02:00
end
2016-05-07 12:20:46 +02:00
opts.on('-c', '--config CONFIG', I18n.t('option.config')) do |config|
options[:config] = config
2013-07-16 19:42:49 +02:00
end
2016-05-07 13:03:23 +02:00
opts.on('-d', '--delete', I18n.t('option.remove')) do
options[:delete] = true
2013-07-16 19:42:49 +02:00
end
2016-05-08 09:35:02 +02:00
opts.on('-e', '--export', I18n.t('option.export')) do
options[:export] = true
2013-09-09 21:48:42 +02:00
end
2016-05-08 10:13:25 +02:00
opts.on('-f', '--file FILE', I18n.t('option.file')) do |file|
2016-05-08 09:35:02 +02:00
options[:file] = file
end
opts.on('-F', '--force', I18n.t('option.force')) do
2016-05-07 12:20:46 +02:00
options[:force] = true
2013-07-16 19:42:49 +02:00
end
2016-05-07 12:20:46 +02:00
opts.on('-g', '--group GROUP', I18n.t('option.group')) do |group|
options[:group] = group
end
2016-05-07 12:20:46 +02:00
opts.on('-G', '--generate-password [LENGTH]', I18n.t('option.generate_password')) do |length|
puts MPW::MPW::password(length)
exit 0
2013-07-18 22:16:53 +02:00
end
2016-05-07 12:20:46 +02:00
opts.on('-h', '--help', I18n.t('option.help')) do
puts opts
exit 0
2013-07-25 19:51:43 +02:00
end
2016-05-07 13:03:23 +02:00
opts.on('-i', '--id ID', I18n.t('option.id')) do |id|
options[:id] = id
end
2016-05-08 09:35:02 +02:00
opts.on('-I', '--import', I18n.t('option.import')) do
options[:import] = true
2013-07-17 22:31:28 +02:00
end
2016-05-08 09:35:02 +02:00
opts.on('-k', '--key KEY', I18n.t('option.key')) do |key|
2016-05-07 14:36:57 +02:00
options[:key] = key
end
2015-09-27 09:13:43 +02:00
opts.on('-N', '--no-sync', I18n.t('option.no_sync')) do
options[:sync] = false
end
2016-05-07 12:20:46 +02:00
opts.on('-s', '--show [SEARCH]', I18n.t('option.show')) do |search|
search.nil? ? (options[:show] = '') : (options[:show] = search)
2016-05-05 23:43:34 +02:00
end
2016-05-07 12:20:46 +02:00
opts.on('-S', '--setup', I18n.t('option.setup')) do
options[:setup] = true
2014-01-08 17:57:45 +01:00
end
2016-05-07 13:03:23 +02:00
opts.on('-u', '--update', I18n.t('option.update')) do
options[:update] = true
2016-05-07 12:20:46 +02:00
end
opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet
2013-07-16 19:42:49 +02:00
end
2016-05-12 22:43:57 +02:00
opts.on('-W', '--setup-wallet', I18n.t('option.setup_wallet')) do
options[:setup_wallet] = true
end
2013-07-16 19:42:49 +02:00
end.parse!
2014-02-01 20:17:55 +01:00
# --------------------------------------------------------- #
# Main
# --------------------------------------------------------- #
2014-01-29 20:49:39 +01:00
config = MPW::Config.new(options[:config])
2014-02-02 10:16:10 +01:00
check_error = config.checkconfig
2016-05-07 15:05:14 +02:00
cli = MPW::Cli.new(config)
2014-01-15 23:11:39 +01:00
# Setup a new config
2014-11-16 19:39:38 +01:00
if not check_error or not options[:setup].nil?
2014-01-15 23:11:39 +01:00
cli.setup(lang)
2014-11-16 19:39:38 +01:00
elsif not config.check_gpg_key?
2014-03-16 21:28:23 +01:00
cli.setup_gpg_key
2014-01-25 16:53:48 +01:00
end
2016-05-07 10:04:07 +02:00
cli.get_wallet(options[:wallet])
2014-02-02 10:16:10 +01:00
cli.decrypt
2013-07-16 19:42:49 +02:00
# Display the item's informations
2015-02-01 12:35:42 +01:00
if not options[:show].nil?
2015-02-14 10:42:03 +01:00
opts = {search: options[:show],
group: options[:group],
protocol: options[:protocol],
}
cli.display(opts)
2013-07-16 19:42:49 +02:00
# Remove an item
2016-05-07 13:03:23 +02:00
elsif not options[:delete].nil? and not options[:id].nil?
cli.delete(options[:id], options[:force])
2013-07-16 19:42:49 +02:00
# Update an item
2016-05-07 13:03:23 +02:00
elsif not options[:update].nil? and not options[:id].nil?
cli.update(options[:id])
2013-07-16 19:42:49 +02:00
# Add a new item
2016-05-07 14:36:57 +02:00
elsif not options[:add].nil? and options[:key].nil?
2014-02-02 10:16:10 +01:00
cli.add
2013-07-16 19:42:49 +02:00
2016-05-07 14:36:57 +02:00
# Add a new public key in wallet
elsif not options[:add].nil? and not options[:key].nil?
2016-05-08 10:13:25 +02:00
cli.add_key(options[:key], options[:file])
2016-05-07 14:36:57 +02:00
2016-05-07 17:47:24 +02:00
# Delete a public key in wallet
elsif not options[:delete].nil? and not options[:key].nil?
cli.delete_key(options[:key])
2013-07-25 19:51:43 +02:00
# Export
2016-05-08 09:35:02 +02:00
elsif not options[:export].nil? and not options[:file].nil?
cli.export(options[:file])
2013-07-25 19:51:43 +02:00
# Add a new item
2016-05-08 09:35:02 +02:00
elsif not options[:import].nil? and not options[:file].nil?
cli.import(options[:file])
2013-07-16 19:42:49 +02:00
2016-05-12 22:43:57 +02:00
# Setup wallet config
elsif not options[:setup_wallet].nil?
cli.setup_wallet_config
2013-07-16 19:42:49 +02:00
end
2014-02-01 00:30:52 +01:00
cli = nil
2014-01-15 23:11:39 +01:00
2013-07-16 19:42:49 +02:00
exit 0