Skip to content
Browse files

Added agner_index:exists/2 to the specification and agner_github:exis…

…ts/2 implementation, allows avoiding confusing error messages being printed out
  • Loading branch information...
1 parent ef63d29 commit b8b3e05383d2d377f65c72f7c888ea8d62b02c95 @yrashk yrashk committed Mar 2, 2011
Showing with 69 additions and 27 deletions.
  1. +2 −0 include/agner_index.hrl
  2. BIN rebar
  3. +21 −1 src/agner_github.erl
  4. +1 −1 src/agner_index.erl
  5. +6 −1 src/agner_main.erl
  6. +39 −24 src/agner_server.erl
View
2 include/agner_index.hrl
@@ -4,3 +4,5 @@
-spec branches(agner_account(), agner_repo()) -> list({agner_repo_branch(), sha1()}) | not_found_error().
-spec spec(agner_account(), agner_repo(), agner_package_version()) -> agner_spec() | not_found_error().
-spec spec_url(agner_account(), agner_repo(), sha1()) -> url() | not_found_error().
+-spec exists(agner_account(), agner_repo()) -> true | false.
+
View
BIN rebar
Binary file not shown.
View
22 src/agner_github.erl
@@ -10,7 +10,8 @@
tags/2,
branches/2,
spec/3,
- spec_url/3
+ spec_url/3,
+ exists/2
]).
repositories(Account) ->
@@ -157,6 +158,25 @@ spec_1(Account, RepoServer, AtFilename) ->
spec_url(Account, Name, SHA1) ->
"https://github.com/" ++ proper_repo_name(Account, Name) ++ "/blob/" ++ SHA1 ++ "/agner.config".
+exists(Account, Name) ->
+ Port = agner_download:git(["ls-remote", "-h", "git://github.com/" ++ proper_repo_name(Account, Name) ++ ".git"],
+ [use_stdio, stderr_to_stdout, {line, 255}]),
+ PortHandler = fun (F) ->
+ receive
+ {'EXIT', Port, _} ->
+ false;
+ {Port,{exit_status,0}} ->
+ true;
+ {Port,{exit_status,_}} ->
+ false;
+ {Port, {data, _}} ->
+ F(F);
+ _ ->
+ F(F)
+ end
+ end,
+ PortHandler(PortHandler).
+
%%%
proper_repo_name(Account, Name) ->
View
2 src/agner_index.erl
@@ -6,6 +6,6 @@
behaviour_info(callbacks) ->
[{repositories,1}, {repository, 2}, {tags,2}, {branches, 2}, {spec, 3},
- {spec_url, 3}];
+ {spec_url, 3},{exists, 2}];
behaviour_info(_Other) ->
undefined.
View
7 src/agner_main.erl
@@ -206,7 +206,12 @@ handle_command(spec, Opts) ->
Version = proplists:get_value(version, Opts),
case proplists:get_value(browser, Opts) of
true ->
- agner_utils:launch_browser(agner:spec_url(Package, Version));
+ case agner:spec_url(Package, Version) of
+ {error, not_found} ->
+ ignore; %% error will be reported later
+ URL ->
+ agner_utils:launch_browser(URL)
+ end;
_ ->
ignore
end,
View
63 src/agner_server.erl
@@ -212,11 +212,16 @@ handle_spec(_,_,From,[]) ->
gen_server:reply(From, {error, not_found});
handle_spec(Name, Version, From, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
- case Mod:spec(Params, Name, Version) of
- {error, not_found} ->
- handle_spec(Name, Version, From, Rest);
- Data ->
- gen_server:reply(From, Data)
+ case Mod:exists(Params, Name) of
+ true ->
+ case Mod:spec(Params, Name, Version) of
+ {error, not_found} ->
+ handle_spec(Name, Version, From, Rest);
+ Data ->
+ gen_server:reply(From, Data)
+ end;
+ false ->
+ handle_spec(Name, Version, From, Rest)
end.
-spec handle_spec_url(agner_package_name(), agner_package_version(), gen_server_from(), agner_indices()) -> any().
@@ -226,11 +231,16 @@ handle_spec_url(Name, Version, From, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
case sha1(Mod, Params, Name, Version) of
SHA1 when is_list(SHA1) ->
- case Mod:spec_url(Params, Name, SHA1) of
- {error, not_found} ->
- handle_spec_url(Name, Version, From, Rest);
- URL ->
- gen_server:reply(From, URL)
+ case Mod:exists(Params, Name) of
+ true ->
+ case Mod:spec_url(Params, Name, SHA1) of
+ {error, not_found} ->
+ handle_spec_url(Name, Version, From, Rest);
+ URL ->
+ gen_server:reply(From, URL)
+ end;
+ false ->
+ handle_spec_url(Name, Version, From, Rest)
end;
_ ->
gen_server:reply(From, {error, bad_version})
@@ -275,20 +285,25 @@ handle_versions(_,From,[]) ->
gen_server:reply(From, {error, not_found});
handle_versions(Name, From, [{Mod0, Params}|Rest]) ->
Mod = index_module(Mod0),
- case Mod:repository(Params, Name) of
- {error, not_found} ->
- handle_versions(Name, From, Rest);
- _ ->
- Branches = lists:map(fun
- ({[$%|_],_}) -> undefined;
- ({"gh-pages",_}) -> undefined;
- ({Branch, _}) -> {flavour, Branch} end,
- Mod:branches(Params, Name)),
- Tags = lists:map(fun ({[$%|_],_}) -> undefined;
- ({Tag, _}) -> {release, Tag} end,
- Mod:tags(Params, Name)),
- gen_server:reply(From, lists:filter(fun (undefined) -> false; (_) -> true end, Branches ++ Tags))
- end.
+ case Mod:exists(Params, Name) of
+ true ->
+ case Mod:repository(Params, Name) of
+ {error, not_found} ->
+ handle_versions(Name, From, Rest);
+ _ ->
+ Branches = lists:map(fun
+ ({[$%|_],_}) -> undefined;
+ ({"gh-pages",_}) -> undefined;
+ ({Branch, _}) -> {flavour, Branch} end,
+ Mod:branches(Params, Name)),
+ Tags = lists:map(fun ({[$%|_],_}) -> undefined;
+ ({Tag, _}) -> {release, Tag} end,
+ Mod:tags(Params, Name)),
+ gen_server:reply(From, lists:filter(fun (undefined) -> false; (_) -> true end, Branches ++ Tags))
+ end;
+ false ->
+ handle_versions(Name, From, Rest)
+ end.
-spec sha1(agner_index(), agner_account(), agner_package_name(), agner_package_version()) -> sha1().

0 comments on commit b8b3e05

Please sign in to comment.
Something went wrong with that request. Please try again.