`service add` can be specified token providers.

master
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" if args[0] == "help"
puts parser puts parser
elsif args[0] == "add" 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" elsif args[0] == "del"
Service.new(args[1], args[2]?).remove RC_DIRECTORY Service.new(args[1], args[2]?).remove RC_DIRECTORY
elsif args[0] == "start" elsif args[0] == "start"

View File

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