Sha256: 97224ff2513006800bafa4986f251fce3307a1384f85a584cb30411040777ee5

Contents?: true

Size: 1.68 KB

Versions: 37

Compression:

Stored size: 1.68 KB

Contents

class KuberKit::ServiceDeployer::ServiceListResolver
  include KuberKit::Import[
    "core.service_store"
  ]

  Contract KeywordArgs[
    services: Optional[ArrayOf[String]],
    tags:     Optional[ArrayOf[String]]
  ] => ArrayOf[String]
  def resolve(services: [], tags: [])
    all_definitions = service_store.all_definitions.values

    included_services, excluded_services = split_by_inclusion(services)
    included_tags,     excluded_tags     = split_by_inclusion(tags)
    
    included_definitions = all_definitions.select do |definition|
      service_name = definition.service_name.to_s
      service_tags = definition.to_service_attrs.tags.map(&:to_s)

      matches_any?([service_name], included_services) ||
      matches_any?(service_tags, included_tags)
    end

    included_definitions = included_definitions.reject do |definition|
      service_name = definition.service_name.to_s
      service_tags = definition.to_service_attrs.tags.map(&:to_s)

      matches_any?([service_name], excluded_services) ||
      matches_any?(service_tags, excluded_tags)
    end

    included_definitions.map(&:service_name).map(&:to_s)
  end

  Contract Array => Array
  def split_by_inclusion(array)
    excluded, included = array.partition{|e| e.start_with?('-') }

    excluded.map!{ |item| item.gsub(/^\-/, "") }

    [included, excluded]
  end

  Contract String, String => Bool
  def matches_name?(name, pattern)
    regexp = Regexp.new("\\A" + pattern.gsub("*", "[a-z\_0-9]+") + "\\z")
    name.match?(regexp)
  end

  Contract ArrayOf[String], ArrayOf[String] => Bool
  def matches_any?(names, patterns)
    patterns.any? do |pattern|
      names.any? { |name| matches_name?(name, pattern) }
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
kuber_kit-0.2.6 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.2.5 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.2.4 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.2.3 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.2.2 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.2.1 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.2.0 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.9 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.8 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.7 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.6 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.5 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.4 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.3 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.2 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.1 lib/kuber_kit/service_deployer/service_list_resolver.rb
kuber_kit-0.1.0 lib/kuber_kit/service_deployer/service_list_resolver.rb