mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-23 05:47:53 +00:00
feat: add import gorilla file
This commit is contained in:
parent
b2a38ccd4e
commit
e2b4557981
7 changed files with 126 additions and 19 deletions
|
@ -24,7 +24,13 @@ require 'mpw/cli'
|
|||
# Options
|
||||
# --------------------------------------------------------- #
|
||||
|
||||
options = {}
|
||||
formats =
|
||||
Dir["#{File.expand_path('../../lib/mpw/import', __FILE__)}/*.rb"]
|
||||
.map { |v| File.basename(v, '.rb') }
|
||||
.join(', ')
|
||||
options = {
|
||||
format: 'mpw'
|
||||
}
|
||||
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = "#{I18n.t('option.usage')}: mpw import [options]"
|
||||
|
@ -37,6 +43,10 @@ OptionParser.new do |opts|
|
|||
options[:file] = file
|
||||
end
|
||||
|
||||
opts.on('-F', '--format STRING', I18n.t('option.file_format', formats: formats)) do |format|
|
||||
options[:format] = format
|
||||
end
|
||||
|
||||
opts.on('-h', '--help', I18n.t('option.help')) do
|
||||
puts opts
|
||||
exit 0
|
||||
|
@ -53,4 +63,4 @@ cli = MPW::Cli.new(config)
|
|||
cli.load_config
|
||||
cli.get_wallet(options[:wallet])
|
||||
cli.decrypt
|
||||
cli.import(options[:file])
|
||||
cli.import(options[:file], options[:format])
|
||||
|
|
|
@ -22,7 +22,7 @@ en:
|
|||
write_data: "Can't write the MPW file!"
|
||||
import: "Can't import, unable to read %{file}!"
|
||||
update:
|
||||
host_empty: "You must define a host!"
|
||||
host_and_comment_empty: "You must define a host or a comment!"
|
||||
|
||||
warning:
|
||||
select: 'Your choice is not a valid item!'
|
||||
|
@ -55,6 +55,7 @@ en:
|
|||
disable_special_chars: "Don't use special char to create a password"
|
||||
export: "Export a wallet in an yaml file"
|
||||
file_export: "Specify the file to export data"
|
||||
file_format: "Format of import file (default: mpw; available: %{formats})"
|
||||
file_import: "Specify the file to import"
|
||||
force: "Do not ask confirmation when deleting an item"
|
||||
generate_password: "Create a random password (default 8 characters)"
|
||||
|
@ -130,6 +131,7 @@ en:
|
|||
ask: "Are you sure you want to import this file %{file} ?"
|
||||
file_empty: "The import file is empty!"
|
||||
file_not_exist: "The import file doesn't exist!"
|
||||
format_unknown: "The import format '%{file_format} is unknown!"
|
||||
valid: "The import is successful!"
|
||||
not_valid: "No data to import!"
|
||||
set_config:
|
||||
|
|
|
@ -22,7 +22,7 @@ fr:
|
|||
write_data: "Impossible d'écrire le fichier MPW!"
|
||||
import: "Impossible d'importer le fichier %{file}, car il n'est pas lisible!"
|
||||
update:
|
||||
host_empty: "Vous devez définir un host!"
|
||||
host_and_comment_empty: "Vous devez définir un host ou un commentaire!"
|
||||
|
||||
warning:
|
||||
select: "Votre choix n'est pas un élément valide!"
|
||||
|
@ -55,6 +55,7 @@ fr:
|
|||
disable_special_chars: "Désactive l'utilisation des charactères speciaux dans la génération d'un mot de passe"
|
||||
export: "Exporte un portefeuille dans un fichier yaml"
|
||||
file_export: "Spécifie le fichier où exporter les données"
|
||||
file_format: "Format du fichier d'import (défault: mpw; disponible: %{formats})"
|
||||
file_import: "Spécifie le fichier à importer"
|
||||
force: "Ne demande pas de confirmation pour la suppression d'un élément"
|
||||
generate_password: "Génére un mot de passe aléatoire (défaut 8 caractères)"
|
||||
|
@ -130,6 +131,7 @@ fr:
|
|||
ask: "Êtes vous sûre de vouloir importer le fichier %{file} ?"
|
||||
file_empty: "Le fichier d'import est vide!"
|
||||
file_not_exist: "Le fichier d'import n'existe pas"
|
||||
format_unknown: "Le format d'import '%{file_format}' est inconnu!"
|
||||
valid: "L'import est un succès!"
|
||||
not_valid: "Aucune donnée à importer!"
|
||||
set_config:
|
||||
|
|
|
@ -586,17 +586,26 @@ module MPW
|
|||
|
||||
# Import items from an yaml file
|
||||
# @param file [String] path of import file
|
||||
def import(file)
|
||||
# @param format [String] the software import file format
|
||||
def import(file, format = 'mpw')
|
||||
raise I18n.t('form.import.file_empty') if file.to_s.empty?
|
||||
raise I18n.t('form.import.file_not_exist') unless File.exist?(file)
|
||||
|
||||
YAML.load_file(file).each_value do |row|
|
||||
item = Item.new(group: row['group'],
|
||||
host: row['host'],
|
||||
protocol: row['protocol'],
|
||||
user: row['user'],
|
||||
port: row['port'],
|
||||
comment: row['comment'])
|
||||
begin
|
||||
require "mpw/import/#{format}"
|
||||
rescue LoadError
|
||||
raise I18n.t('form.import.format_unknown', file_format: format)
|
||||
end
|
||||
|
||||
Import.send(format, file).each_value do |row|
|
||||
item = Item.new(
|
||||
group: row['group'],
|
||||
host: row['host'],
|
||||
protocol: row['protocol'],
|
||||
user: row['user'],
|
||||
port: row['port'],
|
||||
comment: row['comment']
|
||||
)
|
||||
|
||||
next if item.empty?
|
||||
|
||||
|
|
57
lib/mpw/import/gorilla.rb
Normal file
57
lib/mpw/import/gorilla.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/ruby
|
||||
# MPW is a software to crypt and manage your passwords
|
||||
# Copyright (C) 2017 Adrien Waksberg <mpw@yae.im>
|
||||
#
|
||||
# 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.
|
||||
|
||||
require 'csv'
|
||||
|
||||
module MPW
|
||||
module Import
|
||||
# Import an export mpw file
|
||||
# @param file [String] the file path to import
|
||||
def self.gorilla(file)
|
||||
data = {}
|
||||
|
||||
CSV.foreach(file, headers: true) do |row|
|
||||
id = row['uuid']
|
||||
comment =
|
||||
if row['title'] && row['notes']
|
||||
"#{row['title']} #{row['notes']}"
|
||||
elsif row['title']
|
||||
row['title']
|
||||
elsif row['notes']
|
||||
row['notes']
|
||||
end
|
||||
|
||||
data[id] = {
|
||||
'group' => row['group'],
|
||||
'host' => row['url'],
|
||||
'user' => row['user'],
|
||||
'password' => row['password'],
|
||||
'comment' => comment
|
||||
}
|
||||
|
||||
if row['url'] =~ %r{^((?<protocol>[a-z]+)://)?(?<host>[a-zA-Z0-9_.-]+)(:(?<port>[0-9]{1,5}))?$}
|
||||
data[id]['protocol'] = Regexp.last_match(:protocol)
|
||||
data[id]['port'] = Regexp.last_match(:port)
|
||||
data[id]['host'] = Regexp.last_match(:host)
|
||||
end
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
end
|
||||
end
|
29
lib/mpw/import/mpw.rb
Normal file
29
lib/mpw/import/mpw.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/ruby
|
||||
# MPW is a software to crypt and manage your passwords
|
||||
# Copyright (C) 2017 Adrien Waksberg <mpw@yae.im>
|
||||
#
|
||||
# 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.
|
||||
|
||||
require 'yaml'
|
||||
|
||||
module MPW
|
||||
module Import
|
||||
# Import an export mpw file
|
||||
# @param file [String] the file path to import
|
||||
def self.mpw(file)
|
||||
YAML.load_file(file)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -33,9 +33,7 @@ module MPW
|
|||
|
||||
# @param options [Hash] the option :host is required
|
||||
def initialize(**options)
|
||||
if !options.key?(:host) || options[:host].to_s.empty?
|
||||
raise I18n.t('error.update.host_empty')
|
||||
end
|
||||
@host = ''
|
||||
|
||||
if !options.key?(:id) || options[:id].to_s.empty? || !options.key?(:created) || options[:created].to_s.empty?
|
||||
@id = generate_id
|
||||
|
@ -53,12 +51,12 @@ module MPW
|
|||
# Update the item
|
||||
# @param options [Hash]
|
||||
def update(**options)
|
||||
if options.key?(:host) && options[:host].to_s.empty?
|
||||
raise I18n.t('error.update.host_empty')
|
||||
unless options[:host] || options[:comment]
|
||||
raise I18n.t('error.update.host_and_comment_empty')
|
||||
end
|
||||
|
||||
@group = options[:group] if options.key?(:group)
|
||||
@host = options[:host] if options.key?(:host)
|
||||
@host = options[:host] if options.key?(:host) && !options[:host].nil?
|
||||
@protocol = options[:protocol] if options.key?(:protocol)
|
||||
@user = options[:user] if options.key?(:user)
|
||||
@port = options[:port].to_i if options.key?(:port) && !options[:port].to_s.empty?
|
||||
|
@ -86,7 +84,7 @@ module MPW
|
|||
def url
|
||||
url = ''
|
||||
url += "#{@protocol}://" if @protocol
|
||||
url += @host
|
||||
url += @host if @host
|
||||
url += ":#{@port}" if @port
|
||||
|
||||
url
|
||||
|
|
Loading…
Reference in a new issue