diff --git a/lib/MPW.rb b/lib/MPW.rb index c90bcd3..9401382 100644 --- a/lib/MPW.rb +++ b/lib/MPW.rb @@ -63,8 +63,8 @@ module MPW def encrypt FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg) - data_to_encrypt = {} @data.each do |item| + puts item.class next if item.empty? data_to_encrypt.merge!(item.id => {'id' => item.id, @@ -101,6 +101,22 @@ module MPW return false end + # Add a new item + # @args: item -> Object MPW::Item + # @rtrn: true if add item + def add(item) + if not item.instance_of?(Item) + @error_msg = I18n.t('error.bad_class') + return false + elsif item.empty? + @error_msg = I18n.t('error.add.empty') + return false + else + @data.push(item) + return true + end + end + # Search in some csv data # @args: search -> the string to search # protocol -> the connection protocol (ssh, web, other) diff --git a/lib/UI/Cli.rb b/lib/UI/Cli.rb index 6a3e8ff..4b468b0 100644 --- a/lib/UI/Cli.rb +++ b/lib/UI/Cli.rb @@ -224,19 +224,21 @@ class Cli # Form to add a new item def add - row = [] + options = {} + puts I18n.t('form.add.title') puts '--------------------' - name = ask(I18n.t('form.add.name')).to_s - group = ask(I18n.t('form.add.group')).to_s - server = ask(I18n.t('form.add.server')).to_s - protocol = ask(I18n.t('form.add.protocol')).to_s - login = ask(I18n.t('form.add.login')).to_s - passwd = ask(I18n.t('form.add.password')).to_s - port = ask(I18n.t('form.add.port')).to_s - comment = ask(I18n.t('form.add.comment')).to_s + options[:name] = ask(I18n.t('form.add.name')).to_s + options[:group] = ask(I18n.t('form.add.group')).to_s + options[:host] = ask(I18n.t('form.add.server')).to_s + options[:protocol] = ask(I18n.t('form.add.protocol')).to_s + options[:user] = ask(I18n.t('form.add.login')).to_s + options[:passwd] = ask(I18n.t('form.add.password')).to_s + options[:port] = ask(I18n.t('form.add.port')).to_s + options[:comment] = ask(I18n.t('form.add.comment')).to_s - if @mpw.update(name, group, server, protocol, login, passwd, port, comment) + item = MPW::Item.new(options) + if @mpw.add(item) if @mpw.encrypt sync puts "#{I18n.t('form.add.valid')}".green