89 lines
2.2 KiB
Crystal
89 lines
2.2 KiB
Crystal
require "yaml"
|
|
require "specparser"
|
|
|
|
class ServiceDefinition
|
|
struct Consumes
|
|
getter token : String
|
|
def initialize(@token)
|
|
@optional = false
|
|
|
|
if @token.match /\?$/
|
|
@token = @token.gsub /\?$/, ""
|
|
@optional = true
|
|
end
|
|
end
|
|
end
|
|
struct Provides
|
|
getter token : String
|
|
def initialize(@token)
|
|
end
|
|
end
|
|
struct Checks
|
|
getter name : String
|
|
getter command : String
|
|
getter directory : String?
|
|
getter file : String?
|
|
|
|
def initialize(@name, @command, @file = nil, @directory = nil)
|
|
end
|
|
def initialize(section : SpecParser::Section)
|
|
@name = section.content["name"].as_s
|
|
@file = section.content["file"]?.try &.as_s
|
|
@directory = section.content["directory"]?.try &.as_s
|
|
@command = section.content["command"].as_s
|
|
end
|
|
end
|
|
|
|
class_getter all = [] of ServiceDefinition
|
|
|
|
getter name : String
|
|
getter command : String
|
|
getter stop_command : String?
|
|
getter directory : String?
|
|
getter user : String?
|
|
getter provides : String?
|
|
getter consumes : Array(Consumes)
|
|
getter environment_variables : Array(String)
|
|
getter checks : Array(Checks)
|
|
getter provides : Array(Provides)
|
|
|
|
def initialize(specs : SpecParser)
|
|
sections = specs.sections
|
|
specs = specs.assignments
|
|
@name = specs["name"].as_s
|
|
@command = specs["command"].as_s
|
|
@stop_command = specs["stop-command"]?.try &.as_s
|
|
@directory = specs["directory"]?.try &.as_s
|
|
@user = specs["user"]?.try &.as_s
|
|
@provides = specs["provides"]?.try &.as_a_or_s.map { |x| Provides.new x } || Array(Provides).new
|
|
@consumes = specs["consumes"]?.try &.as_a_or_s.map { |x| Consumes.new x } || Array(Consumes).new
|
|
@checks = sections.select(&.name.== "check").map { |x| Checks.new x } || Array(Checks).new
|
|
@environment_variables = specs["environment-variables"]?.try &.as_a_or_s || Array(String).new
|
|
end
|
|
|
|
def self.load(path)
|
|
Dir.each_child path do |child|
|
|
if child.match /\.spec$/
|
|
@@all << ServiceDefinition.new SpecParser.parse("#{path}/#{child}").not_nil!
|
|
else
|
|
next
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.get(name) : ServiceDefinition
|
|
_def = @@all.find &.name.==(name)
|
|
|
|
if _def.nil?
|
|
raise Exception.new "Service '#{name}' does not exist."
|
|
end
|
|
|
|
_def
|
|
end
|
|
|
|
def to_s
|
|
name
|
|
end
|
|
end
|
|
|