Packages
github_issues
0.4.70
0.4.71
0.4.70
0.4.69
0.4.68
0.4.66
0.4.65
0.4.64
0.4.63
0.4.62
0.4.61
0.4.60
0.4.59
0.4.58
0.4.56
0.4.55
0.4.54
0.4.53
0.4.52
0.4.51
0.4.50
0.4.48
0.4.47
0.4.46
0.4.45
0.4.44
0.4.43
0.4.42
0.4.41
0.4.40
0.4.39
0.4.38
0.4.37
0.4.36
0.4.35
0.4.34
0.4.33
0.4.32
0.4.31
0.4.30
0.4.29
0.4.28
0.4.27
0.4.26
0.4.25
0.4.24
0.4.23
0.4.22
0.4.21
0.4.20
0.4.19
0.4.18
0.4.17
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.24
0.3.23
0.3.22
0.3.21
0.3.20
0.3.19
0.3.18
0.3.17
0.3.16
0.3.15
0.3.14
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.0
0.2.5
0.2.3
0.2.2
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.4
0.1.2
0.1.1
0.1.0
Writes GitHub Issues to "stdio" in a table with borders and colors.
Current section
Files
Jump to
Current section
Files
lib/github/issues/cli.ex
# ┌───────────────────────────────────────────────────────────┐
# │ Inspired by the book "Programming Elixir" by Dave Thomas. │
# └───────────────────────────────────────────────────────────┘
defmodule GitHub.Issues.CLI do
@moduledoc """
Parses the command line and prints a table of the first or last _n_ issues
of a GitHub project.
##### Inspired by the book [Programming Elixir](https://pragprog.com/titles/elixir16/programming-elixir-1-6) by Dave Thomas.
##### Reference https://dev.to/paulasantamaria/command-line-interfaces-structure-syntax-2533
"""
use PersistConfig
alias GitHub.Issues
alias GitHub.Issues.{Help, Log, Message}
alias IO.ANSI.Table
alias IO.ANSI.Table.Style
@default_count get_env(:default_count)
@default_switches get_env(:default_switches)
@parsing_options get_env(:parsing_options)
@table_spec get_env(:table_spec)
@typedoc "GitHub project"
@type project :: String.t()
@typedoc "GitHub user"
@type user :: String.t()
@doc """
Parses the command line and prints a table of the first or last _n_ issues
of a GitHub project.
`argv` can be "-h" or "--help", which prints info on the command's
usage and syntax. Otherwise it is a GitHub user, a GitHub project, and
optionally the number of issues to format (the first _n_ ones).
To format the last _n_ issues, specify switch `--last`.
To ring the bell, specify switch `--bell`.
To choose a table style, specify switch `--table-style`.
## Parameters
- `argv` - command line arguments (list)
## Switches
- `-h` or `--help` - for help
- `-b` or `--bell` - to ring the bell
- `-l` or `--last` - to format the last _n_ issues
- `-t` or `--table-style` - to choose a table style
## Table styles
#{Style.texts("\s\s- `&arg`&filler - ¬e\n")}
## Examples
alias GitHub.Issues.CLI
CLI.main(["Kraigie", "nostrum", "--last", "--no-help"])
CLI.main(["Kraigie", "nostrum", "--no-last"])
CLI.main(["Kraigie", "nostrum", "--no-bell", "--table-style", "plain"])
CLI.main(["Kraigie", "nostrum", "11", "--last"])
CLI.main(["Kraigie", "nostrum", "--last", "11"])
"""
@spec main(OptionParser.argv()) :: :ok
def main(argv) do
case OptionParser.parse(argv, @parsing_options) do
{switches, args, []} -> :ok = maybe_write_table(switches, args)
_invalid -> :ok = Help.print_help()
end
end
@doc """
Allows to run command `mix run -e 'GitHub.Issues.CLI.main()'`.
The above command is equivalent to:\s\s
`mix run -e 'GitHub.Issues.CLI.main([""Kraigie"", ""nostrum"", ""6""])'`
## Examples
$env:MIX_ENV="test"; mix run -e 'GitHub.Issues.CLI.main()'
$env:MIX_ENV="dev"; mix run -e 'GitHub.Issues.CLI.main()'
$env:MIX_ENV="prod"; mix run -e 'GitHub.Issues.CLI.main()'
"""
@spec main :: :ok
def main do
:ok = main(["Kraigie", "nostrum", "6"])
end
## Private functions
@spec maybe_write_table(Keyword.t(), OptionParser.argv()) :: :ok
defp maybe_write_table(switches, [user, project]) do
maybe_write_table(switches, [user, project, @default_count])
end
defp maybe_write_table(switches, [user, project, count]) do
with %{help: false, bell: bell?, last: last?, table_style: style} <-
Map.merge(@default_switches, Map.new(switches)),
{:ok, style} <- Style.from_switch_arg(style),
{count, ""} when count > 0 <- Integer.parse(count),
count = if(last?, do: -count, else: count),
options = [count: count, bell: bell?, style: style] do
:ok = write_table(user, project, options)
else
:error -> :ok = Help.print_help()
end
end
defp maybe_write_table(_switches, _args) do
:ok = Help.print_help()
end
@spec write_table(user, project, Keyword.t()) :: :ok
defp write_table(user, project, options) do
case Issues.fetch(user, project) do
{:ok, issues} ->
:ok = Log.info(:writing_table, {user, project, __ENV__})
:ok = Message.writing_table(user, project)
:ok = Table.write(@table_spec, issues, options)
{:error, text} ->
:ok = Log.error(:fetching_error, {user, project, text, __ENV__})
:ok = Message.fetching_error(user, project, text)
end
end
end