Skip to content

Instantly share code, notes, and snippets.

@sinasamavati
Created February 23, 2014 18:21
Show Gist options
  • Save sinasamavati/9175134 to your computer and use it in GitHub Desktop.
Save sinasamavati/9175134 to your computer and use it in GitHub Desktop.
Fetcher
-module(ziton).
-export([fetch/2]).
-export([fetch/3]).
%% download content at URI to Path
fetch(URI, Path) ->
ensure_deps_started(),
case httpc:request(URI) of
Error={error, _} ->
Error;
{ok, {{_, Status, _}, _, Body}} when Status =:= 200; Status =:= 304 ->
file:make_dir(filename:dirname(Path)),
file:write_file(Path, Body, [raw, binary]);
{ok, {{_, Status, _}, _, _}} ->
{error, Status}
end.
%% clone git/hg/svn repository
fetch(SCM, URI, Path) when SCM =:= git; SCM =:= hg ->
exec(SCM, ["clone", URI, Path]);
fetch(svn, URI, Path) ->
exec("svn", ["checkout", URI, Path]).
%% execute a shell command
exec(Cmd, Opts) when is_atom(Cmd) ->
exec(atom_to_list(Cmd), Opts);
exec(Cmd, Opts) ->
Opts1 = [" " ++ X || X <- Opts],
os:cmd(Cmd ++ lists:concat(Opts1)).
ensure_deps_started() ->
ensure_started(crypto),
ensure_started(asn1),
ensure_started(public_key),
ensure_started(ssl),
ensure_started(inets).
ensure_started(App) ->
case application:start(App) of
ok ->
ok;
{error, {already_started, App}} ->
ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment