#!/usr/bin/ruby # author: nishiki # mail: nishiki@yaegashi.fr require 'rubygems' require 'optparse' require 'pathname' require 'locale' require 'set' require 'i18n' # --------------------------------------------------------- # # 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) # TODO require "#{APP_ROOT}/../lib/mpw/mpw.rb" require "#{APP_ROOT}/../lib/mpw/config.rb" require "#{APP_ROOT}/../lib/mpw/ui/cli.rb" 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[:sync] = true options[:group] = nil options[:config] = nil options[:wallet] = 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('-e', '--export FILE', I18n.t('option.export')) do |file| options[:export] = file end opts.on('-i', '--import FILE', I18n.t('option.import')) do |file| options[:import] = file end opts.on('-N', '--no-sync', I18n.t('option.no_sync')) do options[:sync] = false end opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet 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, options[:wallet]) # 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 # Display the item's informations if not options[:show].nil? opts = {search: options[:show], group: options[:group], protocol: options[:protocol], } cli.display(opts) # 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]) # Add a new item elsif not options[:import].nil? cli.import(options[:import]) # Interactive mode end cli = nil exit 0