Skip to content

Commit

Permalink
Merge branch 'fix_use_global_options'
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Nov 13, 2024
2 parents 0dba60e + f90ee94 commit cdba486
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/alpine_x86_64_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
key: ${{ runner.os }}-shards-${{ hashFiles('shard.yml') }}
restore-keys: ${{ runner.os }}-shards-
- name: Download source
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install shards
run: shards check || shards install --without-development
- name: Check formatting
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gnu_x86_64_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
key: ${{ runner.os }}-shards-${{ hashFiles('shard.yml') }}
restore-keys: ${{ runner.os }}-shards-
- name: Download source
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install shards
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
key: ${{ runner.os }}-shards-${{ hashFiles('shard.yml') }}
restore-keys: ${{ runner.os }}-shards-
- name: Download source
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
- name: Install shards
Expand Down
53 changes: 48 additions & 5 deletions src/procodile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Procodile
end
end

# ORIGINAL_ARGV = ARGV.join(" ")
ORIGINAL_ARGV = ARGV.join(" ")
command = ARGV[0]? || "help"
options = {} of Symbol => String
cli = Procodile::CLI.new
Expand Down Expand Up @@ -51,21 +51,64 @@ end
# Get the global configuration file data
global_config_path = ENV["PROCODILE_CONFIG"]? || "/etc/procodile"

if File.file?(global_config_path)
global_config = Procodile::Config::Option.from_yaml(File.read(global_config_path))
end
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 || Procodile::Config::Option.new
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

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
end
end
end
end

begin
if command != "help"
cli.config = Procodile::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

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

cli.dispatch(command)
Expand Down
46 changes: 34 additions & 12 deletions src/procodile/app_determination.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Procodile
@given_root : String?

@root : String?
@procfile : String? = nil
@procfile : String?
getter root, procfile

# Start by creating an determination ased on the root and procfile that has been provided
Expand All @@ -16,7 +16,7 @@ module Procodile
@pwd : String,
given_root : String?,
@given_procfile : String?,
@global_options : Config::Option = Config::Option.new
@global_options : Array(Config::GlobalOption)
)
@given_root = given_root ? expand_path(given_root, pwd) : nil

Expand All @@ -28,6 +28,28 @@ module Procodile
!@root
end

# Choose which of the ambiguous options we want to choose
def set_app_id_and_find_root_and_procfile(id : Int32) : Nil
@app_id = id

find_root_and_procfile_from_options(@global_options)
end

# Return an hash of possible options to settle the ambiguity
def app_options : Hash(Int32, String)
if ambiguous?
hash = {} of Int32 => String

@global_options.each_with_index do |option, i|
hash[i] = option.name || option.root
end

hash
else
{} of Int32 => String
end
end

private def calculate : Nil
# Try and find something using the information that has been given to us by the user
find_root_and_procfile(@pwd, @given_root, @given_procfile)
Expand All @@ -36,7 +58,7 @@ module Procodile
find_root_and_procfile_from_options(@global_options) if ambiguous?
end

private def find_root_and_procfile(pwd : String, given_root : String?, given_procfile : String?) : String?
private def find_root_and_procfile(pwd : String, given_root : String?, given_procfile : String?) : Nil
case
when given_root && given_procfile
# The user has provided both the root and procfile, we can use these
Expand All @@ -61,8 +83,6 @@ module Procodile
@in_app_directory = true
end
end

@root
end

private def expand_path(path : String, root : String? = nil) : String
Expand All @@ -79,18 +99,20 @@ module Procodile
end
end

private def find_root_and_procfile_from_options(options : Config::Option) : String?
private def find_root_and_procfile_from_options(options : Config::GlobalOption | Array(Config::GlobalOption)) : Nil
case options
when Config::Option
when Config::GlobalOption
# Use the current hash
find_root_and_procfile(@pwd, options.root, options.procfile)
when Array(Config::Option)
when Array(Config::GlobalOption)
# Global options is provides a list of apps. We need to know which one of
# these we should be looking at.

raise Error.new "Still not support pass array yet."
# FIXME: Still not work
# find_root_and_procfile_from_options(options.app_id)
if (app_id = @app_id)
find_root_and_procfile_from_options(options[app_id])
else
STDERR.puts "Need specify app_id!"
exit 1
end
end
end
end
Expand Down
20 changes: 12 additions & 8 deletions src/procodile/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Procodile

@process_list : Hash(String, String)?
@processes : Hash(String, Procodile::Process)?
@procfile_path : String?
@options : Option?
@local_options : Option?
@process_options : Hash(String, Process::Option)?
Expand All @@ -24,9 +23,7 @@ module Procodile

getter root, loaded_at

def initialize(@root : String, procfile : String? = nil)
@procfile_path = procfile

def initialize(@root : String, @procfile : String? = nil)
unless File.file?(procfile_path)
raise Error.new("Procfile not found at #{procfile_path}")
end
Expand All @@ -52,16 +49,15 @@ module Procodile
end

def reload : Nil
@process_list = nil

@options = nil
@local_options = nil

@process_options = nil
@local_process_options = nil

@loaded_at = nil
@process_list = nil
@environment_variables = nil
@loaded_at = nil

if (processes = @processes)
process_list.each do |name, command|
Expand Down Expand Up @@ -180,7 +176,7 @@ module Procodile
end

def procfile_path : String
@procfile_path || File.join(self.root, "Procfile")
@procfile || File.join(self.root, "Procfile")
end

def options_path : String
Expand Down Expand Up @@ -236,5 +232,13 @@ module Procodile
def initialize
end
end

struct GlobalOption
include YAML::Serializable

property name : String?
property root : String
property procfile : String?
end
end
end

0 comments on commit cdba486

Please sign in to comment.