Skip to content

Commit e85ec0c

Browse files
feng19doawoo
authored andcommitted
feat: move duplicate code to util module
1 parent c2c7205 commit e85ec0c

3 files changed

Lines changed: 26 additions & 28 deletions

File tree

lib/steps/fetch/fetch_musl.ex

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,9 @@ defmodule Burrito.Steps.Fetch.FetchMusl do
5555
Log.info(:step, "Downloading file: #{url}")
5656

5757
resp =
58-
cond do
59-
proxy = System.get_env("HTTP_PROXY") || System.get_env("http_proxy") ->
60-
Log.info(:step, "Using HTTP_PROXY: #{proxy}")
61-
URI.parse(proxy)
62-
63-
proxy = System.get_env("HTTPS_PROXY") || System.get_env("https_proxy") ->
64-
Log.info(:step, "Using HTTPS_PROXY: #{proxy}")
65-
URI.parse(proxy)
66-
67-
true ->
68-
nil
69-
end
70-
|> case do
71-
%{scheme: scheme, host: host, port: port} when scheme in ["http", "https"] ->
58+
case Burrito.Util.get_proxy() do
59+
proxy = %{scheme: scheme, host: host, port: port} when scheme in ["http", "https"] ->
60+
Log.info(:step, "Using PROXY: #{proxy}")
7261
proxy = {String.to_atom(scheme), host, port, []}
7362
Req.get!(url, raw: true, connect_options: [proxy: proxy])
7463

lib/util/default_erts_resolver.ex

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,9 @@ defmodule Burrito.Util.DefaultERTSResolver do
100100
Log.info(:step, "Downloading file: #{url}")
101101

102102
resp =
103-
cond do
104-
proxy = System.get_env("HTTP_PROXY") || System.get_env("http_proxy") ->
105-
Log.info(:step, "Using HTTP_PROXY: #{proxy}")
106-
URI.parse(proxy)
107-
108-
proxy = System.get_env("HTTPS_PROXY") || System.get_env("https_proxy") ->
109-
Log.info(:step, "Using HTTPS_PROXY: #{proxy}")
110-
URI.parse(proxy)
111-
112-
true ->
113-
nil
114-
end
115-
|> case do
116-
%{scheme: scheme, host: host, port: port} when scheme in ["http", "https"] ->
103+
case Burrito.Util.get_proxy() do
104+
proxy = %{scheme: scheme, host: host, port: port} when scheme in ["http", "https"] ->
105+
Log.info(:step, "Using PROXY: #{proxy}")
117106
proxy = {String.to_atom(scheme), host, port, []}
118107
Req.get!(url, raw: true, connect_options: [proxy: proxy])
119108

lib/util/util.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,24 @@ defmodule Burrito.Util do
4747
def running_standalone?() do
4848
System.get_env("__BURRITO") != nil
4949
end
50+
51+
@doc """
52+
Gets the HTTP or HTTPS proxy from environment variables, if set.
53+
1. Checks `HTTP_PROXY` and `http_proxy`
54+
2. Checks `HTTPS_PROXY` and `https_proxy`
55+
3. Returns `nil` if no proxy is set
56+
"""
57+
@spec get_proxy :: URI.t() | nil
58+
def get_proxy do
59+
cond do
60+
proxy = System.get_env("HTTP_PROXY") || System.get_env("http_proxy") ->
61+
URI.parse(proxy)
62+
63+
proxy = System.get_env("HTTPS_PROXY") || System.get_env("https_proxy") ->
64+
URI.parse(proxy)
65+
66+
true ->
67+
nil
68+
end
69+
end
5070
end

0 commit comments

Comments
 (0)