mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-08 04:27:47 +08:00
Add url arg to the CLI (#947)
This commit is contained in:
parent
a667a51509
commit
420284006a
3 changed files with 46 additions and 21 deletions
|
@ -84,10 +84,7 @@ if Mix.target() == :app do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp import_livebook(url) do
|
defp import_livebook(url) do
|
||||||
LivebookWeb.Endpoint.access_struct_url()
|
LivebookWeb.Helpers.notebook_import_url(url)
|
||||||
|> Map.replace!(:path, "/import")
|
|
||||||
|> append_query("url=#{URI.encode_www_form(url)}")
|
|
||||||
|> URI.to_string()
|
|
||||||
|> Livebook.Utils.browser_open()
|
|> Livebook.Utils.browser_open()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -111,14 +108,5 @@ if Mix.target() == :app do
|
||||||
defp macos?() do
|
defp macos?() do
|
||||||
:os.type() == {:unix, :darwin}
|
:os.type() == {:unix, :darwin}
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: On Elixir v1.14, use URI.append_query/2
|
|
||||||
defp append_query(%URI{query: query} = uri, query_to_add) when query in [nil, ""] do
|
|
||||||
%{uri | query: query_to_add}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp append_query(%URI{} = uri, query) do
|
|
||||||
%{uri | query: uri.query <> "&" <> query}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,9 +15,14 @@ defmodule LivebookCLI.Server do
|
||||||
@impl true
|
@impl true
|
||||||
def usage() do
|
def usage() do
|
||||||
"""
|
"""
|
||||||
Usage: livebook server [options]
|
Usage: livebook server [url] [options]
|
||||||
|
|
||||||
Available options:
|
An optional url can be given as argument. If one is given,
|
||||||
|
a browser window will open importing the given url as a notebook:
|
||||||
|
|
||||||
|
livebook server https://example.com/my-notebook.livemd
|
||||||
|
|
||||||
|
## Available options
|
||||||
|
|
||||||
--autosave-path The directory where notebooks with no file are persisted.
|
--autosave-path The directory where notebooks with no file are persisted.
|
||||||
Defaults to livebook/notebooks/ under the default user cache
|
Defaults to livebook/notebooks/ under the default user cache
|
||||||
|
@ -52,7 +57,7 @@ defmodule LivebookCLI.Server do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def call(args) do
|
def call(args) do
|
||||||
opts = args_to_options(args)
|
{opts, extra_args} = args_to_options(args)
|
||||||
config_entries = opts_to_config(opts, [])
|
config_entries = opts_to_config(opts, [])
|
||||||
put_config_entries(config_entries)
|
put_config_entries(config_entries)
|
||||||
|
|
||||||
|
@ -62,7 +67,7 @@ defmodule LivebookCLI.Server do
|
||||||
case check_endpoint_availability(base_url) do
|
case check_endpoint_availability(base_url) do
|
||||||
:livebook_running ->
|
:livebook_running ->
|
||||||
IO.puts("Livebook already running on #{base_url}")
|
IO.puts("Livebook already running on #{base_url}")
|
||||||
open_from_options(base_url, opts)
|
open_from_options(base_url, opts, extra_args)
|
||||||
|
|
||||||
:taken ->
|
:taken ->
|
||||||
print_error(
|
print_error(
|
||||||
|
@ -75,7 +80,7 @@ defmodule LivebookCLI.Server do
|
||||||
# so it's gonna start listening
|
# so it's gonna start listening
|
||||||
case Application.ensure_all_started(:livebook) do
|
case Application.ensure_all_started(:livebook) do
|
||||||
{:ok, _} ->
|
{:ok, _} ->
|
||||||
open_from_options(LivebookWeb.Endpoint.access_url(), opts)
|
open_from_options(LivebookWeb.Endpoint.access_url(), opts, extra_args)
|
||||||
Process.sleep(:infinity)
|
Process.sleep(:infinity)
|
||||||
|
|
||||||
{:error, error} ->
|
{:error, error} ->
|
||||||
|
@ -116,7 +121,7 @@ defmodule LivebookCLI.Server do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp open_from_options(base_url, opts) do
|
defp open_from_options(base_url, opts, []) do
|
||||||
if opts[:open] do
|
if opts[:open] do
|
||||||
Livebook.Utils.browser_open(base_url)
|
Livebook.Utils.browser_open(base_url)
|
||||||
end
|
end
|
||||||
|
@ -128,6 +133,18 @@ defmodule LivebookCLI.Server do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp open_from_options(base_url, _opts, [url]) do
|
||||||
|
base_url
|
||||||
|
|> LivebookWeb.Helpers.notebook_import_url(url)
|
||||||
|
|> Livebook.Utils.browser_open()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp open_from_options(_base_url, _opts, _extra_args) do
|
||||||
|
print_error(
|
||||||
|
"Too many arguments entered. Ensure only one argument is used to specify the file path and all other arguments are preceded by the relevant switch"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
@switches [
|
@switches [
|
||||||
autosave_path: :string,
|
autosave_path: :string,
|
||||||
cookie: :string,
|
cookie: :string,
|
||||||
|
@ -147,9 +164,9 @@ defmodule LivebookCLI.Server do
|
||||||
]
|
]
|
||||||
|
|
||||||
defp args_to_options(args) do
|
defp args_to_options(args) do
|
||||||
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
|
{opts, extra_args} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
|
||||||
validate_options!(opts)
|
validate_options!(opts)
|
||||||
opts
|
{opts, extra_args}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_options!(opts) do
|
defp validate_options!(opts) do
|
||||||
|
|
|
@ -376,6 +376,26 @@ defmodule LivebookWeb.Helpers do
|
||||||
def file_system_label(%FileSystem.Local{}), do: "Local disk"
|
def file_system_label(%FileSystem.Local{}), do: "Local disk"
|
||||||
def file_system_label(%FileSystem.S3{} = fs), do: fs.bucket_url
|
def file_system_label(%FileSystem.S3{} = fs), do: fs.bucket_url
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns a URL (including localhost) to import the given `url` as a notebook.
|
||||||
|
"""
|
||||||
|
def notebook_import_url(base_url \\ LivebookWeb.Endpoint.access_struct_url(), url) do
|
||||||
|
base_url
|
||||||
|
|> URI.parse()
|
||||||
|
|> Map.replace!(:path, "/import")
|
||||||
|
|> append_query("url=#{URI.encode_www_form(url)}")
|
||||||
|
|> URI.to_string()
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: On Elixir v1.14, use URI.append_query/2
|
||||||
|
defp append_query(%URI{query: query} = uri, query_to_add) when query in [nil, ""] do
|
||||||
|
%{uri | query: query_to_add}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp append_query(%URI{} = uri, query) do
|
||||||
|
%{uri | query: uri.query <> "&" <> query}
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns the text in singular or plural depending on the quantity
|
Returns the text in singular or plural depending on the quantity
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue