From 6ef338a016c4238595e5fd2346688f14954c5736 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Sun, 25 Dec 2022 12:27:23 -0600 Subject: [PATCH 1/9] feat(exec): changes exec command to run inside of container --- .tool-versions | 1 + Gemfile.lock | 1 + lib/neptuno/cli.rb | 2 +- lib/neptuno/cli/execute.rb | 17 ++++++++--------- 4 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..a2e7782 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.0.2 diff --git a/Gemfile.lock b/Gemfile.lock index 5501748..55ffc43 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -97,6 +97,7 @@ GEM zeitwerk (2.6.7) PLATFORMS + arm64-darwin-21 x86_64-darwin-21 x86_64-darwin-22 x86_64-linux diff --git a/lib/neptuno/cli.rb b/lib/neptuno/cli.rb index 0b1663b..f39449b 100644 --- a/lib/neptuno/cli.rb +++ b/lib/neptuno/cli.rb @@ -14,7 +14,7 @@ module CLI register "ls", List, aliases: ["ps"] register "activate", Activate, aliases: ["a"] register "config", Configure, aliases: %w[configure conf cc] - register "execute", Execute, aliases: ["e"] + register "execute", Execute, aliases: ["e", "exec"] register "build", ::Neptuno::Docker::Build, aliases: ["b"] register "up", Docker::Up, aliases: ["u"] register "down", Docker::Down, aliases: ["d"] diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index f757908..7062904 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -10,15 +10,14 @@ class Execute < Neptuno::CLI::Base desc "Execute service script" def call(**options) - command_service_to("execute", service_as_args: options[:args]&.first) do |service, _project| - commands = Dir.glob("#{neptuno_path}/scripts/#{service}/*").map { |x| x.split("/") }.map(&:last) - command = options[:args].last if commands.include?(options[:args]&.last) - puts "#{neptuno_path}/scripts/#{service}/*" - puts service - puts commands.to_s - puts Dir.glob("#{neptuno_path}/scripts/#{service}/*").to_s - command ||= prompt.select("execute", commands || []) - `cd #{neptuno_path}/scripts/#{service} && ./#{command}` + puts options[:args].to_s + # Some terrible non rubyist code to get the command and the service to execute + service_to ||= options[:args].length() > 1 ? options[:args].first : nil; + command ||= options[:args].length() > 1 ? options[:args].last : options[:args].first; # is there a way to get rest of array instead of using .last? + + # if options[:args] has more than 1 element, assume first arg is the container name and the next is the command + command_service_to('execute', service_as_args: service_to) do |service, _project| + system("cd #{neptuno_path} && docker compose exec #{service} #{command}") end end end From 3b851a12f5cdbc850d5eb31af5b8a7473add61a4 Mon Sep 17 00:00:00 2001 From: Joseph Groves <63882142+josephgroves@users.noreply.github.com> Date: Sun, 25 Dec 2022 12:37:56 -0600 Subject: [PATCH 2/9] Update lib/neptuno/cli/execute.rb --- lib/neptuno/cli/execute.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index 7062904..c825cfa 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -10,7 +10,6 @@ class Execute < Neptuno::CLI::Base desc "Execute service script" def call(**options) - puts options[:args].to_s # Some terrible non rubyist code to get the command and the service to execute service_to ||= options[:args].length() > 1 ? options[:args].first : nil; command ||= options[:args].length() > 1 ? options[:args].last : options[:args].first; # is there a way to get rest of array instead of using .last? From 329754c5fd46a12c4aad3a803f84b4874ed38f51 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Tue, 27 Dec 2022 12:01:32 -0600 Subject: [PATCH 3/9] refactor(exec): slight clean up --- lib/neptuno/cli/execute.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index c825cfa..f61278a 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -10,12 +10,9 @@ class Execute < Neptuno::CLI::Base desc "Execute service script" def call(**options) - # Some terrible non rubyist code to get the command and the service to execute - service_to ||= options[:args].length() > 1 ? options[:args].first : nil; - command ||= options[:args].length() > 1 ? options[:args].last : options[:args].first; # is there a way to get rest of array instead of using .last? + service_to, *command = options[:args] - # if options[:args] has more than 1 element, assume first arg is the container name and the next is the command - command_service_to('execute', service_as_args: service_to) do |service, _project| + command_service_to('execute', service_as_args: command.empty? ? service_to : nil) do |service, _project| system("cd #{neptuno_path} && docker compose exec #{service} #{command}") end end From 86468d62f7f523269483e6a9b79bc57743beb9d1 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Tue, 14 Mar 2023 12:32:51 -0600 Subject: [PATCH 4/9] fix: simplify exec implementation --- lib/neptuno/cli/execute.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index f61278a..4389d27 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -6,14 +6,14 @@ module CLI class Execute < Neptuno::CLI::Base include TTY::File include TTY::Config + + desc "Execute command inside of container" - desc "Execute service script" - - def call(**options) - service_to, *command = options[:args] - - command_service_to('execute', service_as_args: command.empty? ? service_to : nil) do |service, _project| - system("cd #{neptuno_path} && docker compose exec #{service} #{command}") + def call(services: [],**options) + command_service_to('execute', service_as_args: services) do |service, _project| + puts "Executing #{options[:args][-1]} inside of #{service} container" + system("cd #{neptuno_path} && docker compose exec #{service} ash -c \"#{options[:args][-1]}\"") + # TODO: Add support for referencing procs as executable commands with exec end end end From f89b0d68e75be73bd2cd2ba5fe4f549165900e89 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Tue, 14 Mar 2023 12:54:39 -0600 Subject: [PATCH 5/9] fix: use default shell env to execute commands --- lib/neptuno/cli/execute.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index 4389d27..b8341b5 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -12,7 +12,7 @@ class Execute < Neptuno::CLI::Base def call(services: [],**options) command_service_to('execute', service_as_args: services) do |service, _project| puts "Executing #{options[:args][-1]} inside of #{service} container" - system("cd #{neptuno_path} && docker compose exec #{service} ash -c \"#{options[:args][-1]}\"") + system("cd #{neptuno_path} && docker compose exec #{service} $0 -c \"#{options[:args][-1]}\"") # TODO: Add support for referencing procs as executable commands with exec end end From a3ba32b1df127680f7cf3d7a7543b13dff9485d3 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Wed, 3 May 2023 11:07:04 -0600 Subject: [PATCH 6/9] feat(exec): exec function can now use commands defined in procfiles --- lib/neptuno/cli/execute.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index b8341b5..9f2cfe2 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -11,9 +11,23 @@ class Execute < Neptuno::CLI::Base def call(services: [],**options) command_service_to('execute', service_as_args: services) do |service, _project| - puts "Executing #{options[:args][-1]} inside of #{service} container" - system("cd #{neptuno_path} && docker compose exec #{service} $0 -c \"#{options[:args][-1]}\"") + command = options[:args][-1] # TODO: Add support for referencing procs as executable commands with exec + # Creates a hash of processes from Procfile + procfile = File.read("#{neptuno_path}/procfiles/#{service}/Procfile") + procHash = {} + procfile.each_line do |line| + if line =~ /^([A-Za-z0-9_]+):\s*(.*)$/ + procHash[$1] = $2 + end + end + if procHash.has_key?(command) + puts "Executing #{command} from procfile inside of #{service} container" + system("cd #{neptuno_path} && #{procHash[command]}") + else + puts "Executing #{command} inside of #{service} container" + system("cd #{neptuno_path} && docker compose exec #{service} $0 -c \"#{command}\"") + end end end end From 52f4e4a5ac7d71f2432b5b63dddc2f639e7d2bd8 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Wed, 3 May 2023 11:42:36 -0600 Subject: [PATCH 7/9] refactor(exec): clean up exec function --- lib/neptuno/cli/execute.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index 9f2cfe2..5108e10 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -12,17 +12,13 @@ class Execute < Neptuno::CLI::Base def call(services: [],**options) command_service_to('execute', service_as_args: services) do |service, _project| command = options[:args][-1] - # TODO: Add support for referencing procs as executable commands with exec # Creates a hash of processes from Procfile - procfile = File.read("#{neptuno_path}/procfiles/#{service}/Procfile") - procHash = {} - procfile.each_line do |line| - if line =~ /^([A-Za-z0-9_]+):\s*(.*)$/ - procHash[$1] = $2 - end + procHash = File.foreach("#{neptuno_path}/procfiles/#{service}/Procfile").with_object({}) do |line, hash| + name, command = line.strip.split(':', 2) + hash[name] = command end if procHash.has_key?(command) - puts "Executing #{command} from procfile inside of #{service} container" + puts "Found #{command} in procfile, executing #{command}" system("cd #{neptuno_path} && #{procHash[command]}") else puts "Executing #{command} inside of #{service} container" From 7a12041f80bb3ec278dd0d89abf17f19e61fd994 Mon Sep 17 00:00:00 2001 From: Joseph Groves Date: Wed, 3 May 2023 12:36:56 -0600 Subject: [PATCH 8/9] fix(exec): fix clashing scope vars --- lib/neptuno/cli/execute.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/neptuno/cli/execute.rb b/lib/neptuno/cli/execute.rb index 5108e10..bf1e263 100644 --- a/lib/neptuno/cli/execute.rb +++ b/lib/neptuno/cli/execute.rb @@ -14,8 +14,8 @@ def call(services: [],**options) command = options[:args][-1] # Creates a hash of processes from Procfile procHash = File.foreach("#{neptuno_path}/procfiles/#{service}/Procfile").with_object({}) do |line, hash| - name, command = line.strip.split(':', 2) - hash[name] = command + name, process = line.strip.split(':', 2) + hash[name] = process end if procHash.has_key?(command) puts "Found #{command} in procfile, executing #{command}" From ab8628f2c9d8325bb134ca82ac80bacb7271c1c8 Mon Sep 17 00:00:00 2001 From: Joseph Groves <63882142+josephgroves@users.noreply.github.com> Date: Wed, 3 May 2023 16:28:58 -0600 Subject: [PATCH 9/9] Delete .tool-versions --- .tool-versions | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index a2e7782..0000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -ruby 3.0.2