Skip to content

Allow duplicate servers in ServerList #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/net/ssh/multi/server_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ class ServerList
# Create a new ServerList that wraps the given server list. Duplicate entries
# will be discarded.
def initialize(list=[])
@list = list.uniq
options = list.last.is_a?(Hash) ? list.pop : {}
@allow_duplicate_servers = options.delete :allow_duplicate_servers
@list = @allow_duplicate_servers ? list : list.uniq
end

# Adds the given server to the list, and returns the argument. If an
# identical server definition already exists in the collection, the
# argument is _not_ added, and the existing server record is returned
# instead.
def add(server)
index = @list.index(server)
if index
server = @list[index]
else
if @allow_duplicate_servers
@list.push(server)
else
index = @list.index(server)
if index
server = @list[index]
else
@list.push(server)
end
end
server
end
Expand Down Expand Up @@ -71,10 +77,10 @@ def flatten
end
end

result.uniq
@allow_duplicate_servers ? result : result.uniq
end

alias to_ary flatten
end

end; end; end
end; end; end
9 changes: 7 additions & 2 deletions lib/net/ssh/multi/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class Session
# :warn if connection errors should cause a warning.
attr_accessor :on_error

# Whether to allow duplicate server definitions. This defaults to false,
# but you may set it to true if you really want to run parallel commands on
# the same server.
attr_accessor :allow_duplicate_servers

# The default user name to use when connecting to a server. If a user name
# is not given for a particular server, this value will be used. It defaults
# to ENV['USER'] || ENV['USERNAME'], or "unknown" if neither of those are
Expand Down Expand Up @@ -169,7 +174,7 @@ class Session
# session.use ...
# end
def initialize(options={})
@server_list = ServerList.new
@server_list = ServerList.new([ { :allow_duplicate_servers => options[:allow_duplicate_servers] } ])
@groups = Hash.new { |h,k| h[k] = ServerList.new }
@gateway = nil
@open_groups = []
Expand Down Expand Up @@ -357,7 +362,7 @@ def servers_for(*criteria)
aggregator.concat(servers)
end

list.uniq
allow_duplicate_servers ? list : list.uniq
end
end

Expand Down