From 7fdccfa1b2cd7f002292509eb35a8a4af4ed08bf Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Tue, 29 Oct 2019 13:08:58 +0100 Subject: [PATCH] `service status --verbose` --- src/service.cr | 6 ++++++ src/status.cr | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/service.cr b/src/service.cr index 146df51..f6f48da 100644 --- a/src/service.cr +++ b/src/service.cr @@ -14,6 +14,7 @@ parser = uninitialized OptionParser args = [] of String force = false +verbose = false alias Command = Proc(Array(String), Nil) alias CommandTuple = Tuple(String, String, Command) @@ -215,6 +216,8 @@ commands.push "stop", "Stops a running service." do |args| end commands.push "status", "Prints the status of services." do |args| + ENV["SERVICE_VERBOSE"] = verbose.to_s + child = Process.run "#{OWN_LIBEXEC_DIR}/status", args, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit @@ -309,6 +312,9 @@ parser = OptionParser.parse do |cli| cli.on "-f", "--force", "Ignores warnings and executes dangerous operations." do force = true end + cli.on "-v", "--verbose", "Prints more data when doing things." do + verbose = true + end cli.unknown_args do |x| args = x diff --git a/src/status.cr b/src/status.cr index 741be59..4768dcc 100644 --- a/src/status.cr +++ b/src/status.cr @@ -10,6 +10,8 @@ Service.load RC_DIRECTORY LibC.setuid 0 LibC.setgid 0 +verbose = ENV["SERVICE_VERBOSE"]?.try &.==("true") + list_status = false services = ARGV if services.size == 0 @@ -35,7 +37,37 @@ else if service.nil? service_not_found = true else - puts "#{service.full_id}: #{service.status PID_DIRECTORY}" + if verbose + STDOUT << ("%-20s " % "#{service.full_id}:").colorize(:white).to_s + + status_string = "%-10s" % 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::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