service add can be specified token providers.

This commit is contained in:
Luka Vandervelden 2019-08-10 22:02:11 +02:00
parent efe2c0e964
commit 9e7921a803
2 changed files with 41 additions and 13 deletions

View File

@ -49,7 +49,37 @@ begin
if args[0] == "help"
puts parser
elsif args[0] == "add"
Service.new(args[1], args[2]?).write RC_DIRECTORY
environment : String? = nil
providers = Hash(String, String).new
args.each_with_index do |arg, i|
next if i == 0
match = arg.match /(.*)=(.*)/
if match.nil?
# FIXME: Check environment is not defined already.
environment = arg
next
end
providers[match[1]] = match[2]
end
Service.new(args[1], environment).tap do |service|
service.consumes.each do |token|
provider = providers[token.token]?
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 "Use the `service add #{args[1]} #{token.token}=<provider>` syntax to specify it."
exit 1
end
service.providers[token.token] = provider
end
pp! service.providers
end.write RC_DIRECTORY
elsif args[0] == "del"
Service.new(args[1], args[2]?).remove RC_DIRECTORY
elsif args[0] == "start"

View File

@ -17,7 +17,6 @@ end
class Service
getter environment : Environment
getter providers = ProvidersList.new
getter consumed_tokens = Array(Consumer).new
class Exception < ::Exception
end
@ -35,17 +34,13 @@ class Service
end
end
def initialize(name, environment_name : String?, @consumed_tokens = [] of Consumer)
def initialize(name, environment_name : String?)
@reference = ServiceDefinition.get name
@environment = if environment_name.nil? || environment_name == ""
Environment.root
else
Environment.get environment_name
end
@consumed_tokens.each do |consume|
@providers[consume.token] = consume.from
end
end
def initialize(specs : Specs)
@ -61,7 +56,7 @@ class Service
end
specs.sections.select(&.name.==("consumes")).each do |section|
@consumed_tokens << Consumer.new section.options[0], section.content["from"].as_s
@providers[section.name] = section.content["from"].as_s
end
end
@ -72,9 +67,9 @@ class Service
# FIXME: consumed tokens are missing.
]
@consumed_tokens.each do |token|
file << "%consumes #{token.token}"
file << " from: #{token.from}"
@providers.each do |token, provider|
file << "%consumes #{token}"
file << " from: #{provider}"
end
file.join("\n") + "\n"
@ -104,6 +99,9 @@ class Service
def provides
@reference.provides
end
def consumes
@reference.consumes
end
private def build_environment
env = {} of String => String
@ -343,8 +341,8 @@ class Service
def dependency_tree
tree = [self] of ServiceTree
@consumed_tokens.each do |token|
service = Service.get_by_id token.from
@providers.each do |token, provider_id|
service = Service.get_by_id provider_id
unless service
# FIXME: Does it make the dep tree invalid?