cli: add RescueMixin

Add an error handler to the CLI
This commit is contained in:
0x1eef 2024-02-17 20:05:25 -03:00
parent a1e1739b63
commit f2f1474fac
11 changed files with 39 additions and 3 deletions

View file

@ -8,6 +8,7 @@ class Twenty::Command < Cmd
require_relative "command/mixin/common_option_mixin"
require_relative "command/mixin/migration_mixin"
require_relative "command/mixin/sqlite_mixin"
require_relative "command/mixin/rescue_mixin"
##
# commands

View file

@ -5,6 +5,7 @@ class Twenty::Command::Connect < Twenty::Command
description: "Connect a project to twenty"
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
def run
options = parse_options(argv)

View file

@ -6,6 +6,7 @@ class Twenty::Command::Console < Twenty::Command
include CommonOptionMixin
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
def run
options = parse_options(argv)

View file

@ -5,6 +5,7 @@ class Twenty::Command::Disconnect < Twenty::Command
description: "Disconnect a project from twenty"
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
def run
options = parse_options(argv)

View file

@ -5,6 +5,7 @@ class Twenty::Command::Down < Twenty::Command
description: "Stop the twenty web server"
include Twenty::Path
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
def run
options = parse_options(argv)
@ -15,7 +16,7 @@ class Twenty::Command::Down < Twenty::Command
def run_command(options)
if File.readable?(pidfile)
Process.kill('SIGINT', Integer(pid))
Process.kill("SIGINT", Integer(pid))
else
warn "PID file is not readable."
end
@ -27,6 +28,6 @@ class Twenty::Command::Down < Twenty::Command
def pid
@pid ||= File
.binread(pidfile)
.gsub(/[^\d]/, '')
.gsub(/[^\d]/, "")
end
end

View file

@ -5,6 +5,7 @@ class Twenty::Command::Migrate < Twenty::Command
description: "Migrate the database"
include CommonOptionMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
def run
options = parse_options(argv)

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
class Twenty::Command
module RescueMixin
def run(...)
super(...)
rescue => ex
require "paint"
$stderr.print "\n",
" ", Paint[" Exception ", :white, :red, :bold], "\n",
" ", Paint[ex.class.to_s, :bold], "\n",
" ", ex.message, "\n\n",
" ", Paint[" Backtrace ", :white, :blue, :bold], "\n",
format_backtrace(ex.backtrace), "\n",
"\n"
end
private
def format_backtrace(backtrace)
backtrace.last(5).map do
" #{_1.gsub(Dir.getwd, "")}"
end.join("\n")
end
end
end

View file

@ -20,6 +20,7 @@ class Twenty::Command::Up < Twenty::Command
include CommonOptionMixin
prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin
prepend Twenty::Command::RescueMixin
def run
options = parse_options(argv)

View file

@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
gem.description = gem.summary
gem.executables = ["twenty", "20"]
gem.add_runtime_dependency "cmd.rb", "~> 0.4"
gem.add_runtime_dependency "paint", "~> 2.3"
gem.add_development_dependency "test-unit", "~> 3.5.7"
gem.add_development_dependency "yard", "~> 0.9"
gem.add_development_dependency "redcarpet", "~> 3.5"

View file

@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
gem.description = gem.summary
gem.executables = ["twenty", "20"]
gem.add_runtime_dependency "cmd.rb", "~> 0.4"
gem.add_runtime_dependency "paint", "~> 2.3"
gem.add_development_dependency "test-unit", "~> 3.5.7"
gem.add_development_dependency "yard", "~> 0.9"
gem.add_development_dependency "redcarpet", "~> 3.5"

View file

@ -39,7 +39,8 @@ module Twenty::Servlet::ServerMixin
{
DocumentRoot: Twenty.build,
BindAddress: cli_options.bind,
Port: cli_options.port
Port: cli_options.port,
Logger: WEBrick::Log.new(File::NULL)
}
end
end