Better handling of commands.

master
Luka Vandervelden 2019-10-24 20:31:38 +02:00
parent 3ca16cfcce
commit bdeb23c275
1 changed files with 171 additions and 137 deletions

View File

@ -13,17 +13,6 @@ args = [] of String
parser = OptionParser.parse do |parser|
parser.banner = "usage: service <command> [options]\n" +
"\n" +
"commands:\n" +
" start Starts a stopped or dead service.\n" +
" stop Stops a running service.\n" +
" status Shows the current state of a service.\n" +
" show Describe a service in detail.\n" +
" add Add a service to an environment.\n" +
" del Remove a service from an environment.\n" +
" add-environment Creates a new, empty environment.\n" +
" list-environments Lists registered environments.\n" +
"\n" +
"options:\n"
parser.on "-h", "--help", "Prints this help message." do
@ -35,25 +24,38 @@ parser = OptionParser.parse do |parser|
end
end
command = args[0]?
if command.nil?
STDERR << parser << "\n"
exit 1
alias Command = Proc(Array(String), Nil)
alias CommandTuple = Tuple(String, String, Command)
class CommandsList
def initialize
@commands = Array(CommandTuple).new
end
ServiceDefinition.load SERVICES_DIRECTORY
Environment.load ENVIRONMENTS_DIRECTORY
Service.load RC_DIRECTORY
def push(name : String, description : String, &proc : Command)
@commands << Tuple.new(name, description, proc)
end
begin
if args[0] == "help"
puts parser
elsif args[0] == "add"
def find(&block : Proc(CommandTuple, Bool))
@commands.find do |tuple|
if block.call tuple
next true
end
end
end
def to_s
"commands:\n\n"+ @commands.map do |tuple|
" %-32s %s" % [tuple[0], tuple[1]]
end.join "\n"
end
end
commands = CommandsList.new
commands.push "add", "Adds a service to an environment." do |args|
providers = Hash(String, String).new
environment, service = Service.parse_id args[1]
environment, service = Service.parse_id args[0]
args.shift
args.each_with_index do |arg, i|
next if i == 0
@ -85,10 +87,14 @@ begin
end
pp! service.providers
end.write RC_DIRECTORY
elsif args[0] == "del"
Service.new(args[1], args[2]?).remove RC_DIRECTORY
elsif args[0] == "start"
services = args[1..args.size].map do |arg|
end
commands.push "del", "Removes a service from an environment." do |args|
Service.new(args[0], args[1]?).remove RC_DIRECTORY
end
commands.push "start", "Starts a service." do
services = args.map do |arg|
service = Service.get_by_id(arg)
unless service
@ -106,8 +112,10 @@ begin
service.start PID_DIRECTORY, LOG_DIRECTORY
end
end
elsif args[0] == "stop"
services = args[1..args.size].map do |arg|
end
commands.push "stop", "Stops a running service." do |args|
services = args.map do |arg|
service = Service.get_by_id(arg)
unless service
@ -129,8 +137,9 @@ begin
service.stop PID_DIRECTORY
end
end
elsif args[0] == "status"
args.shift
end
commands.push "status", "Prints the status of services." do |args|
child = Process.run "#{OWN_LIBEXEC_DIR}/status", args,
output: Process::Redirect::Inherit,
error: Process::Redirect::Inherit
@ -142,13 +151,15 @@ begin
end
exit return_value
elsif args[0] == "show"
end
commands.push "show", "Shows a service's configuration and state." do |args|
service = Service.all.find do |service|
unless service.name == args[1]
unless service.name == args[0]
next false
end
env = args[2]? || "root"
env = args[1]? || "root"
if service.environment.name != env
next false
end
@ -161,16 +172,39 @@ begin
STDERR << "No such service is registered.\n"
exit 2
end
elsif args[0] == "add-environment"
Environment.new(args[1]).write ENVIRONMENTS_DIRECTORY
elsif args[0] == "list-environments"
end
commands.push "add-environment", "Creates a new (empty) environment." do |arg|
Environment.new(args[9]).write ENVIRONMENTS_DIRECTORY
end
commands.push "list-environments", "Lists all currently defined environments.s", do |arg|
Environment.all.map do |env|
puts env.to_s
end
else
end
commands.push "help", "Prints this help message." do
puts parser
puts
puts commands.to_s
end
command = commands.find &.[0].==(args[0]?)
if command.nil?
STDERR << parser << "\n"
STDERR << "\n"
STDERR << commands.to_s << "\n"
exit 1
end
ServiceDefinition.load SERVICES_DIRECTORY
Environment.load ENVIRONMENTS_DIRECTORY
Service.load RC_DIRECTORY
begin
args.shift
command.[2].call(args)
rescue e : Service::Exception
STDERR << e.message << "\n"
exit 2