renamed app, added escript functionality

This commit is contained in:
ewood 2022-05-28 09:02:24 -06:00
parent aadb08fa67
commit cfa0fd7dd0
6 changed files with 64 additions and 27 deletions

View File

@ -1,20 +1,20 @@
# Smolex
# Smol
An elixir TUI client for Gemini/Spartan and the smolnet in general.
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `smolex` to your list of dependencies in `mix.exs`:
by adding `smol` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:smolex, "~> 0.1.0"}
{:smol, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/smolex>.
be found at <https://hexdocs.pm/Smol>.

View File

@ -1,14 +1,44 @@
defmodule Smolex do
defmodule Smol do
use GenServer
require Logger
@crlf "\r\n"
@schemes ["gemini", "spartan"]
def main(args \\ [])
def main(nil) do
IO.puts("At least one argument is required")
end
def main(args) do
args
|> parse_args()
|> response()
receive do
{:all_done_boss} -> System.halt(0)
end
end
def parse_args(args) do
args
|> OptionParser.parse(strict: [request: :string])
end
def response({opts, word, invalid} = args) do
if opts[:request], do: request(opts[:request])
end
#defp error_response(opt) do
# IO.puts("No option #{opt}")
#end
def request(url) do
uri = URI.parse(url)
IO.puts(uri)
if Enum.any?(@schemes, fn u -> u === uri.scheme end) do
process = Smol.start_link("")
#IO.inspect(process)
GenServer.call(__MODULE__, {:connect, url}, :infinity)
else
IO.puts("Invalid url")
@ -37,8 +67,13 @@ defmodule Smolex do
@impl true
def handle_info({:ssl_closed, _sslsocket}, state) do
{:stop, :normal, state}
end
@impl true
def terminate(reason, state) do
IO.puts(state)
{:noreply, state}
:normal
end
def set_opts do
@ -48,15 +83,12 @@ defmodule Smolex do
def connect(url) do
uri = URI.parse(url)
:ssl.start()
IO.puts(uri.port)
port = 1965
{:ok, sslsocket} = :ssl.connect(String.to_charlist(uri.host), port, set_opts())
crlf_url = url <> @crlf
status = :ssl.send(sslsocket, String.to_charlist(crlf_url))
unless status === :ok do
IO.puts("Error sending request")
end

View File

@ -1,4 +1,4 @@
defmodule Smolex.Application do
defmodule Smol.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
@ -8,14 +8,14 @@ defmodule Smolex.Application do
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: Smolex.Worker.start_link(arg)
# {Smolex.Worker, arg}
#{Smolex, ""}
# Starts a worker by calling: Smol.Worker.start_link(arg)
# {Smol.Worker, arg}
#{Smol, ""}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_all, name: Smolex.Supervisor]
opts = [strategy: :one_for_all, name: Smol.Supervisor]
Supervisor.start_link(children, opts)
end
end

13
mix.exs
View File

@ -1,13 +1,14 @@
defmodule Smolex.MixProject do
defmodule Smol.MixProject do
use Mix.Project
def project do
[
app: :smolex,
app: :smol,
version: "0.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps()
deps: deps(),
escript: escript()
]
end
@ -15,7 +16,7 @@ defmodule Smolex.MixProject do
def application do
[
extra_applications: [:logger, :ssl],
mod: {Smolex.Application, []}
mod: {Smol.Application, []}
]
end
@ -26,4 +27,8 @@ defmodule Smolex.MixProject do
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
defp escript do
[main_module: Smol]
end
end

8
test/smol_test.exs Normal file
View File

@ -0,0 +1,8 @@
defmodule SmolTest do
use ExUnit.Case
doctest Smol
test "greets the world" do
assert Smol.hello() == :world
end
end

View File

@ -1,8 +0,0 @@
defmodule SmolexTest do
use ExUnit.Case
doctest Smolex
test "greets the world" do
assert Smolex.hello() == :world
end
end