From 58490af135417f8125320f1e80ce05479f85f439 Mon Sep 17 00:00:00 2001 From: Jerome Dalbert Date: Sun, 6 Oct 2024 14:48:05 -0700 Subject: [PATCH] Add `kamal app open` command To open the app in a web browser --- lib/kamal/cli/app.rb | 18 ++++++++++++++++++ test/cli/app_test.rb | 14 ++++++++++++++ test/fixtures/deploy_with_proxy_host.yml | 13 +++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 test/fixtures/deploy_with_proxy_host.yml diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index 74b7b4df2..49269a738 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -227,6 +227,24 @@ def logs end end + desc "open", "open app in a web browser" + def open + host = KAMAL.primary_role.proxy.hosts.first || KAMAL.primary_host + protocol = KAMAL.primary_role.ssl? ? "https" : "http" + cmd = + case RbConfig::CONFIG["host_os"] + when /mswin|mingw|cygwin/ then "start" + when /darwin/ then "open" + when /linux|bsd/ then "xdg-open" + end + + if cmd + system(cmd, "#{protocol}://#{host}") + else + say "Could not detect OS browser" + end + end + desc "remove", "Remove app containers and images from servers" def remove with_lock do diff --git a/test/cli/app_test.rb b/test/cli/app_test.rb index 32b37456f..6769a64ec 100644 --- a/test/cli/app_test.rb +++ b/test/cli/app_test.rb @@ -235,6 +235,20 @@ class CliAppTest < CliTestCase end end + test "open with proxy host domain" do + RbConfig::CONFIG.stubs(:[]).with("host_os").returns("linux") + Object.any_instance.expects(:system).with("xdg-open", "https://app.example.com") + + run_command("open", config: :with_proxy_host) + end + + test "open with primary host IP" do + RbConfig::CONFIG.stubs(:[]).with("host_os").returns("linux") + Object.any_instance.expects(:system).with("xdg-open", "http://1.1.1.1") + + run_command("open") + end + test "remove" do run_command("remove").tap do |output| assert_match /#{Regexp.escape("sh -c 'docker ps --latest --quiet --filter label=service=app --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 --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting' | head -1 | xargs docker stop")}/, output diff --git a/test/fixtures/deploy_with_proxy_host.yml b/test/fixtures/deploy_with_proxy_host.yml new file mode 100644 index 000000000..692f63df5 --- /dev/null +++ b/test/fixtures/deploy_with_proxy_host.yml @@ -0,0 +1,13 @@ +service: app +image: dhh/app +servers: + web: + - "1.1.1.1" +proxy: + ssl: true + host: app.example.com +registry: + username: user + password: pw +builder: + arch: amd64