lib/ssh/manager/cli.rb in ssh-manager-1.1.1 vs lib/ssh/manager/cli.rb in ssh-manager-1.1.2
- old
+ new
@@ -1,6 +1,8 @@
require_relative 'db'
+require 'colorize'
+require 'readline'
require 'yaml'
FileUtils.cp ("#{File.dirname(__FILE__)}/../../../config/settings.yml"), ("#{File.join(Dir.home)}" + '/.config/sshm/') unless File.exists?(("#{File.join(Dir.home)}" + '/.config/sshm/settings.yml'))
CONFIG = YAML.load_file("#{File.join(ENV['HOME'])}/.config/sshm/settings.yml")
require_relative 'version'
@@ -104,29 +106,82 @@
end
def transfer_key(id)
#TODO connect_via
#TODO options
- connection = DATABASE.get_connection_by_id(id)
- user = connection[:user]
- user = ENV['USER'] if user == ""
- %x(ssh-copy-id #{user}@#{connection[:ip]})
+ id.each do |con|
+ connection = DATABASE.get_connection_by_id(con)
+ user = connection[:user]
+ user = ENV['USER'] if user == ""
+ %x(ssh-copy-id #{user}@#{connection[:ip]})
+ end
end
def ping(id)
- connection = DATABASE.get_connection_by_id(id)
+ id.each do |con|
+ connection = DATABASE.get_connection_by_id(con)
if connection[:connect_via]
connect_via = DATABASE.get_connection_by_id(connection[:connect_via])
ssh = "ssh #{connect_via[:user]}@#{connect_via[:ip]}"
exec("#{ssh} ping #{connection[:ip]} -c 3")
else
exec("ping #{connection[:ip]} -c 3")
end
+ end
end
+
+ def execute_command(id)
+ # id.first should be the command
+ cmd = id.shift
+ id.each do |con|
+ connection = DATABASE.get_connection_by_id(con)
+ if connection[:connect_via]
+ connect_via = DATABASE.get_connection_by_id(connection[:connect_via])
+ ssh = "ssh #{connect_via[:user]}@#{connect_via[:ip]}"
+ system("#{ssh} #{cmd}")
+ else
+ ssh = "ssh #{connection[:user]}@#{connection[:ip]}"
+ system("#{ssh} #{cmd}")
+ end
+ end
+ end
+
def test(id)
+ require 'byebug'
+ byebug
end
+ def settings
+ CONFIG.keys.each do |key|
+ puts "#{key}=#{CONFIG[key]}"
+ end
+ possible_options ={terminal: %w(xfce4-terminal gnome-terminal urxvt),
+ deletion_interval: %w(never), tabbed: %w(true false),
+ self: %w(true false)}
+
+ # change tabbed to mode %w(tabbed whatelse)
+ print "Want to change? (y/n)"
+ answer = STDIN.gets.chomp
+ comp = proc { |s| possible_options.values.flatten.grep(/^#{Regexp.escape(s)}/) }
+ Readline.completion_append_character = " "
+ Readline.completion_proc = comp
+ if answer == "y"
+ CONFIG.keys.each do |key|
+ puts "#{key}:".colorize(:green)
+ puts "possible options are: #{possible_options[key.to_sym]}"
+ puts "leave blank to not make any changes (TAB-Completion avalaible)"
+ input = Readline.readline('> ', true)
+ CONFIG[key] = input if input != ""
+ end
+ File.open(("#{File.join(ENV['HOME'])}/.config/sshm/settings.yml"),'w') do |h|
+ h.write CONFIG.to_yaml
+ end
+ else
+ exit
+ end
+ end
+
def transfer_file(filename, id='', dest_path="/home/#{user}/")
#TODO connect_via
#TODO options
connection = DATABASE.get_connection_by_id(id)
user = connection[:user]
@@ -166,18 +221,20 @@
puts "\n"
end
end
def update(id)
- connection = DATABASE.get_connection_by_id(id)
- @input_fields.each do |key|
- #TODO make this a method
- connection[key] = %x{source #{File.dirname(__FILE__)}/ask.sh; ask '#{@pretty_names[key]}' '#{connection[key]}'}.chomp
+ id.each do |con|
+ connection = DATABASE.get_connection_by_id(id)
+ @input_fields.each do |key|
+ #TODO make this a method
+ connection[key] = %x{source #{File.dirname(__FILE__)}/ask.sh; ask '#{@pretty_names[key]}' '#{connection[key]}'}.chomp
+ end
+ connection[:connect_via] = nil if connection[:connect_via] == ""
+ DATABASE.update_connection(connection)
+ #TODO catch SQLite3::ConstraintException
end
- connection[:connect_via] = nil if connection[:connect_via] == ""
- DATABASE.update_connection(connection)
- #TODO catch SQLite3::ConstraintException
end
def search_for(term)
puts "All results for searchterm: #{term}"
@visible_fields.each { |f| printf "%-#{@column_width}s", @pretty_names[f] }
@@ -189,13 +246,45 @@
end
end
end
def multiple_connection(term)
+ @cons = []
DATABASE.search_for(term).each do |x|
x.all.each do |dataset|
- self.connect_to(dataset[:id])
+ @cons.push(dataset)
end
+ end
+ # FAIL, first push everything in cons, then do the rest !
+ puts "what yer gonna do?"
+ options = {execute_command: 'execute_command', connect_to: 'connect', ping: 'ping', info: 'info', delete: 'delete'}
+ comp = proc { |s| options.values.grep(/^#{Regexp.escape(s)}/) }
+ #Readline.completion_append_character = " "
+ Readline.completion_proc = comp
+ puts options.values.to_s
+ input= Readline.readline('> ', true)
+ ids = @cons.map {|x| x[:id].to_s}
+ input = input.chomp.gsub(/\s+\Z/, "")
+ case input
+ when 'execute_command'
+ # which commands
+ self.execute_command(ids)
+ exit
+ when 'connect'
+ self.connect_to(ids)
+ exit
+ when 'ping'
+ # exits after first ping
+ self.ping(ids)
+ exit
+ when 'info'
+ self.show_info(ids)
+ exit
+ when 'delete'
+ self.delete(ids)
+ exit
+ else
+ puts "That i dont know :/"
end
end
end
end
end