Sha256: c443342dc167e0c40e33aa1ac5689b4010df501613320cd8c8aef549bc2aa36a

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

# auto_register: false

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

class PgExport
  module Gateways
    class Ssh
      CHUNK_SIZE = (2**16).freeze

      def initialize(host:, user:, password:)
        @host, @user, @password, @logger = host, user, password
      end

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

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

      def close
        @ssh&.close
      end

      def list(regex_string)
        ssh
          .exec!("ls -l | grep #{regex_string.gsub('*', '')}")
          .split("\n")
          .map { |row| extract_meaningful_attributes(row) }
          .sort_by { |item| item[:name] }
          .reverse
      end

      def delete(name)
        # @TODO
      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

1 entries across 1 versions & 1 rubygems

Version Path
pg_export-1.0.0.rc1 lib/pg_export/lib/pg_export/gateways/ssh.rb