From cce8d411382b5840f543f7393c9196baece3a92b Mon Sep 17 00:00:00 2001 From: Steffen Deusch Date: Wed, 29 Jun 2022 18:50:16 +0200 Subject: [PATCH] only rely on name in options --- lib/nerves_ssh.ex | 20 ++++++++------------ lib/nerves_ssh/application.ex | 2 +- lib/nerves_ssh/options.ex | 2 +- test/nerves_ssh_test.exs | 14 ++++++-------- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/nerves_ssh.ex b/lib/nerves_ssh.ex index 164af25..fb986e7 100644 --- a/lib/nerves_ssh.ex +++ b/lib/nerves_ssh.ex @@ -29,20 +29,16 @@ defmodule NervesSSH do defp via_name(name), do: {:via, Registry, {NervesSSH.Registry, name}} @doc false - @spec start_link(Options.t() | {atom(), Options.t()}) :: GenServer.on_start() - def start_link({name, %Options{} = opts}) do - GenServer.start_link(__MODULE__, %{opts | name: name}, name: via_name(name)) - end - + @spec start_link(Options.t()) :: GenServer.on_start() def start_link(%Options{} = opts) do - GenServer.start_link(__MODULE__, %{opts | name: :default}, name: via_name(:default)) + GenServer.start_link(__MODULE__, opts, name: via_name(opts.name)) end @doc """ Read the configuration options """ @spec configuration :: Options.t() - def configuration(name \\ :default) do + def configuration(name \\ NervesSSH) do GenServer.call(via_name(name), :configuration) end @@ -52,7 +48,7 @@ defmodule NervesSSH do See [ssh.daemon_info/1](http://erlang.org/doc/man/ssh.html#daemon_info-1). """ @spec info() :: {:ok, keyword()} | {:error, :bad_daemon_ref} - def info(name \\ :default) do + def info(name \\ NervesSSH) do GenServer.call(via_name(name), :info) end @@ -62,7 +58,7 @@ defmodule NervesSSH do This will also attempt to save the key in `{USER_DIR}/authorized_keys` """ @spec add_authorized_key(String.t()) :: :ok - def add_authorized_key(name \\ :default, key) when is_binary(key) do + def add_authorized_key(name \\ NervesSSH, key) when is_binary(key) do GenServer.call(via_name(name), {:add_authorized_key, key}) end @@ -72,7 +68,7 @@ defmodule NervesSSH do This will also attempt to remove the key in `{USER_DIR}/authorized_keys` """ @spec remove_authorized_key(String.t()) :: :ok - def remove_authorized_key(name \\ :default, key) when is_binary(key) do + def remove_authorized_key(name \\ NervesSSH, key) when is_binary(key) do GenServer.call(via_name(name), {:remove_authorized_key, key}) end @@ -83,7 +79,7 @@ defmodule NervesSSH do authentication for this user """ @spec add_user(String.t(), String.t() | nil) :: :ok - def add_user(name \\ :default, user, password) do + def add_user(name \\ NervesSSH, user, password) do GenServer.call(via_name(name), {:add_user, [user, password]}) end @@ -91,7 +87,7 @@ defmodule NervesSSH do Remove a user credential from the SSH daemon """ @spec remove_user(String.t()) :: :ok - def remove_user(name \\ :default, user) do + def remove_user(name \\ NervesSSH, user) do GenServer.call(via_name(name), {:remove_user, [user]}) end diff --git a/lib/nerves_ssh/application.ex b/lib/nerves_ssh/application.ex index 3de6a2f..1dcf04f 100644 --- a/lib/nerves_ssh/application.ex +++ b/lib/nerves_ssh/application.ex @@ -30,7 +30,7 @@ defmodule NervesSSH.Application do [] app_env -> - [{NervesSSH, {:default, Options.with_defaults(app_env)}}] + [{NervesSSH, Options.with_defaults(app_env)}] end opts = [strategy: :one_for_one, name: NervesSSH.Supervisor] diff --git a/lib/nerves_ssh/options.ex b/lib/nerves_ssh/options.ex index eb31b88..5c662a2 100644 --- a/lib/nerves_ssh/options.ex +++ b/lib/nerves_ssh/options.ex @@ -41,7 +41,7 @@ defmodule NervesSSH.Options do daemon_option_overrides: keyword() } - defstruct name: :default, + defstruct name: NervesSSH, authorized_keys: [], decoded_authorized_keys: [], user_passwords: [], diff --git a/test/nerves_ssh_test.exs b/test/nerves_ssh_test.exs index 5d63de3..eb9bd05 100644 --- a/test/nerves_ssh_test.exs +++ b/test/nerves_ssh_test.exs @@ -77,7 +77,7 @@ defmodule NervesSSHTest do start_supervised!({NervesSSH, nerves_ssh_config()}) # Test we can send SSH command - state = :sys.get_state({:via, Registry, {NervesSSH.Registry, :default}}) + state = :sys.get_state({:via, Registry, {NervesSSH.Registry, NervesSSH}}) assert {:ok, "2", 0} == ssh_run("1 + 1") # Simulate sshd failure. restart @@ -85,7 +85,7 @@ defmodule NervesSSHTest do Process.sleep(800) # Test recovery - new_state = :sys.get_state({:via, Registry, {NervesSSH.Registry, :default}}) + new_state = :sys.get_state({:via, Registry, {NervesSSH.Registry, NervesSSH}}) assert state.sshd != new_state.sshd assert {:ok, "4", 0} == ssh_run("2 + 2") @@ -263,14 +263,12 @@ defmodule NervesSSHTest do @tag :has_good_sshd_exec test "can start multiple named daemons" do - config = nerves_ssh_config() - other_config = Map.update!(config, :port, &(&1 + 1)) + config = nerves_ssh_config() |> Map.put(:name, :daemon_a) + other_config = %{config | name: :daemon_b, port: config.port + 1} # start two servers, starting with identical configs, except the port - start_supervised!(Supervisor.child_spec({NervesSSH, {:daemon_a, config}}, id: :daemon_a)) + start_supervised!(Supervisor.child_spec({NervesSSH, config}, id: :daemon_a)) - start_supervised!( - Supervisor.child_spec({NervesSSH, {:daemon_b, other_config}}, id: :daemon_b) - ) + start_supervised!(Supervisor.child_spec({NervesSSH, other_config}, id: :daemon_b)) assert {:ok, "2", 0} == ssh_run("1 + 1", @key_login)