service/src/service/environment.cr

71 lines
1.3 KiB
Crystal
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

require "specparser"
require "./context.cr"
class Environment
enum Type
Prefix
RootFileSystem
end
getter name : String
getter type : Type = Type::Prefix
getter files = Array(ServiceDefinition::FileDefinition).new
# The place well put services data and configuration.
@root : String?
@context : Service::Context
def initialize(context)
initialize context, "root"
end
def initialize(@context, @name, type = "prefix")
@type = Type.parse type
@files = Array(ServiceDefinition::FileDefinition).new
end
def initialize(@context, @name, specs : SpecParser)
assignments = specs.assignments
assignments["type"].try &.as_s.tap do |type|
@type = Type.parse type
end
specs.sections.select(&.name.==("check")).each do |check|
@files << ServiceDefinition::FileDefinition.new check
end
end
def root
@root || "#{SERVED_DATA_DIRECTORY}/#{@name}"
end
def write(dir : String)
File.write "#{dir}/#{@name}.spec", to_spec
end
def remove(dir : String)
File.delete "#{dir}/#{@name}.spec"
end
def to_spec
[
"type: #{@type.to_s}"
].join("\n") + "\n"
end
def get_provider(token)
@context.services.find do |service|
service.environment == self && service.provides? token
end.try &.id
end
def to_s
"#{name} (#{type.to_s.downcase})"
end
end