From 3d99ac80deb10dcd836f0a9c3c3b1bc35c8a8d46 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Thu, 13 Oct 2016 21:59:51 +0200 Subject: [PATCH] new binary for config --- bin/mpw-config | 91 +++++++++++++++++++++++++++++++++++++++++++++++ lib/mpw/cli.rb | 44 ++++++++++++----------- lib/mpw/config.rb | 3 +- 3 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 bin/mpw-config diff --git a/bin/mpw-config b/bin/mpw-config new file mode 100644 index 0000000..ebde9e2 --- /dev/null +++ b/bin/mpw-config @@ -0,0 +1,91 @@ +#!/usr/bin/ruby +# MPW is a software to crypt and manage your passwords +# Copyright (C) 2016 Adrien Waksberg +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +$: << File.expand_path('../../lib', __FILE__) + +require 'optparse' +require 'locale' +require 'set' +require 'i18n' +require 'mpw/config' +require 'mpw/cli' + +# --------------------------------------------------------- # +# 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["#{File.expand_path('../../i18n', __FILE__)}/*.yml"] +I18n.default_locale = :en +I18n.locale = lang.to_sym + +# --------------------------------------------------------- # +# Options +# --------------------------------------------------------- # + +options = {} + +OptionParser.new do |opts| + opts.banner = "#{I18n.t('option.usage')}: mpw [options]" + + opts.on('-c', '--config CONFIG', I18n.t('option.config')) do |config| + options[:config] = config + end + + opts.on('-g', '--gpg-exe PATH', I18n.t('option.lang')) do |gpg_exe| + options[:gpg_exe] = gpg_exe + end + + opts.on('-h', '--help', I18n.t('option.help')) do + puts opts + exit 0 + end + + opts.on('--init', I18n.t('option.init')) do + options[:init] = true + end + + opts.on('-k', '--key GPG_KEY', I18n.t('option.lang')) do |gpg_key| + options[:gpg_key] = gpg_key + end + + opts.on('-l', '--lang LANG', I18n.t('option.lang')) do |lang| + options[:lang] = lang + end + + opts.on('-w', '--wallet-dir LANG', I18n.t('option.lang')) do |wallet_dir| + options[:wallet_dir] = wallet_dir + end +end.parse! + +config = MPW::Config.new(options[:config]) +cli = MPW::Cli.new(config, options[:clipboard], options[:sync], options[:otp]) + +if not options[:init].nil? + cli.setup(options) + cli.setup_gpg_key(options[:gpg_key]) if not config.check_gpg_key? + exit 0 +end + +cli.set_config(options) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index cb061c7..0547ac4 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -17,6 +17,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require 'readline' +require 'locale' require 'i18n' require 'colorize' require 'highline/import' @@ -40,17 +41,32 @@ class Cli @otp = otp end + # Change a parameter int the config after init + # @args: options -> param to change + def set_config(options) + raise I18n.t('error.config.check') if not @config.is_valid? + + gpg_key = options[:gpg_key] || @config.key + lang = options[:lang] || @config.lang + wallet_dir = options[:wallet_dir] || @config.wallet_dir + gpg_exe = options[:gpg_exe] || @config.gpg_exe + + @config.setup(gpg_key, lang, wallet_dir, gpg_exe) + rescue Exception => e + puts "#{I18n.t('display.error')} #15: #{e}".red + exit 2 + end + # Create a new config file # @args: language -> the software language - def setup(language) + def setup(options) @config.is_valid? - options = text_editor('setup_form', language) - language = options[:language] || language + lang = options[:lang] || Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1] - I18n.locale = language.to_sym + I18n.locale = lang.to_sym - @config.setup(options[:gpg_key], language, options[:wallet_dir], options[:gpg_exe]) + @config.setup(options[:gpg_key], lang, options[:wallet_dir], options[:gpg_exe]) raise I18n.t('error.config.check') if not @config.is_valid? @@ -61,16 +77,11 @@ class Cli end # Setup a new GPG key - def setup_gpg_key + # @args: gpg_key -> the key name + def setup_gpg_key(gpg_key) puts I18n.t('form.setup_gpg_key.title') puts '--------------------' ask = ask(I18n.t('form.setup_gpg_key.ask')).to_s - - if not ['Y', 'y', 'O', 'o'].include?(ask) - raise I18n.t('form.setup_gpg_key.no_create') - end - - name = ask(I18n.t('form.setup_gpg_key.name')).to_s password = ask(I18n.t('form.setup_gpg_key.password')) {|q| q.echo = false} confirm = ask(I18n.t('form.setup_gpg_key.confirm_password')) {|q| q.echo = false} @@ -78,16 +89,9 @@ class Cli raise I18n.t('form.setup_gpg_key.error_password') end - length = ask(I18n.t('form.setup_gpg_key.length')).to_s - expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s - password = password.to_s - - length = length.nil? or length.empty? ? 2048 : length.to_i - expire = expire.nil? or expire.empty? ? 0 : expire.to_i - puts I18n.t('form.setup_gpg_key.wait') - @config.setup_gpg_key(password, name, length, expire) + @config.setup_gpg_key(password.to_s, gpg_key) puts "#{I18n.t('form.setup_gpg_key.valid')}".green rescue Exception => e diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index 90df409..b504c2a 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -57,12 +57,11 @@ class Config # gpg_exe -> the path of gpg executable # @rtrn: true if le config file is create def setup(key, lang, wallet_dir, gpg_exe) - if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ raise I18n.t('error.config.key_bad_format') end - if wallet_dir.empty? + if wallet_dir.to_s.empty? wallet_dir = "#{@config_dir}/wallets" end