Apply review comments

This commit is contained in:
Alexandre de Souza 2025-07-24 17:24:04 -03:00
parent 081d84867d
commit eab2d1f2c5
No known key found for this signature in database
GPG key ID: E39228FFBA346545
5 changed files with 41 additions and 44 deletions

View file

@ -31,14 +31,14 @@ defmodule LivebookCLI do
[arg] when arg in @version_args -> display_version()
[name, arg] when arg in @help_args -> Task.usage(name)
[name | args] -> Task.call(name, List.delete(args, name))
_args -> Utils.print_text(usage())
_args -> Utils.log_info(usage())
end
end
defp unix?(), do: match?({:unix, _}, :os.type())
defp display_help() do
Utils.print_text("""
Utils.log_info("""
Livebook is an interactive notebook system for Elixir
#{usage()}\
@ -46,7 +46,7 @@ defmodule LivebookCLI do
end
defp display_version() do
Utils.print_text("""
Utils.log_info("""
#{:erlang.system_info(:system_version)}
Elixir #{System.build_info()[:build]}

View file

@ -50,10 +50,10 @@ defmodule LivebookCLI.Deploy do
end
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],
teams_key: opts[:teams_key],
deployment_group: opts[:deployment_group]
@ -82,8 +82,8 @@ defmodule LivebookCLI.Deploy do
acc
end
{:path, value}, acc ->
Enum.reduce_while(value, acc, &validate_path/2)
{:paths, values}, acc ->
Enum.reduce_while(values, acc, &validate_path/2)
_otherwise, acc ->
acc
@ -103,10 +103,10 @@ defmodule LivebookCLI.Deploy do
defp validate_path(value, acc) do
cond do
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) ->
{: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 ->
{:cont, acc}
@ -124,15 +124,15 @@ defmodule LivebookCLI.Deploy do
end
defp deploy_to_teams(team, config) do
if length(config.path) == 1 do
if length(config.paths) == 1 do
log_debug("Found 1 notebook")
else
log_debug("Found #{length(config.path)} notebooks")
log_debug("Found #{length(config.paths)} notebooks")
end
log_info("Deploying notebooks:")
for path <- config.path do
for path <- config.paths do
log_info(" * Preparing to deploy notebook #{Path.basename(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
case Livebook.Teams.deploy_app_from_cli(team, app_deployment, config.deployment_group) do
{:ok, url} ->
print_text([:green, " * #{app_deployment.title} deployed successfully. (#{url})"])
log_info([:green, " * #{app_deployment.title} deployed successfully. (#{url})"])
{:error, errors} ->
print_text([:red, " * #{app_deployment.title} failed to deploy."])
log_error(" * #{app_deployment.title} failed to deploy.")
errors = normalize_errors(errors)
raise LivebookCLI.Error, """
@ -153,7 +153,7 @@ defmodule LivebookCLI.Deploy do
"""
{:transport_error, reason} ->
print_text([:red, " * #{app_deployment.title} failed to deploy."])
log_error(" * #{app_deployment.title} failed to deploy.")
raise LivebookCLI.Error, reason
end
end
@ -192,7 +192,7 @@ defmodule LivebookCLI.Deploy do
defp normalize_key("session_token"), do: "Deploy Key"
defp normalize_key("teams_key"), do: "Teams Key"
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
errors

View file

@ -28,7 +28,7 @@ defmodule LivebookCLI.Task do
@spec usage(String.t()) :: :ok
def usage(name) do
task = fetch_task!(name)
print_text(task.usage())
log_info(task.usage())
rescue
exception -> log_exception(exception, name, __STACKTRACE__)
end
@ -38,17 +38,11 @@ defmodule LivebookCLI.Task do
defp fetch_task!(name) do
log_error("Unknown command #{name}")
print_text(LivebookCLI.usage())
log_info(LivebookCLI.usage())
System.halt(1)
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()
defp log_exception(exception, command_name, stacktrace) when is_exception(exception) do
[:red, format_exception(exception, command_name, stacktrace)]

View file

@ -1,11 +1,8 @@
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
IO.puts(message)
message
|> IO.ANSI.format()
|> IO.puts()
end
if Mix.env() == :dev do
@ -26,8 +23,8 @@ defmodule LivebookCLI.Utils do
|> IO.puts()
end
def print_text(message) do
message
def log_error(message) do
[:red, message]
|> IO.ANSI.format()
|> IO.puts()
end

View file

@ -224,7 +224,7 @@ defmodule LivebookCLI.Integration.DeployTest do
{key, _} = TeamsRPC.create_deploy_key(node, org: org)
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(
key,
team.teams_key,
@ -238,7 +238,7 @@ defmodule LivebookCLI.Integration.DeployTest do
{key, _} = TeamsRPC.create_deploy_key(node, org: org)
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(
key,
team.teams_key,
@ -250,16 +250,22 @@ defmodule LivebookCLI.Integration.DeployTest do
end
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([
"--deploy-key",
deploy_key,
"--teams-key",
teams_key,
"--deployment-group",
deployment_group_name,
path
])
LivebookCLI.Deploy.call(
[
"--deploy-key",
deploy_key,
"--teams-key",
teams_key,
"--deployment-group",
deployment_group_name
] ++ paths
)
end
end