Providers are auto-guessed when adding services.

master
Luka Vandervelden 2019-08-10 22:29:11 +02:00
parent 9e7921a803
commit 4eefe465f4
3 changed files with 21 additions and 1 deletions

View File

@ -70,6 +70,10 @@ begin
service.consumes.each do |token| service.consumes.each do |token|
provider = providers[token.token]? provider = providers[token.token]?
if provider.nil?
provider = service.get_default_provider token.token
end
if provider.nil? if provider.nil?
STDERR.puts "This service consumes a “#{token.token}” token, but you have not specified what other service is supposed to provide it." STDERR.puts "This service consumes a “#{token.token}” token, but you have not specified what other service is supposed to provide it."
STDERR.puts "Use the `service add #{args[1]} #{token.token}=<provider>` syntax to specify it." STDERR.puts "Use the `service add #{args[1]} #{token.token}=<provider>` syntax to specify it."

View File

@ -67,6 +67,12 @@ class Environment
_def _def
end end
def get_provider(token)
Service.all.find do |service|
service.environment == self && service.provides? token
end.try &.id
end
def to_s def to_s
"#{name} (#{type.to_s.downcase})" "#{name} (#{type.to_s.downcase})"
end end

View File

@ -56,7 +56,7 @@ class Service
end end
specs.sections.select(&.name.==("consumes")).each do |section| specs.sections.select(&.name.==("consumes")).each do |section|
@providers[section.name] = section.content["from"].as_s @providers[section.options[0]] = section.content["from"].as_s
end end
end end
@ -103,6 +103,12 @@ class Service
@reference.consumes @reference.consumes
end end
def provides?(token)
provides.any? do |provider|
provider.token == token
end
end
private def build_environment private def build_environment
env = {} of String => String env = {} of String => String
@ -369,6 +375,10 @@ class Service
tree tree
end end
def get_default_provider(token) : String?
@environment.get_provider(token) || Environment.root.get_provider(token)
end
end end