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 options[:config] = config
end 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| opts.on('-g', '--gpg-exe PATH', I18n.t('option.gpg_exe')) do |gpg_exe|
values[:gpg_exe] = gpg_exe values[:gpg_exe] = gpg_exe
end end
@ -64,12 +68,13 @@ end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])
cli = MPW::Cli.new(config, nil) cli = MPW::Cli.new(config, nil)
if not options[:init].nil? if options.has_key?(:init)
cli.setup(values) cli.setup(values)
cli.load_config cli.load_config
cli.get_wallet cli.get_wallet
cli.setup_gpg_key(values[:gpg_key]) cli.setup_gpg_key(values[:gpg_key])
cli.setup_wallet_config cli.setup_wallet_config
else else
cli.load_config
cli.set_config(values) cli.set_config(values)
end end

View file

@ -54,6 +54,7 @@ en:
alpha: "Use letter to generate a password" alpha: "Use letter to generate a password"
config: "Specify the configuration file to use" config: "Specify the configuration file to use"
clipboard: "Disable the clipboard feature" 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" delete_gpg_key: "Delete the wallet's share with an other GPG key"
export: "Export a wallet in an yaml file" export: "Export a wallet in an yaml file"
file_export: "Specify the file where export data" file_export: "Specify the file where export data"
@ -128,6 +129,8 @@ en:
file_not_exist: "The import file doesn't exist!" file_not_exist: "The import file doesn't exist!"
valid: "The import is succesfull!" valid: "The import is succesfull!"
not_valid: "No data to import!" not_valid: "No data to import!"
set_config:
valid: "The config file has been edited!"
setup_config: setup_config:
title: "Setup a new config file" title: "Setup a new config file"
lang: "Choose your language (en, fr, ...) [default=%{lang}]: " 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" alpha: "Utilise des lettres dans la génération d'un mot de passe"
config: "Spécifie le fichier de configuration à utiliser" config: "Spécifie le fichier de configuration à utiliser"
clipboard: "Désactive la fonction presse papier" 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" delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG"
export: "Exporte un portefeuille dans un fichier yaml" export: "Exporte un portefeuille dans un fichier yaml"
file_export: "Spécifie le fichier où exporter les données" 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" file_not_exist: "Le fichier d'import n'existe pas"
valid: "L'import est un succès!" valid: "L'import est un succès!"
not_valid: "Aucune donnée à importer!" not_valid: "Aucune donnée à importer!"
set_config:
valid: "Le fichier de configuration a bien été modifié!"
setup_config: setup_config:
title: "Création d'un nouveau fichier de configuration" title: "Création d'un nouveau fichier de configuration"
lang: "Choisissez votre langue (en, fr, ...) [défaut=%{lang}]: " lang: "Choisissez votre langue (en, fr, ...) [défaut=%{lang}]: "

View file

@ -40,12 +40,15 @@ class Cli
# Change a parameter int the config after init # Change a parameter int the config after init
# @args: options -> param to change # @args: options -> param to change
def set_config(options) def set_config(options)
gpg_key = options[:gpg_key] || @config.key gpg_key = options[:gpg_key] || @config.key
lang = options[:lang] || @config.lang lang = options[:lang] || @config.lang
wallet_dir = options[:wallet_dir] || @config.wallet_dir wallet_dir = options[:wallet_dir] || @config.wallet_dir
gpg_exe = options[:gpg_exe] || @config.gpg_exe 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 rescue Exception => e
puts "#{I18n.t('display.error')} #15: #{e}".red puts "#{I18n.t('display.error')} #15: #{e}".red
exit 2 exit 2
@ -341,7 +344,9 @@ class Cli
def list_wallet def list_wallet
wallets = [] wallets = []
Dir.glob("#{@config.wallet_dir}/*.mpw").each do |f| 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 end
table_list('wallets', wallets) table_list('wallets', wallets)
@ -355,6 +360,8 @@ class Cli
if wallets.length == 1 if wallets.length == 1
@wallet_file = wallets[0] @wallet_file = wallets[0]
elsif not @config.default_wallet.to_s.empty?
@wallet_file = "#{@config.wallet_dir}/#{@config.default_wallet}.mpw"
else else
@wallet_file = "#{@config.wallet_dir}/default.mpw" @wallet_file = "#{@config.wallet_dir}/default.mpw"
end end

View file

@ -29,6 +29,7 @@ class Config
attr_accessor :key attr_accessor :key
attr_accessor :lang attr_accessor :lang
attr_accessor :config_dir attr_accessor :config_dir
attr_accessor :default_wallet
attr_accessor :wallet_dir attr_accessor :wallet_dir
attr_accessor :gpg_exe attr_accessor :gpg_exe
@ -45,31 +46,28 @@ class Config
@config_dir = "#{Dir.home}/.config/mpw" @config_dir = "#{Dir.home}/.config/mpw"
end end
if @config_file.nil? or @config_file.empty? @config_file = "#{@config_dir}/mpw.cfg" if @config_file.nil? or @config_file.empty?
@config_file = "#{@config_dir}/mpw.cfg"
end
end end
# Create a new config file # Create a new config file
# @args: key -> the gpg key to encrypt # @args: key -> the gpg key to encrypt
# lang -> the software language # lang -> the software language
# wallet_dir -> the directory where are the wallets password # wallet_dir -> the directory where are the wallets password
# gpg_exe -> the path of gpg executable # default_wallet -> the default wallet
# gpg_exe -> the path of gpg executable
# @rtrn: true if le config file is create # @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]+/ if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
raise I18n.t('error.config.key_bad_format') raise I18n.t('error.config.key_bad_format')
end end
if wallet_dir.to_s.empty? wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty?
wallet_dir = "#{@config_dir}/wallets" config = { 'key' => key,
end 'lang' => lang,
'wallet_dir' => wallet_dir,
config = { 'key' => key, 'default_wallet' => default_wallet,
'lang' => lang, 'gpg_exe' => gpg_exe,
'wallet_dir' => wallet_dir, }
'gpg_exe' => gpg_exe,
}
FileUtils.mkdir_p(@config_dir, mode: 0700) FileUtils.mkdir_p(@config_dir, mode: 0700)
FileUtils.mkdir_p(wallet_dir, mode: 0700) FileUtils.mkdir_p(wallet_dir, mode: 0700)
@ -77,7 +75,6 @@ class Config
File.open(@config_file, 'w') do |file| File.open(@config_file, 'w') do |file|
file << config.to_yaml file << config.to_yaml
end end
rescue Exception => e rescue Exception => e
raise "#{I18n.t('error.config.write')}\n#{e}" raise "#{I18n.t('error.config.write')}\n#{e}"
end end
@ -116,16 +113,16 @@ class Config
# Load the config file # Load the config file
def load_config def load_config
config = YAML::load_file(@config_file) config = YAML::load_file(@config_file)
@key = config['key'] @key = config['key']
@lang = config['lang'] @lang = config['lang']
@wallet_dir = config['wallet_dir'] @wallet_dir = config['wallet_dir']
@gpg_exe = config['gpg_exe'] @default_wallet = config['default_wallet']
@gpg_exe = config['gpg_exe']
raise if @key.empty? or @wallet_dir.empty? raise if @key.empty? or @wallet_dir.empty?
I18n.locale = @lang.to_sym I18n.locale = @lang.to_sym
rescue Exception => e rescue Exception => e
raise "#{I18n.t('error.config.load')}\n#{e}" raise "#{I18n.t('error.config.load')}\n#{e}"
end end