From 1c80a80158b4b9197cc7a6512f0ee6041768514f Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Thu, 7 Nov 2019 18:08:57 +0100 Subject: [PATCH] Custom names for services. Services must be created as `[env/]custom_name` and be passed a `type=` parameter when created. --- src/service.cr | 9 ++++++++- src/service/service.cr | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/service.cr b/src/service.cr index f6f48da..8922acb 100644 --- a/src/service.cr +++ b/src/service.cr @@ -54,6 +54,7 @@ commands.push "add", "Adds a service to an environment." do |args| end environment, service = Service.parse_id args[0] + name = service args.each_with_index do |arg, i| next if i == 0 @@ -67,7 +68,9 @@ commands.push "add", "Adds a service to an environment." do |args| _, key, value = match - if key == "domain" + if key == "type" + service = value + elsif key == "domain" domain = value elsif match = key.match /^port:(.*)/ ports[match[1]] = value.to_i @@ -82,6 +85,10 @@ commands.push "add", "Adds a service to an environment." do |args| service.domain = domain end + if name != service + service.name = name + end + service.consumes.each do |token| provider = providers[token.token]? diff --git a/src/service/service.cr b/src/service/service.cr index 8955f4c..fc1a9d2 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -32,6 +32,7 @@ class Service getter environment : Environment getter providers = ProvidersList.new + setter name : String? property domain : String? getter ports = Hash(String, Int32).new @@ -66,7 +67,11 @@ class Service def initialize(specs : SpecParser) assignments = specs.assignments - @reference = ServiceDefinition.get assignments["name"].as_s + # Complicated because of compat with when services had no dedicated + # name and their types were their name. + type = assignments["type"]?.try &.as_s + @name = (assignments["name"]?.try(&.as_s) || type).not_nil! + @reference = ServiceDefinition.get type || @name @domain = assignments["domain"]?.try &.as_s @@ -102,10 +107,14 @@ class Service def to_spec file = [ - "name: #{@reference.name}", + "type: #{@reference.name}", "environment: #{@environment.name}" ] + unless @name.nil? + file << "name: #{@name}" + end + if @ports.size > 0 ports_list = @ports.map do |name, number| "#{name}=#{number}" @@ -144,10 +153,10 @@ class Service # FIXME: At this point, macros would be both more readable and shorter. def name - @reference.name + @name || @reference.name end def type - @reference.type + @reference.name end def command @reference.command @@ -359,6 +368,7 @@ class Service def summary "%-16s #{name.colorize(:white).to_s}\n" % "Name:" + + "%-16s #{type.colorize(:white).to_s}\n" % "Type:" + "%-16s #{environment.name.colorize(:white).to_s} (#{environment.type.to_s.downcase})\n" % "Environment:" + ( if provides.size > 0