Skip to content

Commit

Permalink
Add kamal app open command
Browse files Browse the repository at this point in the history
To open the app in a web browser
  • Loading branch information
jeromedalbert committed Oct 7, 2024
1 parent e34031f commit 58490af
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/kamal/cli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/cli/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/deploy_with_proxy_host.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 58490af

Please sign in to comment.