mpw/bin/mpw

162 lines
3.6 KiB
Text
Raw Normal View History

2013-07-16 19:42:49 +02:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
require 'rubygems'
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
options[:format] = 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
2015-02-01 12:35:42 +01:00
opts.on('-s', '--show [SEARCH]', I18n.t('option.show')) do |search|
search.nil? ? (options[:show] = '') : (options[:show] = search)
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
2014-01-26 11:39:53 +01:00
opts.on('-u', '--update ID', I18n.t('option.update')) do |id|
2013-07-16 19:42:49 +02:00
options[:update] = id
end
2015-02-01 12:35:42 +01:00
opts.on('-d', '--delete ID', I18n.t('option.remove')) do |id|
options[:delete] = id
2013-07-16 19:42:49 +02:00
end
2014-01-26 11:39:53 +01:00
opts.on('-g', '--group GROUP', I18n.t('option.group')) do |group|
2013-09-09 21:48:42 +02:00
options[:group] = group
end
2014-01-26 11:39:53 +01:00
opts.on('-a', '--add', I18n.t('option.add')) do
2013-07-16 19:42:49 +02:00
options[:add] = true
end
2014-01-26 11:39:53 +01:00
opts.on('-c', '--config CONFIG', I18n.t('option.config')) do |config|
options[:config] = config
end
2014-01-26 11:39:53 +01:00
opts.on('-S', '--setup', I18n.t('option.setup')) do
2013-07-18 22:16:53 +02:00
options[:setup] = true
end
2014-01-26 11:39:53 +01:00
opts.on('-e', '--export FILE', I18n.t('option.export')) do |file|
2013-07-25 19:51:43 +02:00
options[:export] = file
end
2014-01-26 11:39:53 +01:00
opts.on('-i', '--import FILE', I18n.t('option.import')) do |file|
2013-07-25 19:51:43 +02:00
options[:import] = file
2013-07-17 22:31:28 +02:00
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-05 23:43:34 +02:00
opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet
end
2014-01-26 11:39:53 +01:00
opts.on('-G', '--generate-password [LENGTH]', I18n.t('option.generate_password')) do |length|
2014-04-29 23:24:05 +02:00
puts MPW::MPW::password(length)
2014-01-08 17:57:45 +01:00
exit 0
end
2014-01-26 11:39:53 +01:00
opts.on('-h', '--help', I18n.t('option.help')) do
2013-07-16 19:42:49 +02:00
puts opts
exit 0
end
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-05 23:43:34 +02:00
cli = Cli.new(config, options[:wallet])
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
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
2015-02-01 12:35:42 +01:00
elsif not options[:delete].nil?
cli.delete(options[:delete], 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?
2014-02-02 10:16:10 +01: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])
2013-07-25 19:51:43 +02:00
# Add a new item
elsif not options[:import].nil?
cli.import(options[:import])
2013-07-16 19:42:49 +02:00
2013-09-08 17:56:10 +02:00
# Interactive mode
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