mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-09-07 05:24:40 +08:00
Apply review comments
This commit is contained in:
parent
081d84867d
commit
eab2d1f2c5
5 changed files with 41 additions and 44 deletions
|
@ -31,14 +31,14 @@ defmodule LivebookCLI do
|
||||||
[arg] when arg in @version_args -> display_version()
|
[arg] when arg in @version_args -> display_version()
|
||||||
[name, arg] when arg in @help_args -> Task.usage(name)
|
[name, arg] when arg in @help_args -> Task.usage(name)
|
||||||
[name | args] -> Task.call(name, List.delete(args, name))
|
[name | args] -> Task.call(name, List.delete(args, name))
|
||||||
_args -> Utils.print_text(usage())
|
_args -> Utils.log_info(usage())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp unix?(), do: match?({:unix, _}, :os.type())
|
defp unix?(), do: match?({:unix, _}, :os.type())
|
||||||
|
|
||||||
defp display_help() do
|
defp display_help() do
|
||||||
Utils.print_text("""
|
Utils.log_info("""
|
||||||
Livebook is an interactive notebook system for Elixir
|
Livebook is an interactive notebook system for Elixir
|
||||||
|
|
||||||
#{usage()}\
|
#{usage()}\
|
||||||
|
@ -46,7 +46,7 @@ defmodule LivebookCLI do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp display_version() do
|
defp display_version() do
|
||||||
Utils.print_text("""
|
Utils.log_info("""
|
||||||
#{:erlang.system_info(:system_version)}
|
#{:erlang.system_info(:system_version)}
|
||||||
Elixir #{System.build_info()[:build]}
|
Elixir #{System.build_info()[:build]}
|
||||||
|
|
||||||
|
|
|
@ -50,10 +50,10 @@ defmodule LivebookCLI.Deploy do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp config_from_args(args) do
|
defp config_from_args(args) do
|
||||||
{opts, path} = OptionParser.parse!(args, strict: @switches)
|
{opts, paths} = OptionParser.parse!(args, strict: @switches)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
path: List.flatten(path),
|
paths: paths,
|
||||||
session_token: opts[:deploy_key],
|
session_token: opts[:deploy_key],
|
||||||
teams_key: opts[:teams_key],
|
teams_key: opts[:teams_key],
|
||||||
deployment_group: opts[:deployment_group]
|
deployment_group: opts[:deployment_group]
|
||||||
|
@ -82,8 +82,8 @@ defmodule LivebookCLI.Deploy do
|
||||||
acc
|
acc
|
||||||
end
|
end
|
||||||
|
|
||||||
{:path, value}, acc ->
|
{:paths, values}, acc ->
|
||||||
Enum.reduce_while(value, acc, &validate_path/2)
|
Enum.reduce_while(values, acc, &validate_path/2)
|
||||||
|
|
||||||
_otherwise, acc ->
|
_otherwise, acc ->
|
||||||
acc
|
acc
|
||||||
|
@ -103,10 +103,10 @@ defmodule LivebookCLI.Deploy do
|
||||||
defp validate_path(value, acc) do
|
defp validate_path(value, acc) do
|
||||||
cond do
|
cond do
|
||||||
not File.exists?(value) ->
|
not File.exists?(value) ->
|
||||||
{:halt, add_error(acc, normalize_key(:path), "must be a valid path")}
|
{:halt, add_error(acc, normalize_key(:paths), "must be a valid path")}
|
||||||
|
|
||||||
File.dir?(value) ->
|
File.dir?(value) ->
|
||||||
{:halt, add_error(acc, normalize_key(:path), "must be a file path")}
|
{:halt, add_error(acc, normalize_key(:paths), "must be a file path")}
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
{:cont, acc}
|
{:cont, acc}
|
||||||
|
@ -124,15 +124,15 @@ defmodule LivebookCLI.Deploy do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp deploy_to_teams(team, config) do
|
defp deploy_to_teams(team, config) do
|
||||||
if length(config.path) == 1 do
|
if length(config.paths) == 1 do
|
||||||
log_debug("Found 1 notebook")
|
log_debug("Found 1 notebook")
|
||||||
else
|
else
|
||||||
log_debug("Found #{length(config.path)} notebooks")
|
log_debug("Found #{length(config.paths)} notebooks")
|
||||||
end
|
end
|
||||||
|
|
||||||
log_info("Deploying notebooks:")
|
log_info("Deploying notebooks:")
|
||||||
|
|
||||||
for path <- config.path do
|
for path <- config.paths do
|
||||||
log_info(" * Preparing to deploy notebook #{Path.basename(path)}")
|
log_info(" * Preparing to deploy notebook #{Path.basename(path)}")
|
||||||
files_dir = Livebook.FileSystem.File.local(path)
|
files_dir = Livebook.FileSystem.File.local(path)
|
||||||
|
|
||||||
|
@ -140,10 +140,10 @@ defmodule LivebookCLI.Deploy do
|
||||||
{:ok, app_deployment} <- prepare_app_deployment(path, content, files_dir) do
|
{:ok, app_deployment} <- prepare_app_deployment(path, content, files_dir) do
|
||||||
case Livebook.Teams.deploy_app_from_cli(team, app_deployment, config.deployment_group) do
|
case Livebook.Teams.deploy_app_from_cli(team, app_deployment, config.deployment_group) do
|
||||||
{:ok, url} ->
|
{:ok, url} ->
|
||||||
print_text([:green, " * #{app_deployment.title} deployed successfully. (#{url})"])
|
log_info([:green, " * #{app_deployment.title} deployed successfully. (#{url})"])
|
||||||
|
|
||||||
{:error, errors} ->
|
{:error, errors} ->
|
||||||
print_text([:red, " * #{app_deployment.title} failed to deploy."])
|
log_error(" * #{app_deployment.title} failed to deploy.")
|
||||||
errors = normalize_errors(errors)
|
errors = normalize_errors(errors)
|
||||||
|
|
||||||
raise LivebookCLI.Error, """
|
raise LivebookCLI.Error, """
|
||||||
|
@ -153,7 +153,7 @@ defmodule LivebookCLI.Deploy do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
{:transport_error, reason} ->
|
{:transport_error, reason} ->
|
||||||
print_text([:red, " * #{app_deployment.title} failed to deploy."])
|
log_error(" * #{app_deployment.title} failed to deploy.")
|
||||||
raise LivebookCLI.Error, reason
|
raise LivebookCLI.Error, reason
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -192,7 +192,7 @@ defmodule LivebookCLI.Deploy do
|
||||||
defp normalize_key("session_token"), do: "Deploy Key"
|
defp normalize_key("session_token"), do: "Deploy Key"
|
||||||
defp normalize_key("teams_key"), do: "Teams Key"
|
defp normalize_key("teams_key"), do: "Teams Key"
|
||||||
defp normalize_key("deployment_group"), do: "Deployment Group"
|
defp normalize_key("deployment_group"), do: "Deployment Group"
|
||||||
defp normalize_key("path"), do: "Path"
|
defp normalize_key("paths"), do: "File Paths"
|
||||||
|
|
||||||
defp format_errors(errors, prefix) do
|
defp format_errors(errors, prefix) do
|
||||||
errors
|
errors
|
||||||
|
|
|
@ -28,7 +28,7 @@ defmodule LivebookCLI.Task do
|
||||||
@spec usage(String.t()) :: :ok
|
@spec usage(String.t()) :: :ok
|
||||||
def usage(name) do
|
def usage(name) do
|
||||||
task = fetch_task!(name)
|
task = fetch_task!(name)
|
||||||
print_text(task.usage())
|
log_info(task.usage())
|
||||||
rescue
|
rescue
|
||||||
exception -> log_exception(exception, name, __STACKTRACE__)
|
exception -> log_exception(exception, name, __STACKTRACE__)
|
||||||
end
|
end
|
||||||
|
@ -38,17 +38,11 @@ defmodule LivebookCLI.Task do
|
||||||
|
|
||||||
defp fetch_task!(name) do
|
defp fetch_task!(name) do
|
||||||
log_error("Unknown command #{name}")
|
log_error("Unknown command #{name}")
|
||||||
print_text(LivebookCLI.usage())
|
log_info(LivebookCLI.usage())
|
||||||
|
|
||||||
System.halt(1)
|
System.halt(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp log_error(message) do
|
|
||||||
[:red, message]
|
|
||||||
|> IO.ANSI.format()
|
|
||||||
|> IO.puts()
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec log_exception(Exception.t(), String.t(), Exception.stacktrace()) :: no_return()
|
@spec log_exception(Exception.t(), String.t(), Exception.stacktrace()) :: no_return()
|
||||||
defp log_exception(exception, command_name, stacktrace) when is_exception(exception) do
|
defp log_exception(exception, command_name, stacktrace) when is_exception(exception) do
|
||||||
[:red, format_exception(exception, command_name, stacktrace)]
|
[:red, format_exception(exception, command_name, stacktrace)]
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
defmodule LivebookCLI.Utils do
|
defmodule LivebookCLI.Utils do
|
||||||
def option_parse(argv, opts \\ []) do
|
|
||||||
{parsed, argv, errors} = OptionParser.parse(argv, opts)
|
|
||||||
{Enum.into(parsed, %{}), argv, errors}
|
|
||||||
end
|
|
||||||
|
|
||||||
def log_info(message) do
|
def log_info(message) do
|
||||||
IO.puts(message)
|
message
|
||||||
|
|> IO.ANSI.format()
|
||||||
|
|> IO.puts()
|
||||||
end
|
end
|
||||||
|
|
||||||
if Mix.env() == :dev do
|
if Mix.env() == :dev do
|
||||||
|
@ -26,8 +23,8 @@ defmodule LivebookCLI.Utils do
|
||||||
|> IO.puts()
|
|> IO.puts()
|
||||||
end
|
end
|
||||||
|
|
||||||
def print_text(message) do
|
def log_error(message) do
|
||||||
message
|
[:red, message]
|
||||||
|> IO.ANSI.format()
|
|> IO.ANSI.format()
|
||||||
|> IO.puts()
|
|> IO.puts()
|
||||||
end
|
end
|
||||||
|
|
|
@ -224,7 +224,7 @@ defmodule LivebookCLI.Integration.DeployTest do
|
||||||
{key, _} = TeamsRPC.create_deploy_key(node, org: org)
|
{key, _} = TeamsRPC.create_deploy_key(node, org: org)
|
||||||
deployment_group = TeamsRPC.create_deployment_group(node, org: org, url: @url)
|
deployment_group = TeamsRPC.create_deployment_group(node, org: org, url: @url)
|
||||||
|
|
||||||
assert_raise LivebookCLI.Error, ~r/Path must be a valid path/s, fn ->
|
assert_raise LivebookCLI.Error, ~r/File Paths must be a valid path/s, fn ->
|
||||||
deploy(
|
deploy(
|
||||||
key,
|
key,
|
||||||
team.teams_key,
|
team.teams_key,
|
||||||
|
@ -238,7 +238,7 @@ defmodule LivebookCLI.Integration.DeployTest do
|
||||||
{key, _} = TeamsRPC.create_deploy_key(node, org: org)
|
{key, _} = TeamsRPC.create_deploy_key(node, org: org)
|
||||||
deployment_group = TeamsRPC.create_deployment_group(node, org: org, url: @url)
|
deployment_group = TeamsRPC.create_deployment_group(node, org: org, url: @url)
|
||||||
|
|
||||||
assert_raise LivebookCLI.Error, ~r/Path must be a file path/s, fn ->
|
assert_raise LivebookCLI.Error, ~r/File Paths must be a file path/s, fn ->
|
||||||
deploy(
|
deploy(
|
||||||
key,
|
key,
|
||||||
team.teams_key,
|
team.teams_key,
|
||||||
|
@ -250,16 +250,22 @@ defmodule LivebookCLI.Integration.DeployTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp deploy(deploy_key, teams_key, deployment_group_name, path) do
|
defp deploy(deploy_key, teams_key, deployment_group_name, path) do
|
||||||
path = if Path.wildcard(path) == [], do: path, else: Path.wildcard(path)
|
paths =
|
||||||
|
case Path.wildcard(path) do
|
||||||
|
[] -> [path]
|
||||||
|
[path] -> [path]
|
||||||
|
paths -> paths
|
||||||
|
end
|
||||||
|
|
||||||
LivebookCLI.Deploy.call([
|
LivebookCLI.Deploy.call(
|
||||||
"--deploy-key",
|
[
|
||||||
deploy_key,
|
"--deploy-key",
|
||||||
"--teams-key",
|
deploy_key,
|
||||||
teams_key,
|
"--teams-key",
|
||||||
"--deployment-group",
|
teams_key,
|
||||||
deployment_group_name,
|
"--deployment-group",
|
||||||
path
|
deployment_group_name
|
||||||
])
|
] ++ paths
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue