Sha256: cb4f9b8fd03edfeb59f5e38ec2287110429c524bd7def9e5fa53357f8d829a3f

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'ed25519'
require 'net/ssh'
require 'net/scp'

class PgExport
  module Gateways
    class Ssh
      def initialize(host:, user:, password:)
        @host, @user, @password, @logger = host, user, password
      end

      def open
        @ssh =
          if password.nil?
            Net::SSH.start(host, user)
          else
            Net::SSH.start(host, user, password)
          end
      end

      def welcome
        open.exec!('hostname; whoami')
      end

      def close
        @ssh&.close
      end

      def list(name)
        grep =
          if name.nil? || name.empty?
            ''
          else
            " | grep #{name}"
          end

        ssh
          .exec!("ls -l#{grep}")
          .split("\n").map { |row| extract_meaningful_attributes(row) }
          .reject { |item| item[:name].nil? }
          .sort_by { |item| item[:name] }
          .reverse
      end

      def delete(name)
        ssh.exec!("rm #{name}")
      end

      def persist(file, name)
        ssh.scp.upload(file.path, name).wait
      end

      def get(file, name)
        ssh.scp.download(name, file.path).wait
      end

      def to_s
        host
      end

      def ssh
        @ssh ||= open
      end

      private

      attr_reader :host, :user, :password

      def extract_meaningful_attributes(item)
        MEANINGFUL_KEYS.zip(item.split(' ').values_at(8, 4)).to_h
      end

      MEANINGFUL_KEYS = %i[name size].freeze
      private_constant :MEANINGFUL_KEYS
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pg_export-1.0.0 lib/pg_export/gateways/ssh.rb
pg_export-1.0.0.rc8 lib/pg_export/gateways/ssh.rb
pg_export-1.0.0.rc6 lib/pg_export/lib/pg_export/gateways/ssh.rb