1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-22 02:40:04 +00:00
mpw/mpw

164 lines
3.7 KiB
Text
Raw Normal View History

2013-07-16 19:42:49 +02:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
# info: a simple script who manage your passwords
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-18 21:56:45 +02:00
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
2014-09-12 08:41:27 +02:00
require "#{APP_ROOT}/lib/UI/Cli"
require "#{APP_ROOT}/lib/Config"
require "#{APP_ROOT}/lib/MPW"
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
2013-12-30 22:14:29 +01:00
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
2014-11-16 19:39:38 +01: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
2013-09-09 21:48:42 +02:00
options[:group] = nil
options[:config] = 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
2014-01-26 11:39:53 +01:00
opts.on('-d', '--display [SEARCH]', I18n.t('option.show')) do |search|
2013-07-16 19:42:49 +02:00
search.nil? ? (options[:display] = '') : (options[:display] = search)
end
2014-01-26 11:39:53 +01:00
opts.on('-A', '--show-all', I18n.t('option.show_all')) do
2013-09-19 22:18:59 +02:00
options[:type] = nil
2013-12-26 15:20:14 +01:00
options[:display] = ''
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
2014-01-26 11:39:53 +01:00
opts.on('-r', '--remove ID', I18n.t('option.remove')) do |id|
2013-07-16 19:42:49 +02:00
options[:remove] = id
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('-p', '--protocol PROTOCOL', I18n.t('option.protocol')) do |type|
2013-07-16 19:42:49 +02:00
options[:type] = type
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
2014-11-15 19:14:42 +01:00
options[:type] = :csv
end
opts.on('-t', '--type TYPE', I18n.t('option.type')) do |type|
options[:type] = type.to_sym
2013-07-25 19:51:43 +02:00
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
end
2014-01-26 11:39:53 +01:00
opts.on('-f', '--force', I18n.t('option.force')) do
2013-07-17 22:31:28 +02:00
options[:force] = true
end
2014-01-26 11:39:53 +01:00
opts.on('-F', '--format', I18n.t('option.format')) do
options[:format] = true
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
2014-09-07 10:00:46 +02:00
cli = 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
2014-02-02 10:16:10 +01:00
cli.decrypt
cli.sync
2013-07-16 19:42:49 +02:00
# Display the item's informations
2014-01-25 16:53:48 +01:00
if not options[:display].nil?
2015-01-17 00:05:40 +01:00
cli.display(options[:display], options[:group], options[:type])
2013-07-16 19:42:49 +02:00
# Remove an item
elsif not options[:remove].nil?
cli.remove(options[:remove], 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?
2014-11-15 19:14:42 +01:00
cli.export(options[:export], options[:type])
2013-07-25 19:51:43 +02:00
# Add a new item
elsif not options[:import].nil?
2014-12-06 23:32:09 +01:00
cli.import(options[:import], options[:type], options[:force])
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