lib/mpw/sync/ssh.rb in mpw-2.0.3 vs lib/mpw/sync/ssh.rb in mpw-3.0.0

- old
+ new

@@ -1,80 +1,67 @@ #!/usr/bin/ruby -# author: nishiki -# mail: nishiki@yaegashi.fr +# MPW is a software to crypt and manage your passwords +# Copyright (C) 2016 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 'rubygems' require 'i18n' require 'net/ssh' require 'net/sftp' module MPW - class SyncSSH - - attr_accessor :error_msg - attr_accessor :enable - - # Constructor - # @args: host -> the server host - # port -> ther connection port - # gpg_key -> the gpg key - # password -> the remote password - def initialize(host, user, password, path, port=nil) - @error_msg = nil - @enable = false +class SyncSSH - @host = host - @user = user - @password = password - @path = path - @port = port.instance_of?(Integer) ? 22 : port - end - - # Connect to server - # @rtrn: false if the connection fail - def connect - Net::SSH.start(@host, @user, password: @password, port: @port) do |ssh| - @enable = true - end - rescue Exception => e - @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" - @enable = false - else - return @enable - end - - # Get data on server - # @args: gpg_password -> the gpg password - # @rtrn: nil if nothing data or error - def get(file_tmp) - return false if not @enable - - Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| - sftp.lstat(@path) do |response| - sftp.download!(@path, file_tmp) if response.ok? - end - end + # Constructor + # @args: config -> the config + def initialize(config) + @host = config['host'] + @user = config['user'] + @password = config['password'] + @path = config['path'] + @port = config['port'].instance_of?(Integer) ? 22 : config['port'] + end - return true - rescue Exception => e - @error_msg = "#{I18n.t('error.sync.download')}\n#{e}" - return false + # Connect to server + def connect + Net::SSH.start(@host, @user, password: @password, port: @port) do + break end - - # Update the remote data - # @args: file_gpg -> the data to send on server - # @rtrn: false if there is a problem - def update(file_gpg) - return true if not @enable - - Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| - sftp.upload!(file_gpg, @path) - end + rescue Exception => e + raise "#{I18n.t('error.sync.connection')}\n#{e}" + end - return true - rescue Exception => e - @error_msg = "#{I18n.t('error.sync.upload')}\n#{e}" - return false + # Get data on server + # @args: file_tmp -> the path where download the file + def get(file_tmp) + Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| + sftp.lstat(@path) do |response| + sftp.download!(@path, file_tmp) if response.ok? + end end + rescue Exception => e + raise "#{I18n.t('error.sync.download')}\n#{e}" + end + # Update the remote data + # @args: file_gpg -> the data to send on server + def update(file_gpg) + Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| + sftp.upload!(file_gpg, @path) + end + rescue Exception => e + raise "#{I18n.t('error.sync.upload')}\n#{e}" end +end end