def search_teams(team_names, global=false)
r = []
team_names.each do |team_name|
teams_for_name =
global ?
rest_client.search_teams(team_name, global) :
rest_client.search_owned_teams(team_name)
team_for_name = nil
suggestions = nil
if (exact_matches = teams_for_name.select {|t| t.name == team_name }).present?
if exact_matches.length == 1
team_for_name = exact_matches.first
else
raise RHC::TeamNotFoundException.new("There is more than one team named '#{team_name}'. " +
"Please use the --ids flag and specify the exact id of the team you want to manage.")
end
elsif (case_insensitive_matches = teams_for_name.select {|t| t.name =~ /^#{Regexp.escape(team_name)}$/i }).present?
if case_insensitive_matches.length == 1
team_for_name = case_insensitive_matches.first
else
suggestions = case_insensitive_matches
end
else
suggestions = teams_for_name
end
if team_for_name
r << team_for_name
elsif suggestions.present?
msg = global ? "No global team found with the name '#{team_name}'." : "You do not have a team named '#{team_name}'."
raise RHC::TeamNotFoundException.new(msg + " Did you mean one of the following?\n#{suggestions[0..50].map(&:name).join(", ")}")
else
msg = global ? "No global team found with the name '#{team_name}'." : "You do not have a team named '#{team_name}'."
raise RHC::TeamNotFoundException.new(msg)
end
end
r.flatten
end