mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-26 23:33:03 +00:00
feat: add option to set default wallet
This commit is contained in:
parent
50d88fc970
commit
c841123ac0
5 changed files with 44 additions and 29 deletions
|
@ -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
|
||||||
|
|
|
@ -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}]: "
|
||||||
|
|
|
@ -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}]: "
|
||||||
|
|
|
@ -43,9 +43,12 @@ class Cli
|
||||||
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
|
||||||
|
default_wallet = options[:default_wallet] || @config.default_wallet
|
||||||
gpg_exe = options[:gpg_exe] || @config.gpg_exe
|
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
|
||||||
|
|
|
@ -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,29 +46,26 @@ 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
|
||||||
|
# default_wallet -> the default wallet
|
||||||
# gpg_exe -> the path of gpg executable
|
# 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"
|
|
||||||
end
|
|
||||||
|
|
||||||
config = { 'key' => key,
|
config = { 'key' => key,
|
||||||
'lang' => lang,
|
'lang' => lang,
|
||||||
'wallet_dir' => wallet_dir,
|
'wallet_dir' => wallet_dir,
|
||||||
|
'default_wallet' => default_wallet,
|
||||||
'gpg_exe' => gpg_exe,
|
'gpg_exe' => gpg_exe,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -120,12 +117,12 @@ class Config
|
||||||
@key = config['key']
|
@key = config['key']
|
||||||
@lang = config['lang']
|
@lang = config['lang']
|
||||||
@wallet_dir = config['wallet_dir']
|
@wallet_dir = config['wallet_dir']
|
||||||
|
@default_wallet = config['default_wallet']
|
||||||
@gpg_exe = config['gpg_exe']
|
@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
|
||||||
|
|
Loading…
Reference in a new issue