diff --git a/src/procodile.cr b/src/procodile.cr index 994e0e3..9f0f91a 100644 --- a/src/procodile.cr +++ b/src/procodile.cr @@ -3,6 +3,8 @@ require "./procodile/app_determination" require "./procodile/cli" module Procodile + VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }} + class Error < Exception end @@ -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 diff --git a/src/procodile/cli.cr b/src/procodile/cli.cr index 8c0b079..6714a1b 100644 --- a/src/procodile/cli.cr +++ b/src/procodile/cli.cr @@ -2,7 +2,6 @@ require "./config" require "./control_client" require "./commands/*" require "./core_ext/process" -require "./version" module Procodile class CLI diff --git a/src/procodile/commands/help_command.cr b/src/procodile/commands/help_command.cr index c39d5b8..f4ac296 100644 --- a/src/procodile/commands/help_command.cr +++ b/src/procodile/commands/help_command.cr @@ -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 diff --git a/src/procodile/control_session.cr b/src/procodile/control_session.cr index 2acb0ca..41fe0ec 100644 --- a/src/procodile/control_session.cr +++ b/src/procodile/control_session.cr @@ -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, @@ -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 diff --git a/src/procodile/version.cr b/src/procodile/version.cr deleted file mode 100644 index 3b5cb16..0000000 --- a/src/procodile/version.cr +++ /dev/null @@ -1,3 +0,0 @@ -module Procodile - VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }} -end