1
0
Fork 0
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:
Adrien Waksberg 2017-09-03 02:37:17 +02:00
parent b2a38ccd4e
commit e2b4557981
7 changed files with 126 additions and 19 deletions

View file

@ -24,7 +24,13 @@ require 'mpw/cli'
# Options # 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| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw import [options]" opts.banner = "#{I18n.t('option.usage')}: mpw import [options]"
@ -37,6 +43,10 @@ OptionParser.new do |opts|
options[:file] = file options[:file] = file
end 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 opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
@ -53,4 +63,4 @@ cli = MPW::Cli.new(config)
cli.load_config cli.load_config
cli.get_wallet(options[:wallet]) cli.get_wallet(options[:wallet])
cli.decrypt cli.decrypt
cli.import(options[:file]) cli.import(options[:file], options[:format])

View file

@ -22,7 +22,7 @@ en:
write_data: "Can't write the MPW file!" write_data: "Can't write the MPW file!"
import: "Can't import, unable to read %{file}!" import: "Can't import, unable to read %{file}!"
update: update:
host_empty: "You must define a host!" host_and_comment_empty: "You must define a host or a comment!"
warning: warning:
select: 'Your choice is not a valid item!' 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" disable_special_chars: "Don't use special char to create a password"
export: "Export a wallet in an yaml file" export: "Export a wallet in an yaml file"
file_export: "Specify the file to export data" 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" file_import: "Specify the file to import"
force: "Do not ask confirmation when deleting an item" force: "Do not ask confirmation when deleting an item"
generate_password: "Create a random password (default 8 characters)" 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} ?" ask: "Are you sure you want to import this file %{file} ?"
file_empty: "The import file is empty!" file_empty: "The import file is empty!"
file_not_exist: "The import file doesn't exist!" file_not_exist: "The import file doesn't exist!"
format_unknown: "The import format '%{file_format} is unknown!"
valid: "The import is successful!" valid: "The import is successful!"
not_valid: "No data to import!" not_valid: "No data to import!"
set_config: set_config:

View file

@ -22,7 +22,7 @@ fr:
write_data: "Impossible d'écrire le fichier MPW!" write_data: "Impossible d'écrire le fichier MPW!"
import: "Impossible d'importer le fichier %{file}, car il n'est pas lisible!" import: "Impossible d'importer le fichier %{file}, car il n'est pas lisible!"
update: update:
host_empty: "Vous devez définir un host!" host_and_comment_empty: "Vous devez définir un host ou un commentaire!"
warning: warning:
select: "Votre choix n'est pas un élément valide!" 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" 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" 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"
file_format: "Format du fichier d'import (défault: mpw; disponible: %{formats})"
file_import: "Spécifie le fichier à importer" file_import: "Spécifie le fichier à importer"
force: "Ne demande pas de confirmation pour la suppression d'un élément" 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)" 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} ?" ask: "Êtes vous sûre de vouloir importer le fichier %{file} ?"
file_empty: "Le fichier d'import est vide!" file_empty: "Le fichier d'import est vide!"
file_not_exist: "Le fichier d'import n'existe pas" 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!" valid: "L'import est un succès!"
not_valid: "Aucune donnée à importer!" not_valid: "Aucune donnée à importer!"
set_config: set_config:

View file

@ -586,17 +586,26 @@ module MPW
# Import items from an yaml file # Import items from an yaml file
# @param file [String] path of import 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_empty') if file.to_s.empty?
raise I18n.t('form.import.file_not_exist') unless File.exist?(file) raise I18n.t('form.import.file_not_exist') unless File.exist?(file)
YAML.load_file(file).each_value do |row| begin
item = Item.new(group: row['group'], require "mpw/import/#{format}"
host: row['host'], rescue LoadError
protocol: row['protocol'], raise I18n.t('form.import.format_unknown', file_format: format)
user: row['user'], end
port: row['port'],
comment: row['comment']) 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? next if item.empty?

57
lib/mpw/import/gorilla.rb Normal file
View 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
View 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

View file

@ -33,9 +33,7 @@ module MPW
# @param options [Hash] the option :host is required # @param options [Hash] the option :host is required
def initialize(**options) def initialize(**options)
if !options.key?(:host) || options[:host].to_s.empty? @host = ''
raise I18n.t('error.update.host_empty')
end
if !options.key?(:id) || options[:id].to_s.empty? || !options.key?(:created) || options[:created].to_s.empty? if !options.key?(:id) || options[:id].to_s.empty? || !options.key?(:created) || options[:created].to_s.empty?
@id = generate_id @id = generate_id
@ -53,12 +51,12 @@ module MPW
# Update the item # Update the item
# @param options [Hash] # @param options [Hash]
def update(**options) def update(**options)
if options.key?(:host) && options[:host].to_s.empty? unless options[:host] || options[:comment]
raise I18n.t('error.update.host_empty') raise I18n.t('error.update.host_and_comment_empty')
end end
@group = options[:group] if options.key?(:group) @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) @protocol = options[:protocol] if options.key?(:protocol)
@user = options[:user] if options.key?(:user) @user = options[:user] if options.key?(:user)
@port = options[:port].to_i if options.key?(:port) && !options[:port].to_s.empty? @port = options[:port].to_i if options.key?(:port) && !options[:port].to_s.empty?
@ -86,7 +84,7 @@ module MPW
def url def url
url = '' url = ''
url += "#{@protocol}://" if @protocol url += "#{@protocol}://" if @protocol
url += @host url += @host if @host
url += ":#{@port}" if @port url += ":#{@port}" if @port
url url