Class: Capissh::ServerDefinition
- Inherits:
-
Object
- Object
- Capissh::ServerDefinition
- Includes:
- Comparable
- Defined in:
- lib/capissh/server_definition.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.default_user ⇒ Object
The default user name to use when a user name is not explicitly provided.
-
.wrap_list(*list) ⇒ Object
Turns a list, or something resembling a list, into a properly-formatted ServerDefinition list.
-
.wrap_server(item, options) ⇒ Object
Wraps a string in a ServerDefinition, if it isn’t already.
Instance Method Summary collapse
- #<=>(server) ⇒ Object
- #connect_to_port ⇒ Object
-
#eql?(server) ⇒ Boolean
(also: #==)
Redefined, so that Array#uniq will work to remove duplicate server definitions, based solely on their host names.
-
#hash ⇒ Object
Redefined, so that Array#uniq will work to remove duplicate server definitions, based on their connection information.
-
#initialize(server, options = {}) ⇒ ServerDefinition
constructor
Initialize a ServerDefinition with a string or object that describes the authority URI part, “user@host:port”, for connecting with SSH.
- #to_s ⇒ Object
Constructor Details
#initialize(server, options = {}) ⇒ ServerDefinition
Initialize a ServerDefinition with a string or object that describes the authority URI part, “user@host:port”, for connecting with SSH.
Any object that responds to the following methods, in order of priority, can be used in a ServerDefinition:
-
#host, #user (optional), and #port (optional, default: 22)
-
#authority - responding with something like “[user@]host”
-
#to_s - responding with something like “[user@]host”
If options are passed for the second argument, certain keys will be used:
-
:user - sets the user if one was not given in the authority
-
:port - sets the port if one was not given in the authority
-
:ssh_options - used for connecting with Net::SSH
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/capissh/server_definition.rb', line 51 def initialize(server, ={}) @server = server if @server.respond_to?(:host) @host = @server.host @port = @server.port if @server.respond_to?(:port) @user = @server.user if @server.respond_to?(:user) else if @server.respond_to?(:authority) string = @server. elsif @server.respond_to?(:to_s) string = @server.to_s else raise ArgumentError, "Invalid server for ServerDefinition: #{@server.inspect}. Must respond to #host, #authority, or #to_s" end @user, @host, @port = string.match(/^(?:([^;,:=]+)@|)(.*?)(?::(\d+)|)$/)[1,3] end @options = .dup user_opt, port_opt = @options.delete(:user), @options.delete(:port) @user ||= user_opt @port ||= port_opt @port = @port.to_i if @port end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
34 35 36 |
# File 'lib/capissh/server_definition.rb', line 34 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/capissh/server_definition.rb', line 34 def @options end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
34 35 36 |
# File 'lib/capissh/server_definition.rb', line 34 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
34 35 36 |
# File 'lib/capissh/server_definition.rb', line 34 def server @server end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
34 35 36 |
# File 'lib/capissh/server_definition.rb', line 34 def user @user end |
Class Method Details
.default_user ⇒ Object
The default user name to use when a user name is not explicitly provided
30 31 32 |
# File 'lib/capissh/server_definition.rb', line 30 def self.default_user ENV['USER'] || ENV['USERNAME'] || "not-specified" end |
.wrap_list(*list) ⇒ Object
Turns a list, or something resembling a list, into a properly-formatted ServerDefinition list. Keep an eye on this one – it’s entirely too magical for its own good. In particular, if ServerDefinition ever inherits from Array, this will break.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/capissh/server_definition.rb', line 14 def self.wrap_list(*list) = list.last.is_a?(Hash) ? list.pop : {} if list.length == 1 if list.first.nil? return [] elsif list.first.is_a?(Array) list = list.first end end .merge! list.pop if list.last.is_a?(Hash) list.map do |item| self.wrap_server item, end end |
.wrap_server(item, options) ⇒ Object
Wraps a string in a ServerDefinition, if it isn’t already.
6 7 8 |
# File 'lib/capissh/server_definition.rb', line 6 def self.wrap_server(item, ) item.is_a?(ServerDefinition) ? item : ServerDefinition.new(item, ) end |
Instance Method Details
#<=>(server) ⇒ Object
78 79 80 |
# File 'lib/capissh/server_definition.rb', line 78 def <=>(server) [host, port, user] <=> [server.host, server.port, server.user] end |
#connect_to_port ⇒ Object
98 99 100 |
# File 'lib/capissh/server_definition.rb', line 98 def connect_to_port port || 22 end |
#eql?(server) ⇒ Boolean Also known as: ==
Redefined, so that Array#uniq will work to remove duplicate server definitions, based solely on their host names.
84 85 86 87 88 |
# File 'lib/capissh/server_definition.rb', line 84 def eql?(server) host == server.host && user == server.user && port == server.port end |
#hash ⇒ Object
Redefined, so that Array#uniq will work to remove duplicate server definitions, based on their connection information.
94 95 96 |
# File 'lib/capissh/server_definition.rb', line 94 def hash @hash ||= [host, user, port].hash end |
#to_s ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/capissh/server_definition.rb', line 102 def to_s @to_s ||= begin s = host s = "#{user}@#{s}" if user s = "#{s}:#{port}" if port && port != 22 s end end |