service/src/status.cr

79 lines
1.6 KiB
Crystal

require "./service/service.cr"
require "./config.cr"
context = Service::Context.new
context.load
LibC.setuid 0
LibC.setgid 0
verbose = ENV["SERVICE_VERBOSE"]?.try &.==("true")
list_status = false
services = ARGV
if services.size == 0
list_status = true
services = context.services
else
services = services.map do |id|
context.get_service_by_id id
end
end
if services.size == 1 && ! list_status
services[0]?.try do |service|
puts service.status PID_DIRECTORY
exit 0
end
exit 1
else
service_not_found = false
services.each do |service|
if service.nil?
service_not_found = true
else
if verbose
STDOUT << ("%-20s " % "#{service.full_id}:").colorize(:white).to_s
status_string = "%-15s" % service.status PID_DIRECTORY
status_string = case service.status PID_DIRECTORY
when Service::Status::Dead
status_string.colorize(:red).bright
when Service::Status::Running
status_string.colorize(:green).bright
when Service::Status::NonRunnable
status_string.colorize(:cyan)
when Service::Status::Stopped
status_string.colorize :yellow
else
status_string.colorize # ¯\_(ツ)_/¯
end
STDOUT << status_string.to_s
STDOUT << (
service.ports.map do |name, number|
"#{name}:#{number.colorize(:magenta).to_s}"
end +
service.providers.map do |token, id|
"-#{token.colorize(:yellow)}:" + id.colorize(:cyan).to_s
end +
service.provides.map do |token|
"+#{token.token.colorize(:green).to_s}"
end
).join " "
STDOUT << "\n"
else
puts "#{service.full_id}: #{service.status PID_DIRECTORY}"
end
end
end
exit 1 if service_not_found
end