Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kamal app open command to open app in a web browser #1070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
jeromedalbert marked this conversation as resolved.
Show resolved Hide resolved
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