Skip to content

Commit

Permalink
Reduce nested code in some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
milmazz committed Dec 27, 2016
1 parent 1aa5d55 commit 80cc04e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
12 changes: 6 additions & 6 deletions lib/hex/crypto/key_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ defmodule Hex.Crypto.KeyManager do
case module.init(protected, opts) do
{:ok, params} ->
key_manager = %KeyManager{module: module, params: params}
case ContentEncryptor.init(protected, opts) do
{:ok, content_encryptor} ->
{:ok, key_manager, content_encryptor}
content_encryptor_error ->
content_encryptor_error
end
protected
|> ContentEncryptor.init(opts)
|> process_content_encryptor(key_manager)
key_manager_error ->
key_manager_error
end
Expand Down Expand Up @@ -74,4 +71,7 @@ defmodule Hex.Crypto.KeyManager do
defp key_manager_module("PBES2-HS384"), do: {:ok, Crypto.PBES2_HMAC_SHA2}
defp key_manager_module("PBES2-HS512"), do: {:ok, Crypto.PBES2_HMAC_SHA2}
defp key_manager_module(alg), do: {:error, "Unrecognized KeyManager algorithm: #{inspect alg}"}

defp process_content_encryptor({:ok, content}, key_manager), do: {:ok, key_manager, content}
defp process_content_encryptor(error, _), do: error
end
10 changes: 4 additions & 6 deletions lib/hex/crypto/pbes2_hmac_sha2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ defmodule Hex.Crypto.PBES2_HMAC_SHA2 do
{:ok, password} ->
case fetch_p2c(protected) do
{:ok, _iteration} ->
case fetch_p2s(protected) do
{:ok, _salt} ->
{:ok, %{hash: hash, password: password}}
error ->
error
end
protected |> fetch_p2s() |> process_fetch_p2s(hash, password)
error ->
error
end
Expand All @@ -58,6 +53,9 @@ defmodule Hex.Crypto.PBES2_HMAC_SHA2 do
end
def decrypt(_, _, _, _), do: :error

defp process_fetch_p2s({:ok, salt}, hash, passwd), do: {:ok, %{hash: hash, password: passwd}}
defp process_fetch_p2s(error, _, _), do: error

defp fetch_password(opts) do
case Keyword.fetch(opts, :password) do
{:ok, password} when is_binary(password) -> {:ok, password}
Expand Down
47 changes: 26 additions & 21 deletions lib/hex/remote_converger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@ defmodule Hex.RemoteConverger do
Hex.Shell.info "Dependency resolution completed:"
resolved = Enum.sort(resolved)
Enum.each(resolved, fn {name, version} ->
case Registry.retired(name, version) do
nil ->
Hex.Shell.info IO.ANSI.format [:green, " #{name} #{version}"]
retired ->
Hex.Shell.warn " #{name} #{version} RETIRED!"
Hex.Shell.warn " (#{retirement_reason(retired[:reason])}) #{retired[:message]}"
end
name |> Registry.retired(version) |> show_status(name, version)
end)
end
end

defp show_status(nil, name, version), do: Hex.Shell.info IO.ANSI.format [:green, " #{name} #{version}"]
defp show_status(retired, name, version) do
Hex.Shell.warn " #{name} #{version} RETIRED!"
Hex.Shell.warn " (#{retirement_reason(retired[:reason])}) #{retired[:message]}"
end

defp retirement_reason(:RETIRED_OTHER), do: "other"
defp retirement_reason(:RETIRED_INVALID), do: "invalid"
defp retirement_reason(:RETIRED_SECURITY), do: "security"
Expand All @@ -173,26 +173,31 @@ defmodule Hex.RemoteConverger do

case Hex.Utils.lock(lock[String.to_atom(app)]) do
[:hex, ^atom_name, ^version, checksum, _managers, deps] ->
registry_checksum = Registry.checksum(name, version)

if checksum && Base.decode16!(checksum, case: :lower) != registry_checksum,
do: Mix.raise "Registry checksum mismatch against lock (#{name} #{version})"

if deps do
deps =
Enum.map(deps, fn {app, req, opts} ->
{Atom.to_string(opts[:hex]), Atom.to_string(app), req, !!opts[:optional]}
end)

if Enum.sort(deps) != Enum.sort(Registry.deps(name, version)),
do: Mix.raise "Registry dependencies mismatch against lock (#{name} #{version})"
end
verify_registry(deps, name, version, checksum)
_ ->
:ok
end
end)
end

defp verify_registry(deps, name, version, checksum) do
registry_checksum = Registry.checksum(name, version)
if checksum && Base.decode16!(checksum, case: :lower) != registry_checksum,
do: Mix.raise "Registry checksum mismatch against lock (#{name} #{version})"

if deps, do: verify_dependencies(deps, name, version)
end

defp verify_dependencies(deps, name, version) do
deps =
Enum.map(deps, fn {app, req, opts} ->
{Atom.to_string(opts[:hex]), Atom.to_string(app), req, !!opts[:optional]}
end)

if Enum.sort(deps) != Enum.sort(Registry.deps(name, version)),
do: Mix.raise "Registry dependencies mismatch against lock (#{name} #{version})"
end

defp check_lock(lock) do
Enum.each(lock, fn {_app, info} ->
case Hex.Utils.lock(info) do
Expand Down
14 changes: 7 additions & 7 deletions lib/hex/resolver/backtracks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ defmodule Hex.Resolver.Backtracks do

case Map.fetch(map, name) do
{:ok, list} ->
case try_merge_package_version(list, version, parents) do
{:ok, list} ->
Map.put(map, name, list)
:error ->
Map.put(map, name, [{[version], parents}|list])
end

list
|> try_merge_package_version(version, parents)
|> process_merge_package_version(map, name, version, parents, list)
:error ->
Map.put(map, name, [{[version], parents}])
end
end)
end

defp process_merge_package_version({:ok, list}, map, name, _, _, _), do: Map.put(map, name, list)
defp process_merge_package_version(:error, map, name, version, parents, list),
do: Map.put(map, name, [{[version], parents}|list])

# For a given a package try to merge this version and parents
# into the existing ones
defp try_merge_package_version(list, version, parents) do
Expand Down

0 comments on commit 80cc04e

Please sign in to comment.