|
1 | 1 | defmodule LivebookCLI do |
2 | | - def usage() do |
3 | | - """ |
4 | | - Usage: livebook [command] [options] |
| 2 | + alias LivebookCLI.{Task, Utils} |
5 | 3 |
|
6 | | - Available commands: |
| 4 | + @switches [ |
| 5 | + help: :boolean, |
| 6 | + version: :boolean |
| 7 | + ] |
7 | 8 |
|
8 | | - livebook server Starts the Livebook web application |
9 | | -
|
10 | | - The --help and --version options can be given instead of a command for usage and versioning information. |
11 | | - """ |
12 | | - end |
| 9 | + @aliases [ |
| 10 | + h: :help, |
| 11 | + v: :version |
| 12 | + ] |
13 | 13 |
|
14 | 14 | def main(args) do |
15 | | - {:ok, _} = Application.ensure_all_started(:elixir) |
16 | | - |
17 | | - extract_priv!() |
18 | | - |
19 | | - :ok = Application.load(:livebook) |
20 | | - |
21 | | - if unix?() do |
22 | | - Application.put_env(:elixir, :ansi_enabled, true) |
23 | | - end |
24 | | - |
25 | | - call(args) |
26 | | - end |
27 | | - |
28 | | - defp unix?(), do: match?({:unix, _}, :os.type()) |
29 | | - |
30 | | - defp call([arg]) when arg in ["--help", "-h"], do: display_help() |
31 | | - defp call([arg]) when arg in ["--version", "-v"], do: display_version() |
32 | | - |
33 | | - defp call([task_name | args]) do |
34 | | - case find_task(task_name) do |
35 | | - nil -> |
36 | | - IO.ANSI.format([:red, "Unknown command #{task_name}\n"]) |> IO.puts() |
37 | | - IO.write(usage()) |
38 | | - |
39 | | - task -> |
40 | | - call_task(task, args) |
41 | | - end |
42 | | - end |
43 | | - |
44 | | - defp call(_args), do: IO.write(usage()) |
45 | | - |
46 | | - defp find_task("server"), do: LivebookCLI.Server |
47 | | - defp find_task(_), do: nil |
48 | | - |
49 | | - defp call_task(task, [arg]) when arg in ["--help", "-h"] do |
50 | | - IO.write(task.usage()) |
51 | | - end |
52 | | - |
53 | | - defp call_task(task, args) do |
54 | | - try do |
55 | | - task.call(args) |
56 | | - rescue |
57 | | - error in OptionParser.ParseError -> |
58 | | - IO.ANSI.format([ |
59 | | - :red, |
60 | | - Exception.message(error), |
61 | | - "\n\nFor more information try --help" |
62 | | - ]) |
63 | | - |> IO.puts() |
64 | | - |
65 | | - error -> |
66 | | - IO.ANSI.format([:red, Exception.format(:error, error, __STACKTRACE__), "\n"]) |> IO.puts() |
| 15 | + Utils.setup() |
| 16 | + |
| 17 | + case Utils.option_parse(args, strict: @switches, aliases: @aliases) do |
| 18 | + {parsed, [], _} when parsed.help -> display_help() |
| 19 | + {parsed, [name], _} when parsed.help -> Task.usage(name) |
| 20 | + {parsed, _, _} when parsed.version -> display_version() |
| 21 | + # We want to keep the switches for the task |
| 22 | + {_, [name | _], _} -> Task.call(name, List.delete(args, name)) |
67 | 23 | end |
68 | 24 | end |
69 | 25 |
|
70 | 26 | defp display_help() do |
71 | | - IO.puts("Livebook is an interactive notebook system for Elixir\n") |
72 | | - IO.write(usage()) |
73 | | - end |
74 | | - |
75 | | - defp display_version() do |
76 | | - IO.puts(:erlang.system_info(:system_version)) |
77 | | - IO.puts("Elixir " <> System.build_info()[:build]) |
78 | | - |
79 | | - version = Livebook.Config.app_version() |
80 | | - IO.puts("\nLivebook #{version}") |
81 | | - end |
82 | | - |
83 | | - import Record |
84 | | - defrecord(:zip_file, extract(:zip_file, from_lib: "stdlib/include/zip.hrl")) |
85 | | - |
86 | | - defp extract_priv!() do |
87 | | - archive_dir = Path.join(Livebook.Config.tmp_path(), "escript") |
88 | | - extracted_path = Path.join(archive_dir, "extracted") |
89 | | - in_archive_priv_path = ~c"livebook/priv" |
| 27 | + Utils.print_text(""" |
| 28 | + Livebook is an interactive notebook system for Elixir |
90 | 29 |
|
91 | | - # In dev we want to extract fresh directory on every boot |
92 | | - if Livebook.Config.app_version() =~ "-dev" do |
93 | | - File.rm_rf!(archive_dir) |
94 | | - end |
95 | | - |
96 | | - # When temporary directory is cleaned by the OS, the directories |
97 | | - # may be left in place, so we use a regular file (extracted) to |
98 | | - # check if the extracted archive is already available |
99 | | - if not File.exists?(extracted_path) do |
100 | | - {:ok, sections} = :escript.extract(:escript.script_name(), []) |
101 | | - archive = Keyword.fetch!(sections, :archive) |
102 | | - |
103 | | - file_filter = fn zip_file(name: name) -> |
104 | | - List.starts_with?(name, in_archive_priv_path) |
105 | | - end |
106 | | - |
107 | | - case :zip.extract(archive, cwd: String.to_charlist(archive_dir), file_filter: file_filter) do |
108 | | - {:ok, _} -> |
109 | | - :ok |
| 30 | + Usage: livebook [command] [options] |
110 | 31 |
|
111 | | - {:error, error} -> |
112 | | - print_error_and_exit( |
113 | | - "Livebook failed to extract archive files, reason: #{inspect(error)}" |
114 | | - ) |
115 | | - end |
| 32 | + Available commands: |
116 | 33 |
|
117 | | - File.touch!(extracted_path) |
118 | | - end |
| 34 | + livebook server Starts the Livebook web application |
119 | 35 |
|
120 | | - priv_dir = Path.join(archive_dir, in_archive_priv_path) |
121 | | - Application.put_env(:livebook, :priv_dir, priv_dir, persistent: true) |
| 36 | + The --help and --version options can be given instead of a command for usage and versioning information.\ |
| 37 | + """) |
122 | 38 | end |
123 | 39 |
|
124 | | - @spec print_error_and_exit(String.t()) :: no_return() |
125 | | - defp print_error_and_exit(message) do |
126 | | - IO.ANSI.format([:red, message]) |> IO.puts() |
127 | | - System.halt(1) |
| 40 | + defp display_version() do |
| 41 | + Utils.print_text(""" |
| 42 | + #{:erlang.system_info(:system_version)} |
| 43 | + Elixir #{System.build_info()[:build]} |
| 44 | + Livebook #{Livebook.Config.app_version()}\ |
| 45 | + """) |
128 | 46 | end |
129 | 47 | end |
0 commit comments