#!/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' APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath) require "#{APP_ROOT}/lib/UI/Cli" require "#{APP_ROOT}/lib/Config" require "#{APP_ROOT}/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 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('-d', '--display [SEARCH]', I18n.t('option.show')) do |search| search.nil? ? (options[:display] = '') : (options[:display] = search) end opts.on('-A', '--show-all', I18n.t('option.show_all')) do options[:type] = nil options[:display] = '' end opts.on('-u', '--update ID', I18n.t('option.update')) do |id| options[:update] = id end opts.on('-r', '--remove ID', I18n.t('option.remove')) do |id| options[:remove] = 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] = :csv 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('-F', '--format', I18n.t('option.format')) do options[:format] = 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[:display].nil? cli.display(options[:display], options[:group], options[:type], options[:format]) # Remove an item elsif not options[:remove].nil? cli.remove(options[:remove], 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