From e1451f84669b0c2e8e1a911856c3fc5dd2cd6fcc Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 25 Aug 2020 20:39:34 -0400 Subject: replace login/logout examples with mix tasks --- examples/login.exs | 5 --- examples/logout.exs | 6 --- lib/mix/tasks/polyjuice/login.ex | 85 +++++++++++++++++++++++++++++++++++++++ lib/mix/tasks/polyjuice/logout.ex | 84 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 11 deletions(-) delete mode 100644 examples/login.exs delete mode 100644 examples/logout.exs create mode 100644 lib/mix/tasks/polyjuice/login.ex create mode 100644 lib/mix/tasks/polyjuice/logout.ex diff --git a/examples/login.exs b/examples/login.exs deleted file mode 100644 index 71392ad..0000000 --- a/examples/login.exs +++ /dev/null @@ -1,5 +0,0 @@ -Polyjuice.Client.User.log_in_with_password( - "http://localhost:8008", - "user", - "password" -) |> inspect() |> IO.puts() diff --git a/examples/logout.exs b/examples/logout.exs deleted file mode 100644 index 812812a..0000000 --- a/examples/logout.exs +++ /dev/null @@ -1,6 +0,0 @@ -Polyjuice.Client.User.log_out( - %Polyjuice.Client{ - base_url: "http://localhost:8008", - access_token: "accesstoken" - } -) |> inspect() |> IO.puts() diff --git a/lib/mix/tasks/polyjuice/login.ex b/lib/mix/tasks/polyjuice/login.ex new file mode 100644 index 0000000..3ae1234 --- /dev/null +++ b/lib/mix/tasks/polyjuice/login.ex @@ -0,0 +1,85 @@ +# Copyright 2020 Hubert Chathi +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Mix.Tasks.Polyjuice.Login do + @moduledoc """ + Log in to a Matrix homeserver. + + mix polyjuice.login [opts] homeserver_url username password + + ## Command line options + + * `--storage` - Elixir code to create the storage to save the client state + in. If this option is not provided (or if the `--verbose` option is also + provided), the logged in user ID, device ID and access token will be + printed. + * `--verbose` - print the logged in user ID, device ID and access token even + if the `--storage` option is provided. + + """ + @shortdoc "Log in to a Matrix homeserver." + use Mix.Task + + @impl Mix.Task + def run(args) do + Mix.Task.run("app.start", []) + + with {opts, [url, user_id, password]} <- + OptionParser.parse!(args, + strict: [ + storage: :string, + verbose: :boolean + ] + ) do + storage = opts[:storage] + + storage = + if is_binary(storage) do + Code.eval_string(storage) |> (fn {x, _} -> x end).() + else + nil + end + + client = + Polyjuice.Client.start_link( + url, + storage: storage, + sync: false + ) + + ret = Polyjuice.Client.log_in_with_password(client, user_id, password) + + Polyjuice.Client.stop(client) + + if storage, do: Polyjuice.Client.Storage.close(storage) + + case ret do + {:ok, %{"user_id" => user_id, "device_id" => device_id, "access_token" => access_token}} -> + IO.puts("Login successful.") + + if opts[:verbose] || opts[:storage] == nil do + IO.puts("User ID: #{user_id}") + IO.puts("Device ID: #{device_id}") + IO.puts("Access token: #{access_token}") + end + + _ -> + IO.puts("Login failed: #{inspect(ret)}.") + end + else + _ -> + Mix.Task.run("help", ["polyjuice.login"]) + end + end +end diff --git a/lib/mix/tasks/polyjuice/logout.ex b/lib/mix/tasks/polyjuice/logout.ex new file mode 100644 index 0000000..9ac65a3 --- /dev/null +++ b/lib/mix/tasks/polyjuice/logout.ex @@ -0,0 +1,84 @@ +# Copyright 2020 Hubert Chathi +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +defmodule Mix.Tasks.Polyjuice.Logout do + @moduledoc """ + Log out of a Matrix homeserver. + + mix polyjuice.logout [opts] homeserver_url + + ## Command line options + + * `--storage` - Elixir code to create the storage to fetch the client state + from. + * `--access-token` - The access token to log out. + + At least one of `--storage` or `--access-token` must be provided. + + """ + @shortdoc "Log out of a Matrix homeserver." + use Mix.Task + + @impl Mix.Task + def run(args) do + Mix.Task.run("app.start", []) + + with {opts, [url]} <- + OptionParser.parse!(args, + strict: [ + storage: :string, + access_token: :string + ] + ) do + storage = opts[:storage] + access_token = opts[:access_token] + + if {storage, access_token} == {nil, nil} do + Mix.Task.run("help", ["polyjuice.logout"]) + else + storage = + if is_binary(storage) do + Code.eval_string(storage) |> (fn {x, _} -> x end).() + else + nil + end + + client = + Polyjuice.Client.start_link( + url, + access_token: opts[:access_token], + storage: storage, + sync: false + ) + + ret = Polyjuice.Client.log_out(client) + + Polyjuice.Client.stop(client) + + if storage, do: Polyjuice.Client.Storage.close(storage) + + case ret do + {:ok} -> + IO.puts("Logout successful.") + + _ -> + IO.puts("Logout failed: #{inspect(ret)}.") + end + end + else + _ -> + Mix.Task.run("help", ["polyjuice.logout"]) + end + end +end -- cgit v1.2.3