1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-10-27 02:33:19 +00:00

add export to yaml

This commit is contained in:
nishiki 2014-11-15 19:14:42 +01:00
parent 76ee0fe2b4
commit af04b8761d
5 changed files with 47 additions and 8 deletions

View file

@ -13,6 +13,7 @@ en:
delete:
id_no_exist: "Can't delete the item %{id}, it doesn't exist!"
export:
unknown_type: "The data type %{type} is unknown!"
write: "Can't export, unable to write in %{file}!"
gpg_file:
decrypt: "Can't decrypt file!"
@ -42,6 +43,7 @@ en:
setup: "Create a new configuration file"
protocol: "Select the items with the specified protocol"
export: "Export all items in a CSV file"
type: "Data's type export file [csv|yaml]"
import: "Import item since a CSV file"
force: "Force an action"
format: "Change the display items format by an alternative format"

View file

@ -13,6 +13,7 @@ fr:
delete:
id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!"
export:
unknown_type: "Le type de donnée %{type} est inconnu!"
write: "Impossible d'exporter les données dans le fichier %{file}!"
gpg_file:
decrypt: "Impossible de déchiffrer le fichier GPG!"
@ -42,6 +43,7 @@ fr:
setup: "Création d'un nouveau fichier de configuration"
protocol: "Sélectionne les éléments ayant le protocole spécifié"
export: "Exporte tous les éléments dans un fichier au format CSV"
type: "Format des données du fichier d'export [csv|yaml]"
import: "Importe des éléments depuis un fichier au format CSV"
force: "Force une action, l'action ne demandera pas de confirmation"
format: "Change le format d'affichage des éléments par un alternatif"

View file

@ -196,14 +196,43 @@ module MPW
end
# Export to csv
# @args: file -> a string to match
# @args: file -> file where you export the data
# type -> data type
# @rtrn: true if export work
def export(file)
CSV.open(file, 'w', write_headers: true,
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv|
def export(file, type=:csv)
case type
when :csv
CSV.open(file, 'w', write_headers: true,
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv|
@data.each do |r|
csv << [r[:name], r[:group], r[:protocol], r[:host], r[:login], r[:password], r[:port], r[:comment]]
end
end
when :yaml
data = {}
i = 0
@data.each do |r|
csv << [r[:name], r[:group], r[:protocol], r[:host], r[:login], r[:password], r[:port], r[:comment]]
data.merge!({i => {'id' => r[:id],
'name' => r[:name],
'group' => r[:group],
'protocol' => r[:protocol],
'host' => r[:host],
'login' => r[:login],
'password' => r[:password],
'port' => r[:port],
'comment' => r[:comment]
}
}
)
i += 1
end
File.open(file, 'w') {|f| f << data.to_yaml}
else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
return false
end
return true

View file

@ -307,8 +307,8 @@ class Cli
# Export the items in a CSV file
# @args: file -> the destination file
def export(file)
if @mpw.export(file)
def export(file, type)
if @mpw.export(file, type)
puts "The export in #{file} is succesfull!"
else
puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}"

8
mpw
View file

@ -7,6 +7,7 @@ require 'rubygems'
require 'optparse'
require 'pathname'
require 'locale'
require 'set'
require 'i18n'
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
@ -81,6 +82,11 @@ OptionParser.new do |opts|
opts.on('-e', '--export FILE', I18n.t('option.export')) do |file|
options[:export] = file
options[:type] = :csv
end
opts.on('-t', '--type TYPE', I18n.t('option.type')) do |type|
options[:type] = type.to_sym
end
opts.on('-i', '--import FILE', I18n.t('option.import')) do |file|
@ -143,7 +149,7 @@ elsif not options[:add].nil?
# Export
elsif not options[:export].nil?
cli.export(options[:export])
cli.export(options[:export], options[:type])
# Add a new item
elsif not options[:import].nil?