Commit dd911fd7 authored by Jyrki's avatar Jyrki 🐾

Refactor commands

parent 09dee240
require "logger"
require "./use_case/cli_commands/start_bot"
require "./use_case/cli_commands"
class Application
COMMANDS = {
"start" => UseCase::CliCommands::StartBot
}
def self.run(argv)
new(argv.dup).run
end
......@@ -24,39 +20,13 @@ class Application
exit_with_usage if @argv.empty?
command = @argv.shift.downcase.strip
exit_with_usage unless COMMANDS.has_key?(command)
exit_with_usage unless UseCase::CliCommands::COMMANDS.has_key?(command)
COMMANDS[command].call(@argv)
UseCase::CliCommands::COMMANDS[command].call(@argv)
end
private def exit_with_usage(status = 1)
usage
UseCase::PrintUsage.call
exit status
end
private def usage
puts <<-EOF
Usage: #{PROGRAM_NAME} [command]
Commands:
EOF
filler_size = COMMANDS.keys.map(&.size).max + 3
COMMANDS.each do |command, klass|
filler = " " * (filler_size - command.size)
puts [" ", command, filler, klass.description].join("")
end
puts <<-EOF
Configuration is handled via the following environment variables:
ENABLE_WEBHOOK If set, start up a server for the webhook.
LOG_LEVEL The log level.
Allowed values: DEBUG, INFO, WARN, ERROR, FATAL
Default: INFO
TELEGRAM_API_TOKEN The API token for accessing Telegram's bot API.
WEBHOOK_HOST The host address for the webhook server to bind to.
Default: 0.0.0.0
WEBHOOK_PORT The port for the webhook server to bind to.
Default: 8080
EOF
end
end
require "./cli_commands/start_bot"
require "./cli_commands/help"
module UseCase
module CliCommands
COMMANDS = {{UseCase::CliCommands::Base.subclasses}}.map do |subclass|
{subclass.command_name, subclass}
end.to_h
end
end
......@@ -14,6 +14,10 @@ module UseCase
exit 2
end
def self.command_name : String
raise NotImplementedError.new("Implement #{self.class}.command_name!")
end
def self.description
""
end
......
require "./base"
require "../../print_usage"
module UseCase
module CliCommands
class Help < CliCommands::Base
def self.command_name
"help"
end
def self.description
"Displays this usage text."
end
def call(_argv)
UseCase::PrintUsage.call
end
end
end
end
......@@ -5,6 +5,10 @@ require "../../errors"
module UseCase
module CliCommands
class StartBot < CliCommands::Base
def self.command_name
"start"
end
def self.description
"Starts the telegram bot."
end
......
require "./base"
require "./cli_commands"
module UseCase
class PrintUsage < Base
def call
puts header
puts commands
puts configuration
end
private def header
<<-EOF
Usage: #{PROGRAM_NAME} [command]
Commands:
EOF
end
private def commands
filler_size = UseCase::CliCommands::COMMANDS.keys.map(&.size).max + 3
UseCase::CliCommands::COMMANDS.each do |command, klass|
filler = " " * (filler_size - command.size)
puts [" ", command, filler, klass.description].join("")
end
end
private def configuration
<<-EOF
Configuration is handled via the following environment variables:
ENABLE_WEBHOOK If set, start up a server for the webhook.
LOG_LEVEL The log level.
Allowed values: DEBUG, INFO, WARN, ERROR, FATAL
Default: INFO
TELEGRAM_API_TOKEN The API token for accessing Telegram's bot API.
WEBHOOK_HOST The host address for the webhook server to bind to.
Default: 0.0.0.0
WEBHOOK_PORT The port for the webhook server to bind to.
Default: 8080
EOF
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment