Class: Remote::Server

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/remote/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, data) ⇒ Server

Returns a new instance of Server.



6
7
8
9
10
# File 'lib/remote/server.rb', line 6

def initialize(name, data)
  super data
  self.host ||= name
  self.commands ||= Hash.new
end

Instance Method Details

#to_cmd(*what) ⇒ Object

Construct an SSH command line for the given command.



17
18
19
20
21
22
23
24
# File 'lib/remote/server.rb', line 17

def to_cmd(*what)
  hostname = self.user.nil? ? self.host.to_s : "#{self.user}@#{self.host}"

  cmd = [ "ssh", hostname ]
  cmd << "-i #{self.key}" unless self.key.nil?

  [ cmd.join(' '), translate(*what) ].compact.join(' -- ')
end

#to_sObject



12
13
14
# File 'lib/remote/server.rb', line 12

def to_s
  host.to_s
end

#translate(*cmds) ⇒ Object

Translates a given set of commands, taking into account aliases.

translate(‘git pull’, ‘thor app:restart’) #=> “cd ~/x; git pull; env RACK_ENV=production thor app:restart”



29
30
31
32
33
34
35
36
# File 'lib/remote/server.rb', line 29

def translate(*cmds)
  return nil  if cmds.empty?

  ret = []
  ret << "cd #{self.path}"  unless self.path.nil?
  cmds.each { |full_cmd| ret << translate_single(full_cmd) }
  ret.flatten.compact.join(';').shellescape
end

#translate_single(full_cmd) ⇒ Object

Translates a single command by resolving aliases.



39
40
41
42
43
44
45
46
47
# File 'lib/remote/server.rb', line 39

def translate_single(full_cmd)
  key, *args = full_cmd.split(' ')
  command_alias = self.commands[key]
  unless command_alias.nil?
    command_alias.gsub("\n",';').gsub('%s', args.join(' '))
  else
    full_cmd
  end
end