Skip to content

Commit ccc07af

Browse files
committed
Minor improvements to Server task
1 parent a99b687 commit ccc07af

1 file changed

Lines changed: 11 additions & 26 deletions

File tree

lib/livebook_cli/server.ex

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule LivebookCLI.Server do
2+
import LivebookCLI.Utils
23
@behaviour LivebookCLI.Task
34

45
@external_resource "README.md"
@@ -63,8 +64,7 @@ defmodule LivebookCLI.Server do
6364
6465
Starts a server and imports the notebook at the given URL:
6566
66-
livebook server https://example.com/my-notebook.livemd
67-
67+
livebook server https://example.com/my-notebook.livemd\
6868
"""
6969
end
7070

@@ -84,14 +84,12 @@ defmodule LivebookCLI.Server do
8484

8585
case check_endpoint_availability(base_url) do
8686
:livebook_running ->
87-
IO.puts("Livebook already running on #{base_url}")
87+
log_info("Livebook already running on #{base_url}")
8888
open_from_args(base_url, extra_args)
8989

9090
:taken ->
91-
print_error(
92-
"Another application is already running on port #{port}." <>
93-
" Either ensure this port is free or specify a different port using the --port option"
94-
)
91+
raise "Another application is already running on port #{port}." <>
92+
" Either ensure this port is free or specify a different port using the --port option"
9593

9694
:available ->
9795
start_server(extra_args)
@@ -108,7 +106,7 @@ defmodule LivebookCLI.Server do
108106
Process.sleep(:infinity)
109107

110108
{:error, error} ->
111-
print_error("Livebook failed to start with reason: #{inspect(error)}")
109+
raise "Livebook failed to start with reason: #{inspect(error)}"
112110
end
113111
end
114112

@@ -126,20 +124,13 @@ defmodule LivebookCLI.Server do
126124

127125
defp check_endpoint_availability(base_url) do
128126
Application.ensure_all_started(:req)
129-
130127
health_url = set_path(base_url, "/public/health")
131-
132128
req = Req.new() |> Livebook.Utils.req_attach_defaults()
133129

134130
case Req.get(req, url: health_url, retry: false) do
135-
{:ok, %{status: 200, body: %{"application" => "livebook"}}} ->
136-
:livebook_running
137-
138-
{:ok, _other} ->
139-
:taken
140-
141-
{:error, _exception} ->
142-
:available
131+
{:ok, %{status: 200, body: %{"application" => "livebook"}}} -> :livebook_running
132+
{:ok, _other} -> :taken
133+
{:error, _exception} -> :available
143134
end
144135
end
145136

@@ -158,7 +149,7 @@ defmodule LivebookCLI.Server do
158149
end
159150

160151
defp open_from_args(base_url, ["new"]) do
161-
IO.warn(~s/passing "new" as an argument is deprecated, use "@new" instead/, [])
152+
log_warning(~s/passing "new" as an argument is deprecated, use "@new" instead/)
162153
open_from_args(base_url, ["@new"])
163154
end
164155

@@ -183,9 +174,7 @@ defmodule LivebookCLI.Server do
183174
end
184175

185176
defp open_from_args(_base_url, _extra_args) do
186-
print_error(
187-
"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"
188-
)
177+
raise "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"
189178
end
190179

191180
@switches [
@@ -237,8 +226,4 @@ defmodule LivebookCLI.Server do
237226
|> Map.put(:path, path)
238227
|> URI.to_string()
239228
end
240-
241-
defp print_error(message) do
242-
IO.ANSI.format([:red, message]) |> IO.puts()
243-
end
244229
end

0 commit comments

Comments
 (0)