Sha256: 39334525520bdbca99ad95950c943bf08bc925ea2eb44010b87afb62cc1ebd96

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'sequel'
require 'fileutils'

module SSH
  module Manager
    class Database

      FileUtils.mkdir_p("#{File.join(ENV['HOME'])}/.config/sshm/") unless Dir.exists?("#{ENV['HOME']}/.config/sshm")
      FileUtils.cp ("#{File.dirname(__FILE__)}/../../../sshm.db"), ("#{File.join(Dir.home)}" + '/.config/sshm/') unless File.exists?(("#{File.join(Dir.home)}" + '/.config/sshm/sshm.db'))

      @path = "#{File.join(ENV['HOME'])}/.config/sshm"
      DATABASE = Sequel.connect("sqlite://#{@path}/sshm.db")

      attr_accessor :connections

      def initialize
        @connections= DATABASE.from(:connection)
      end

      def get_connection_data
        @connections.map([:ip, :user, :hostname, :port, :note])
      end

      def add_new_connection(ip, user='root', hostname='', port=22, note='')
        # default params are currently useless FIXME
        @connections.insert(:ip => ip, :user => user, :hostname => hostname, :port => port, :note => note)
      end

      def delete_connection(ip)
        # add && :user => user to ensure deletion
        @connections.where(:ip => ip).delete
      end

      def update_connection(ip, user, hostname, port, note)
        @connections.where(:ip => ip).update(:user => user, :hostname => hostname, :port => port, :note => note)
      end

      def search_for(term)
        # check online: search for 'contains' not for complete matching
        return  @connections.where(:ip => term),  @connections.where(:user => term), @connections.where(:hostname => term), @connections.where(:port => term), @connections.where(:note => term)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ssh-manager-0.0.2 lib/ssh/manager/db.rb