Skip to content

Commit

Permalink
cleanup more qualified namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Nov 23, 2024
1 parent 84cc6e8 commit db95ffb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 102 deletions.
192 changes: 97 additions & 95 deletions src/procodile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./procodile/app_determination"
require "./procodile/cli"

module Procodile
VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}

class Error < Exception
end

Expand All @@ -13,121 +15,121 @@ module Procodile
private def self.bin_path : String
File.join(root, "bin", "procodile")
end
end

ORIGINAL_ARGV = ARGV.join(" ")
command = ARGV[0]? || "help"
options = {} of Symbol => String
cli = Procodile::CLI.new
ORIGINAL_ARGV = ARGV.join(" ")
command = ARGV[0]? || "help"
options = {} of Symbol => String
cli = CLI.new

opt = OptionParser.new do |parser|
parser.banner = "Usage: procodile #{command} [options]"
opt = OptionParser.new do |parser|
parser.banner = "Usage: procodile #{command} [options]"

parser.on("-r", "--root PATH", "The path to the root of your application") do |root|
options[:root] = root
end
parser.on("-r", "--root PATH", "The path to the root of your application") do |root|
options[:root] = root
end

parser.on("--procfile PATH", "The path to the Procfile (defaults to: Procfile)") do |path|
options[:procfile] = path
end
parser.on("--procfile PATH", "The path to the Procfile (defaults to: Procfile)") do |path|
options[:procfile] = path
end

parser.on("-h", "--help", "Show this help message and exit") do
STDOUT.puts parser
exit 0
end
parser.on("-h", "--help", "Show this help message and exit") do
STDOUT.puts parser
exit 0
end

parser.on("-v", "--version", "Show version") do
STDOUT.puts Procodile::VERSION
exit 0
end
parser.on("-v", "--version", "Show version") do
STDOUT.puts VERSION
exit 0
end

parser.invalid_option do |flag|
STDERR.puts "Invalid option: #{flag}.\n\n"
STDERR.puts parser
exit 1
end
parser.invalid_option do |flag|
STDERR.puts "Invalid option: #{flag}.\n\n"
STDERR.puts parser
exit 1
end

parser.missing_option do |flag|
STDERR.puts "Missing option for #{flag}\n\n"
STDERR.puts parser
exit 1
parser.missing_option do |flag|
STDERR.puts "Missing option for #{flag}\n\n"
STDERR.puts parser
exit 1
end
end
end

if cli.class.commands[command]? && (option_proc = cli.class.commands[command].options)
option_proc.call(opt, cli)
end
if cli.class.commands[command]? && (option_proc = cli.class.commands[command].options)
option_proc.call(opt, cli)
end

opt.parse

# duplicate on this line is necessory for get new parsed ARGV
command = ARGV[0]? || "help"

# Get the global configuration file data
global_config_path = ENV["PROCODILE_CONFIG"]? || "/etc/procodile"

global_config = if File.file?(global_config_path)
Array(Procodile::Config::GlobalOption).from_yaml(File.read(global_config_path))
else
[] of Procodile::Config::GlobalOption
end

# Create a determination to work out where we want to load our app from
ap = Procodile::AppDetermination.new(
FileUtils.pwd,
options[:root]?,
options[:procfile]?,
global_config
)

if ap.ambiguous?
if (app_id = ENV["PROCODILE_APP_ID"]?)
ap.set_app_id_and_find_root_and_procfile(app_id.to_i)
elsif ap.app_options.empty?
STDERR.puts "Error: Could not find Procfile in #{FileUtils.pwd}/Procfile".colorize.red
exit 1
else
puts "There are multiple applications configured in #{global_config_path}"
puts "Choose an application:".colorize.light_gray.on_magenta

ap.app_options.each do |i, app|
col = i % 3
print "#{(i + 1)}) #{app}"[0, 28].ljust(col != 2 ? 30 : 0, ' ')
if col == 2 || i == ap.app_options.size - 1
puts
opt.parse

# duplicate on this line is necessory for get new parsed ARGV
command = ARGV[0]? || "help"

# Get the global configuration file data
global_config_path = ENV["PROCODILE_CONFIG"]? || "/etc/procodile"

global_config = if File.file?(global_config_path)
Array(Config::GlobalOption).from_yaml(File.read(global_config_path))
else
[] of Config::GlobalOption
end

# Create a determination to work out where we want to load our app from
ap = AppDetermination.new(
FileUtils.pwd,
options[:root]?,
options[:procfile]?,
global_config
)

if ap.ambiguous?
if (app_id = ENV["PROCODILE_APP_ID"]?)
ap.set_app_id_and_find_root_and_procfile(app_id.to_i)
elsif ap.app_options.empty?
STDERR.puts "Error: Could not find Procfile in #{FileUtils.pwd}/Procfile".colorize.red
exit 1
else
puts "There are multiple applications configured in #{global_config_path}"
puts "Choose an application:".colorize.light_gray.on_magenta

ap.app_options.each do |i, app|
col = i % 3
print "#{(i + 1)}) #{app}"[0, 28].ljust(col != 2 ? 30 : 0, ' ')
if col == 2 || i == ap.app_options.size - 1
puts
end
end
end

input = STDIN.gets
if !input.nil?
app_id = input.strip.to_i - 1
input = STDIN.gets
if !input.nil?
app_id = input.strip.to_i - 1

if ap.app_options[app_id]?
ap.set_app_id_and_find_root_and_procfile(app_id)
else
puts "Invalid app number: #{app_id + 1}"
exit 1
if ap.app_options[app_id]?
ap.set_app_id_and_find_root_and_procfile(app_id)
else
puts "Invalid app number: #{app_id + 1}"
exit 1
end
end
end
end
end

begin
if command != "help"
cli.config = Procodile::Config.new(ap.root || "", ap.procfile)
begin
if command != "help"
cli.config = Config.new(ap.root || "", ap.procfile)

if cli.config.user && ENV["USER"] != cli.config.user
STDERR.puts "Procodile must be run as #{cli.config.user}. Re-executing as #{cli.config.user}...".colorize.red
if cli.config.user && ENV["USER"] != cli.config.user
STDERR.puts "Procodile must be run as #{cli.config.user}. Re-executing as #{cli.config.user}...".colorize.red

Process.exec(
command: "sudo -H -u #{cli.config.user} -- #{$0} #{ORIGINAL_ARGV}",
shell: true
)
::Process.exec(
command: "sudo -H -u #{cli.config.user} -- #{$0} #{ORIGINAL_ARGV}",
shell: true
)
end
end
end

cli.dispatch(command)
rescue ex : Procodile::Error
STDERR.puts "Error: #{ex.message}".colorize.red
exit 1
cli.dispatch(command)
rescue ex : Error
STDERR.puts "Error: #{ex.message}".colorize.red
exit 1
end
end
1 change: 0 additions & 1 deletion src/procodile/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ require "./config"
require "./control_client"
require "./commands/*"
require "./core_ext/process"
require "./version"

module Procodile
class CLI
Expand Down
2 changes: 1 addition & 1 deletion src/procodile/commands/help_command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Procodile
end

private def help : Nil
puts "Welcome to Procodile v#{Procodile::VERSION}".colorize.light_gray.on_magenta
puts "Welcome to Procodile v#{VERSION}".colorize.light_gray.on_magenta
puts "For documentation see https://adam.ac/procodile."
puts

Expand Down
4 changes: 2 additions & 2 deletions src/procodile/control_session.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Procodile
loaded_at = @supervisor.config.loaded_at

result = ControlClient::ReplyOfStatusCommand.new(
version: Procodile::VERSION,
version: VERSION,
messages: @supervisor.messages,
root: @supervisor.config.root,
app_name: @supervisor.config.app_name,
Expand Down Expand Up @@ -113,7 +113,7 @@ module Procodile
if callable[command]?
begin
callable[command].call(options)
rescue e : Procodile::Error
rescue e : Error
Procodile.log nil, "control", "Error: #{e.message}".colorize.red.to_s
"500 #{e.message}"
end
Expand Down
3 changes: 0 additions & 3 deletions src/procodile/version.cr

This file was deleted.

0 comments on commit db95ffb

Please sign in to comment.