1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-23 05:47:53 +00:00

feat: add option to set default wallet

This commit is contained in:
Adrien Waksberg 2017-02-22 21:51:06 +01:00
parent 50d88fc970
commit c841123ac0
5 changed files with 44 additions and 29 deletions

View file

@ -34,6 +34,10 @@ OptionParser.new do |opts|
options[:config] = config
end
opts.on('-d', '--default-wallet NAME', I18n.t('option.default_wallet')) do |default_wallet|
values[:default_wallet] = default_wallet
end
opts.on('-g', '--gpg-exe PATH', I18n.t('option.gpg_exe')) do |gpg_exe|
values[:gpg_exe] = gpg_exe
end
@ -64,12 +68,13 @@ end.parse!
config = MPW::Config.new(options[:config])
cli = MPW::Cli.new(config, nil)
if not options[:init].nil?
if options.has_key?(:init)
cli.setup(values)
cli.load_config
cli.get_wallet
cli.setup_gpg_key(values[:gpg_key])
cli.setup_wallet_config
else
cli.load_config
cli.set_config(values)
end

View file

@ -54,6 +54,7 @@ en:
alpha: "Use letter to generate a password"
config: "Specify the configuration file to use"
clipboard: "Disable the clipboard feature"
default_wallet: "Specify the default wallet to use"
delete_gpg_key: "Delete the wallet's share with an other GPG key"
export: "Export a wallet in an yaml file"
file_export: "Specify the file where export data"
@ -128,6 +129,8 @@ en:
file_not_exist: "The import file doesn't exist!"
valid: "The import is succesfull!"
not_valid: "No data to import!"
set_config:
valid: "The config file has been edited!"
setup_config:
title: "Setup a new config file"
lang: "Choose your language (en, fr, ...) [default=%{lang}]: "

View file

@ -54,6 +54,7 @@ fr:
alpha: "Utilise des lettres dans la génération d'un mot de passe"
config: "Spécifie le fichier de configuration à utiliser"
clipboard: "Désactive la fonction presse papier"
default_wallet: "Spécifie le porte-feuille à utiliser par défaut"
delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG"
export: "Exporte un portefeuille dans un fichier yaml"
file_export: "Spécifie le fichier où exporter les données"
@ -128,6 +129,8 @@ fr:
file_not_exist: "Le fichier d'import n'existe pas"
valid: "L'import est un succès!"
not_valid: "Aucune donnée à importer!"
set_config:
valid: "Le fichier de configuration a bien été modifié!"
setup_config:
title: "Création d'un nouveau fichier de configuration"
lang: "Choisissez votre langue (en, fr, ...) [défaut=%{lang}]: "

View file

@ -43,9 +43,12 @@ class Cli
gpg_key = options[:gpg_key] || @config.key
lang = options[:lang] || @config.lang
wallet_dir = options[:wallet_dir] || @config.wallet_dir
default_wallet = options[:default_wallet] || @config.default_wallet
gpg_exe = options[:gpg_exe] || @config.gpg_exe
@config.setup(gpg_key, lang, wallet_dir, gpg_exe)
@config.setup(gpg_key, lang, wallet_dir, default_wallet, gpg_exe)
puts "#{I18n.t('form.set_config.valid')}".green
rescue Exception => e
puts "#{I18n.t('display.error')} #15: #{e}".red
exit 2
@ -341,7 +344,9 @@ class Cli
def list_wallet
wallets = []
Dir.glob("#{@config.wallet_dir}/*.mpw").each do |f|
wallets << File.basename(f, '.mpw')
wallet = File.basename(f, '.mpw')
wallet += ' *'.green if wallet == @config.default_wallet
wallets << wallet
end
table_list('wallets', wallets)
@ -355,6 +360,8 @@ class Cli
if wallets.length == 1
@wallet_file = wallets[0]
elsif not @config.default_wallet.to_s.empty?
@wallet_file = "#{@config.wallet_dir}/#{@config.default_wallet}.mpw"
else
@wallet_file = "#{@config.wallet_dir}/default.mpw"
end

View file

@ -29,6 +29,7 @@ class Config
attr_accessor :key
attr_accessor :lang
attr_accessor :config_dir
attr_accessor :default_wallet
attr_accessor :wallet_dir
attr_accessor :gpg_exe
@ -45,29 +46,26 @@ class Config
@config_dir = "#{Dir.home}/.config/mpw"
end
if @config_file.nil? or @config_file.empty?
@config_file = "#{@config_dir}/mpw.cfg"
end
@config_file = "#{@config_dir}/mpw.cfg" if @config_file.nil? or @config_file.empty?
end
# Create a new config file
# @args: key -> the gpg key to encrypt
# lang -> the software language
# wallet_dir -> the directory where are the wallets password
# default_wallet -> the default wallet
# gpg_exe -> the path of gpg executable
# @rtrn: true if le config file is create
def setup(key, lang, wallet_dir, gpg_exe)
def setup(key, lang, wallet_dir, default_wallet, 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.to_s.empty?
wallet_dir = "#{@config_dir}/wallets"
end
wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty?
config = { 'key' => key,
'lang' => lang,
'wallet_dir' => wallet_dir,
'default_wallet' => default_wallet,
'gpg_exe' => gpg_exe,
}
@ -77,7 +75,6 @@ class Config
File.open(@config_file, 'w') do |file|
file << config.to_yaml
end
rescue Exception => e
raise "#{I18n.t('error.config.write')}\n#{e}"
end
@ -120,12 +117,12 @@ class Config
@key = config['key']
@lang = config['lang']
@wallet_dir = config['wallet_dir']
@default_wallet = config['default_wallet']
@gpg_exe = config['gpg_exe']
raise if @key.empty? or @wallet_dir.empty?
I18n.locale = @lang.to_sym
rescue Exception => e
raise "#{I18n.t('error.config.load')}\n#{e}"
end