From 65156291568d52c5ced7b15f353f6c4aca77dd3d Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 1 Jul 2021 12:17:49 +0200 Subject: [PATCH] livebook server: Add --open flag (#417) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * livebook server: Add --open flag * Update lib/livebook_cli/server.ex Co-authored-by: Jonatan Kłosko * Add LivebookWeb.Endpoint.access_url/0 Co-authored-by: Jonatan Kłosko --- lib/livebook/application.ex | 13 +------------ lib/livebook_cli/server.ex | 24 +++++++++++++++++++++--- lib/livebook_web/endpoint.ex | 11 +++++++++++ 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/livebook/application.ex b/lib/livebook/application.ex index deebe0bfa..924c73320 100644 --- a/lib/livebook/application.ex +++ b/lib/livebook/application.ex @@ -117,18 +117,7 @@ defmodule Livebook.Application do defp display_startup_info() do if Phoenix.Endpoint.server?(:livebook, LivebookWeb.Endpoint) do - IO.puts("[Livebook] Application running at #{access_url()}") - end - end - - defp access_url() do - root_url = LivebookWeb.Endpoint.url() - - if Livebook.Config.auth_mode() == :token do - token = Application.fetch_env!(:livebook, :token) - root_url <> "/?token=" <> token - else - root_url + IO.puts("[Livebook] Application running at #{LivebookWeb.Endpoint.access_url()}") end end end diff --git a/lib/livebook_cli/server.ex b/lib/livebook_cli/server.ex index ec5aac0ab..dda208e5d 100644 --- a/lib/livebook_cli/server.ex +++ b/lib/livebook_cli/server.ex @@ -32,6 +32,7 @@ defmodule LivebookCLI.Server do --name Set a name for the app distributed node --no-token Disable token authentication, enabled by default If LIVEBOOK_PASSWORD is set, it takes precedence over token auth + --open Open browser window pointing to the application -p, --port The port to start the web application on, defaults to 8080 --root-path The root path to use for file selection --sname Set a short name for the app distributed node @@ -47,11 +48,16 @@ defmodule LivebookCLI.Server do @impl true def call(args) do - config_entries = args_to_config(args) + opts = args_to_options(args) + config_entries = opts_to_config(opts, []) put_config_entries(config_entries) case start_server() do :ok -> + if opts[:open] do + browser_open(LivebookWeb.Endpoint.access_url()) + end + Process.sleep(:infinity) :error -> @@ -85,6 +91,7 @@ defmodule LivebookCLI.Server do default_runtime: :string, ip: :string, name: :string, + open: :boolean, port: :integer, root_path: :string, sname: :string, @@ -95,10 +102,10 @@ defmodule LivebookCLI.Server do p: :port ] - defp args_to_config(args) do + defp args_to_options(args) do {opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases) validate_options!(opts) - opts_to_config(opts, []) + opts end defp validate_options!(opts) do @@ -152,4 +159,15 @@ defmodule LivebookCLI.Server do end defp opts_to_config([_opt | opts], config), do: opts_to_config(opts, config) + + defp browser_open(url) do + {cmd, args} = + case :os.type() do + {:win32, _} -> {"cmd", ["/c", "start", url]} + {:unix, :darwin} -> {"open", [url]} + {:unix, _} -> {"xdg-open", [url]} + end + + System.cmd(cmd, args) + end end diff --git a/lib/livebook_web/endpoint.ex b/lib/livebook_web/endpoint.ex index 9e8d4d4e6..7c3e0d8b1 100644 --- a/lib/livebook_web/endpoint.ex +++ b/lib/livebook_web/endpoint.ex @@ -72,4 +72,15 @@ defmodule LivebookWeb.Endpoint do plug Plug.Head plug Plug.Session, @session_options plug LivebookWeb.Router + + def access_url() do + root_url = url() + + if Livebook.Config.auth_mode() == :token do + token = Application.fetch_env!(:livebook, :token) + root_url <> "/?token=" <> token + else + root_url + end + end end