1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-10-27 10:43:20 +00:00
mpw/mpw
2015-02-08 22:54:04 +01:00

160 lines
3.6 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 'set'
require 'i18n'
require_relative 'lib/UI/Cli'
require_relative 'lib/Config'
require_relative 'lib/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
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
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('-s', '--show [SEARCH]', I18n.t('option.show')) do |search|
search.nil? ? (options[:show] = '') : (options[:show] = search)
end
opts.on('-A', '--show-all', I18n.t('option.show_all')) do
options[:type] = nil
options[:show] = ''
end
opts.on('-u', '--update ID', I18n.t('option.update')) do |id|
options[:update] = id
end
opts.on('-d', '--delete ID', I18n.t('option.remove')) do |id|
options[:delete] = 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
options[:type] = :yaml
end
opts.on('-t', '--type TYPE', I18n.t('option.type')) do |type|
options[:type] = type.to_sym
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('-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(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.decrypt
cli.sync
# Display the item's informations
if not options[:show].nil?
cli.display(options[:show], options[:group], options[:type])
# Remove an item
elsif not options[:delete].nil?
cli.delete(options[:delete], 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], options[:type])
# Add a new item
elsif not options[:import].nil?
cli.import(options[:import], options[:type], options[:force])
# Interactive mode
end
cli = nil
exit 0