Add support for wildcard matches with '*' on roles and hosts.

eg:
  --roles=*_chicago,*_tokyo
  --hosts=app-*

Useful for targeted deploys.
This commit is contained in:
Matthew Kent
2023-11-12 23:22:08 -08:00
parent 8a85840a47
commit 7137850354
3 changed files with 42 additions and 2 deletions

View File

@@ -28,11 +28,11 @@ class Kamal::Commander
end
def specific_roles=(role_names)
@specific_roles = config.roles.select { |r| role_names.include?(r.name) } if role_names.present?
@specific_roles = Kamal::Utils.filter_specific_items(role_names, config.roles) if role_names.present?
end
def specific_hosts=(hosts)
@specific_hosts = config.all_hosts & hosts if hosts.present?
@specific_hosts = Kamal::Utils.filter_specific_items(hosts, config.all_hosts) if hosts.present?
end
def primary_host

View File

@@ -58,4 +58,20 @@ module Kamal::Utils
.gsub(/`/, '\\\\`')
.gsub(DOLLAR_SIGN_WITHOUT_SHELL_EXPANSION_REGEX, '\$')
end
# Apply a list of host or role filters, including wildcard matches
def filter_specific_items(filters, items)
matches = []
Array(filters).select do |filter|
matches += Array(items).select do |item|
# Only allow * for a wildcard
pattern = Regexp.escape(filter).gsub('\*', '.*')
# items are roles or hosts
(item.respond_to?(:name) ? item.name : item).match(/^#{pattern}$/)
end
end
matches
end
end