From c662b8d578275f0796e5bfa05c5bcea94472ddca Mon Sep 17 00:00:00 2001 From: Ali Ismayilov <993934+aliismayilov@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:57:34 +0200 Subject: [PATCH] Make --detach incompatible with reuse or interactive --- lib/kamal/cli/app.rb | 4 ++++ test/cli/app_test.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index 0217dbd1..3f813014 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -96,6 +96,10 @@ class Kamal::Cli::App < Kamal::Cli::Base option :env, aliases: "-e", type: :hash, desc: "Set environment variables for the command" option :detach, type: :boolean, default: false, desc: "Execute command in a detached container" def exec(*cmd) + if (incompatible_options = [ :interactive, :reuse ].select { |key| options[:detach] && options[key] }.presence) + raise ArgumentError, "Detach is not compatible with #{incompatible_options.join(" or ")}" + end + cmd = Kamal::Utils.join_commands(cmd) env = options[:env] detach = options[:detach] diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 63eade9b..c5255c4a 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -279,6 +279,24 @@ class CliAppTest < CliTestCase end end + test "exec detach with reuse" do + assert_raises(ArgumentError, "Detach is not compatible with reuse") do + run_command("exec", "--detach", "--reuse", "ruby -v") + end + end + + test "exec detach with interactive" do + assert_raises(ArgumentError, "Detach is not compatible with interactive") do + run_command("exec", "--interactive", "--detach", "ruby -v") + end + end + + test "exec detach with interactive and reuse" do + assert_raises(ArgumentError, "Detach is not compatible with interactive or reuse") do + run_command("exec", "--interactive", "--detach", "--reuse", "ruby -v") + end + end + test "exec with reuse" do run_command("exec", "--reuse", "ruby -v").tap do |output| assert_match "sh -c 'docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting --filter ancestor=$(docker image ls --filter reference=dhh/app:latest --format '\\''{{.ID}}'\\'') ; docker ps --latest --format '\\''{{.Names}}'\\'' --filter label=service=app --filter label=destination= --filter label=role=web --filter status=running --filter status=restarting' | head -1 | while read line; do echo ${line#app-web-}; done", output # Get current version