Skip to content

Commit

Permalink
Simplify loading of repos_key config
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmj committed Feb 2, 2023
1 parent 2f35e1e commit 0edaffc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/hex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ defmodule Hex.Config do
term
end

def read_repos(config, repo_key) do
hexpm = Hex.Repo.nostate_default_hexpm_repo(repo_key)
def read_repos(config) do
hexpm = Hex.Repo.default_hexpm_repo()

(config[:"$repos"] || %{})
|> Hex.Repo.merge_hexpm(hexpm)
Expand Down
19 changes: 11 additions & 8 deletions lib/hex/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ defmodule Hex.Repo do

case Map.fetch(repos, repo) do
{:ok, config} when repo == "hexpm" ->
hexpm = default_hexpm_repo()
{:ok, %{config | url: hexpm.url || config.url, trusted: hexpm.trusted}}
hexpm = hexpm_repo()
url = hexpm.url || config.url
auth_key = hexpm.auth_key || config.auth_key
{:ok, %{config | url: url, trusted: hexpm.trusted, auth_key: auth_key}}

{:ok, config} ->
{:ok, config}
Expand Down Expand Up @@ -56,23 +58,24 @@ defmodule Hex.Repo do
|> Map.put(:trusted, Map.has_key?(repo, :auth_key) or source.trusted)
end

def default_hexpm_repo() do
def hexpm_repo() do
trusted_mirror_url = Hex.State.fetch!(:trusted_mirror_url)
mirror_url = Hex.State.fetch!(:mirror_url)
auth_key = Hex.State.fetch!(:repos_key)

%{
url: trusted_mirror_url || mirror_url,
public_key: @hexpm_public_key,
auth_key: nil,
auth_key: auth_key,
trusted: trusted_mirror_url != nil or mirror_url == nil
}
end

def nostate_default_hexpm_repo(auth_key) do
def default_hexpm_repo() do
%{
url: @hexpm_url,
public_key: @hexpm_public_key,
auth_key: auth_key,
auth_key: nil,
trusted: true
}
end
Expand Down Expand Up @@ -102,7 +105,7 @@ defmodule Hex.Repo do
)
end

def merge_hexpm(repos, hexpm \\ default_hexpm_repo()) do
def merge_hexpm(repos, hexpm \\ hexpm_repo()) do
Map.update(repos, "hexpm", hexpm, &Map.merge(hexpm, &1))
end

Expand Down Expand Up @@ -144,7 +147,7 @@ defmodule Hex.Repo do
end

def clean_hexpm(repos) do
hexpm = default_hexpm_repo()
hexpm = hexpm_repo()
repo = Map.get(repos, "hexpm", hexpm)
repo = clean_repo(repo, hexpm)

Expand Down
5 changes: 1 addition & 4 deletions lib/hex/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,11 @@ defmodule Hex.State do
{key, load_config_value(global_config, project_config, spec)}
end)

{_source, repos_key} = Map.fetch!(state, :repos_key)

Map.merge(state, %{
clean_pass: {:computed, true},
httpc_profile: {:computed, :hex},
pbkdf2_iters: {:computed, @pbkdf2_iters},
repos: {:computed, Hex.Config.read_repos(global_config, repos_key)},
repos_key: {:computed, repos_key},
repos: {:computed, Hex.Config.read_repos(global_config)},
ssl_version: {:computed, ssl_version()},
shell_process: {:computed, nil}
})
Expand Down
49 changes: 36 additions & 13 deletions test/hex/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Hex.RepoTest do
test "get public key" do
bypass = Bypass.open()
repos = Hex.State.fetch!(:repos)
hexpm = Hex.Repo.default_hexpm_repo()
hexpm = Hex.Repo.hexpm_repo()
repos = put_in(repos["hexpm"].url, "http://localhost:#{bypass.port}")
Hex.State.put(:repos, repos)

Expand All @@ -73,9 +73,15 @@ defmodule Hex.RepoTest do
end

test "fetch_repo/1" do
assert Hex.Repo.fetch_repo("foo") == :error

assert {:ok,
%{auth_key: nil, public_key: _, trusted: true, url: "http://localhost:4043/repo"}} =
Hex.Repo.fetch_repo("hexpm")
%{
auth_key: nil,
public_key: _,
trusted: true,
url: "http://localhost:4043/repo"
}} = Hex.Repo.fetch_repo("hexpm")

assert {:ok,
%{
Expand All @@ -85,25 +91,42 @@ defmodule Hex.RepoTest do
url: "http://localhost:4043/repo/repos/acme"
}} = Hex.Repo.fetch_repo("hexpm:acme")

assert Hex.Repo.fetch_repo("foo") == :error

Hex.State.put(:trusted_mirror_url, "http://example.com")
Hex.State.put(:repos_key, "key")

assert {:ok, %{auth_key: nil, public_key: _, trusted: true, url: "http://example.com"}} =
Hex.Repo.fetch_repo("hexpm")
assert {:ok,
%{
auth_key: "key",
public_key: _,
trusted: true,
url: "http://example.com"
}} = Hex.Repo.fetch_repo("hexpm")

assert {:ok,
%{auth_key: nil, public_key: _, trusted: true, url: "http://example.com/repos/acme"}} =
Hex.Repo.fetch_repo("hexpm:acme")
%{
auth_key: "key",
public_key: _,
trusted: true,
url: "http://example.com/repos/acme"
}} = Hex.Repo.fetch_repo("hexpm:acme")

Hex.State.put(:trusted_mirror_url, nil)
Hex.State.put(:mirror_url, "http://example.com")

assert {:ok, %{auth_key: nil, public_key: _, trusted: false, url: "http://example.com"}} =
Hex.Repo.fetch_repo("hexpm")
assert {:ok,
%{
auth_key: "key",
public_key: _,
trusted: false,
url: "http://example.com"
}} = Hex.Repo.fetch_repo("hexpm")

assert {:ok,
%{auth_key: nil, public_key: _, trusted: false, url: "http://example.com/repos/acme"}} =
Hex.Repo.fetch_repo("hexpm:acme")
%{
auth_key: "key",
public_key: _,
trusted: false,
url: "http://example.com/repos/acme"
}} = Hex.Repo.fetch_repo("hexpm:acme")
end
end

0 comments on commit 0edaffc

Please sign in to comment.