diff options
author | CarlosEDP <me@carlosedp.com> | 2015-07-01 18:01:35 -0300 |
---|---|---|
committer | CarlosEDP <me@carlosedp.com> | 2015-07-03 16:40:26 -0300 |
commit | 60265aba3497aa97a35b706e00a2526e100733bd (patch) | |
tree | e34089733acaf2621d12aa9e69b10b87e6917d80 | |
parent | 8de84f1af04a716357dbc5473f7daf74cb31f44a (diff) |
Get proxy vars from ~/.config/rebar3/rebar.config. Variable format is {http_proxy, http://host:port} or {http_proxy, http://host:port}
-rwxr-xr-x | bootstrap | 59 | ||||
-rw-r--r-- | src/rebar_utils.erl | 69 |
2 files changed, 43 insertions, 85 deletions
@@ -82,7 +82,7 @@ extract(Binary) -> {ok, Contents}. request(Url) -> - setHttpcOptions(), + set_httpc_options(), case httpc:request(get, {Url, []}, [{relaxed, true}], [{body_format, binary}]) of @@ -92,45 +92,30 @@ request(Url) -> Error end. -setHttpcOptions() -> - %% Get http_proxy and https_proxy environment variables - case os:getenv("http_proxy") of - false -> - Http = ""; - Http -> - Http - end, - case os:getenv("https_proxy") of - false -> - Https = ""; - Https -> - Https - end, - - %% Parse the variables to extract host and port - Opts = [], - case http_uri:parse(Http) of - {ok,{_, [], Host, Port, _, []}} -> - Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; - {error, _} -> - Opts1 = Opts - end, - - case http_uri:parse(Https) of - {ok,{_, [], Host2, Port2, _, []}} -> - Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; - {error, _} -> - Opts2 = Opts1 - end, - - case Opts2 of - [] -> - ok; +get_http_var() -> + {ok, [[Home]]} = init:get_argument(home), + ConfDir = filename:join(Home, ".config/rebar3"), + case file:consult(filename:join(ConfDir, "rebar.config")) of + {ok, Config} -> + Config; _ -> - httpc:set_options(Opts2), - ok + [] end. +get_http(Scheme) -> + proplists:get_value(Scheme, get_http_var(), ""). + +set_httpc_options() -> + set_httpc_options(https_proxy, get_http(https_proxy)), + set_httpc_options(proxy, get_http(http_proxy)). + +set_httpc_options(_, []) -> + ok; + +set_httpc_options(Scheme, Proxy) -> + {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), + httpc:set_options([{Scheme, {{Host, Port}, []}}]). + compile(App, FirstFiles) -> Dir = filename:join(filename:absname("_build/default/lib/"), App), filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])), diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 35964ea..77d5e71 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -663,53 +663,26 @@ maybe_ends_in_comma(H) -> _ -> false end. +get_http_var() -> + {ok, [[Home]]} = init:get_argument(home), + ConfDir = filename:join(Home, ".config/rebar3"), + case file:consult(filename:join(ConfDir, "rebar.config")) of + {ok, Config} -> + Config; + _ -> + [] + end. + +get_http(Scheme) -> + proplists:get_value(Scheme, get_http_var(), ""). + set_httpc_options() -> - %% Get http_proxy and https_proxy environment variables - case os:getenv("http_proxy") of - false -> - Http = ""; - Http -> - Http - end, - case os:getenv("https_proxy") of - false -> - Https = ""; - Https -> - Https - end, - - %% Parse the variables to extract host and port - Opts = [], - case http_uri:parse(Http) of - {ok,{_, [], Host, Port, _, []}} -> - Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; - {error, _} -> - Opts1 = Opts - end, + set_httpc_options(https_proxy, get_http(https_proxy)), + set_httpc_options(proxy, get_http(http_proxy)). - case http_uri:parse(Https) of - {ok,{_, [], Host2, Port2, _, []}} -> - Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; - {error, _} -> - Opts2 = Opts1 - end, - - case Opts2 of - [] -> - Opts3 = [ - {max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000} - ]; - _ -> - Opts3 = Opts2 ++ [ - {max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000} - ] - end, - httpc:set_options(Opts3). +set_httpc_options(_, []) -> + ok; + +set_httpc_options(Scheme, Proxy) -> + {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), + httpc:set_options([{Scheme, {{Host, Port}, []}}]). |