mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-23 13:57:52 +00:00
add export to yaml
This commit is contained in:
parent
76ee0fe2b4
commit
af04b8761d
5 changed files with 47 additions and 8 deletions
|
@ -13,6 +13,7 @@ en:
|
||||||
delete:
|
delete:
|
||||||
id_no_exist: "Can't delete the item %{id}, it doesn't exist!"
|
id_no_exist: "Can't delete the item %{id}, it doesn't exist!"
|
||||||
export:
|
export:
|
||||||
|
unknown_type: "The data type %{type} is unknown!"
|
||||||
write: "Can't export, unable to write in %{file}!"
|
write: "Can't export, unable to write in %{file}!"
|
||||||
gpg_file:
|
gpg_file:
|
||||||
decrypt: "Can't decrypt file!"
|
decrypt: "Can't decrypt file!"
|
||||||
|
@ -42,6 +43,7 @@ en:
|
||||||
setup: "Create a new configuration file"
|
setup: "Create a new configuration file"
|
||||||
protocol: "Select the items with the specified protocol"
|
protocol: "Select the items with the specified protocol"
|
||||||
export: "Export all items in a CSV file"
|
export: "Export all items in a CSV file"
|
||||||
|
type: "Data's type export file [csv|yaml]"
|
||||||
import: "Import item since a CSV file"
|
import: "Import item since a CSV file"
|
||||||
force: "Force an action"
|
force: "Force an action"
|
||||||
format: "Change the display items format by an alternative format"
|
format: "Change the display items format by an alternative format"
|
||||||
|
|
|
@ -13,6 +13,7 @@ fr:
|
||||||
delete:
|
delete:
|
||||||
id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!"
|
id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!"
|
||||||
export:
|
export:
|
||||||
|
unknown_type: "Le type de donnée %{type} est inconnu!"
|
||||||
write: "Impossible d'exporter les données dans le fichier %{file}!"
|
write: "Impossible d'exporter les données dans le fichier %{file}!"
|
||||||
gpg_file:
|
gpg_file:
|
||||||
decrypt: "Impossible de déchiffrer le fichier GPG!"
|
decrypt: "Impossible de déchiffrer le fichier GPG!"
|
||||||
|
@ -42,6 +43,7 @@ fr:
|
||||||
setup: "Création d'un nouveau fichier de configuration"
|
setup: "Création d'un nouveau fichier de configuration"
|
||||||
protocol: "Sélectionne les éléments ayant le protocole spécifié"
|
protocol: "Sélectionne les éléments ayant le protocole spécifié"
|
||||||
export: "Exporte tous les éléments dans un fichier au format CSV"
|
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"
|
import: "Importe des éléments depuis un fichier au format CSV"
|
||||||
force: "Force une action, l'action ne demandera pas de confirmation"
|
force: "Force une action, l'action ne demandera pas de confirmation"
|
||||||
format: "Change le format d'affichage des éléments par un alternatif"
|
format: "Change le format d'affichage des éléments par un alternatif"
|
||||||
|
|
39
lib/MPW.rb
39
lib/MPW.rb
|
@ -196,14 +196,43 @@ module MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
# Export to csv
|
# 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
|
# @rtrn: true if export work
|
||||||
def export(file)
|
def export(file, type=:csv)
|
||||||
CSV.open(file, 'w', write_headers: true,
|
case type
|
||||||
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv|
|
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|
|
@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
|
end
|
||||||
|
|
||||||
|
File.open(file, 'w') {|f| f << data.to_yaml}
|
||||||
|
else
|
||||||
|
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -307,8 +307,8 @@ class Cli
|
||||||
|
|
||||||
# Export the items in a CSV file
|
# Export the items in a CSV file
|
||||||
# @args: file -> the destination file
|
# @args: file -> the destination file
|
||||||
def export(file)
|
def export(file, type)
|
||||||
if @mpw.export(file)
|
if @mpw.export(file, type)
|
||||||
puts "The export in #{file} is succesfull!"
|
puts "The export in #{file} is succesfull!"
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}"
|
puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}"
|
||||||
|
|
8
mpw
8
mpw
|
@ -7,6 +7,7 @@ require 'rubygems'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
require 'locale'
|
require 'locale'
|
||||||
|
require 'set'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
|
|
||||||
APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
|
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|
|
opts.on('-e', '--export FILE', I18n.t('option.export')) do |file|
|
||||||
options[:export] = 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
|
end
|
||||||
|
|
||||||
opts.on('-i', '--import FILE', I18n.t('option.import')) do |file|
|
opts.on('-i', '--import FILE', I18n.t('option.import')) do |file|
|
||||||
|
@ -143,7 +149,7 @@ elsif not options[:add].nil?
|
||||||
|
|
||||||
# Export
|
# Export
|
||||||
elsif not options[:export].nil?
|
elsif not options[:export].nil?
|
||||||
cli.export(options[:export])
|
cli.export(options[:export], options[:type])
|
||||||
|
|
||||||
# Add a new item
|
# Add a new item
|
||||||
elsif not options[:import].nil?
|
elsif not options[:import].nil?
|
||||||
|
|
Loading…
Reference in a new issue