Class: SSHClient::ConfigItem

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_client/config_item.rb

Constant Summary collapse

DEFAULT_NAME =
:default
READ_TIMEOUT =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ ConfigItem

Returns a new instance of ConfigItem.



12
13
14
15
16
17
# File 'lib/ssh_client/config_item.rb', line 12

def initialize(name = nil)
  @name = name || DEFAULT_NAME
  @listeners = { stdout: Set.new, stderr: Set.new }

  add_listener { |data| logger.info "<< #{data}" } if default?
end

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



10
11
12
# File 'lib/ssh_client/config_item.rb', line 10

def debug=(value)
  @debug = value
end

#hostnameObject

Returns the value of attribute hostname.



8
9
10
# File 'lib/ssh_client/config_item.rb', line 8

def hostname
  @hostname
end

#loggerObject



64
65
66
# File 'lib/ssh_client/config_item.rb', line 64

def logger
  @logger || (default? ? Logger.new(STDOUT) : SSHClient.config.logger)
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ssh_client/config_item.rb', line 9

def name
  @name
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/ssh_client/config_item.rb', line 8

def password
  @password
end

#read_timeoutObject



42
43
44
# File 'lib/ssh_client/config_item.rb', line 42

def read_timeout
  @read_timeout || (default? ? READ_TIMEOUT : SSHClient.config.read_timeout)
end

#transportObject



46
47
48
# File 'lib/ssh_client/config_item.rb', line 46

def transport
  transport_klass.new self
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/ssh_client/config_item.rb', line 8

def username
  @username
end

Instance Method Details

#add_listener(*args, &blk) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ssh_client/config_item.rb', line 27

def add_listener(*args, &blk)
  Array(args || @listeners.keys).each do |k|
    @listeners[k] << blk
  end
  @cached_listeners = nil
  blk
end

#debug?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ssh_client/config_item.rb', line 68

def debug?
  @debug || (default? ? false : SSHClient.config.debug?)
end

#default?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/ssh_client/config_item.rb', line 72

def default?
  name == DEFAULT_NAME
end

#listenersObject



19
20
21
22
23
24
25
# File 'lib/ssh_client/config_item.rb', line 19

def listeners
  @cached_listeners ||= if default?
    @listeners
  else
    @listeners.each { |k, l| l += SSHClient.config.listeners[k] }
  end
end

#raise_on_errors=(value) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ssh_client/config_item.rb', line 54

def raise_on_errors=(value)
  if value
    @errors_listener = add_listener(:stderr) do |data|
      Thread.main.raise CommandExitWithError.new(data) if data
    end
  else
    remove_listener(@errors_listener, :stderr) if @errors_listener
  end
end

#remove_listener(listener, io_type = nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/ssh_client/config_item.rb', line 35

def remove_listener(listener, io_type = nil)
  Array(io_type || @listeners.keys).each do |k|
    @listeners[k].delete listener
  end
  @cached_listeners = nil
end

#transport_klassObject



50
51
52
# File 'lib/ssh_client/config_item.rb', line 50

def transport_klass
  @transport || (default? ? Transport::NetSSH : SSHClient.config.transport_klass)
end