Fix peer mode queries and add help output

This commit is contained in:
Craig Everett 2020-01-06 11:00:59 +09:00
parent 94224a7ba0
commit 63d85b9f7a
48 changed files with 102 additions and 87 deletions

View File

@ -1 +1 @@
0.6.0
0.6.1

View File

@ -1,6 +1,6 @@
{application,zx,
[{description,"An Erlang development tool and Zomp user client"},
{vsn,"0.6.0"},
{vsn,"0.6.1"},
{applications,[stdlib,kernel]},
{modules,[zx,zx_auth,zx_conn,zx_conn_sup,zx_daemon,zx_key,
zx_lib,zx_local,zx_net,zx_peer,zx_peer_man,

View File

@ -24,7 +24,7 @@
%%% @end
-module(zx).
-vsn("0.6.0").
-vsn("0.6.1").
-behavior(application).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
@ -899,6 +899,7 @@ usage_user() ->
" zx list realms~n"
" zx list packages Realm~n"
" zx list versions PackageID~n"
" zx list [gui | cli | app | lib]~n"
" zx latest PackageID~n"
" zx search Tag~n"
" zx describe Package~n"

View File

@ -9,7 +9,7 @@
%%% @end
-module(zx_auth).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end
-module(zx_conn).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_conn_sup).
-vsn("0.6.0").
-vsn("0.6.1").
-behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -138,7 +138,7 @@
%%% @end
-module(zx_daemon).
-vsn("0.6.0").
-vsn("0.6.1").
-behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
@ -147,8 +147,8 @@
-export([zomp_mode/0]).
-export([pass_meta/3, argv/0,
subscribe/1, unsubscribe/1,
list/0, list/1, list/2, list/3, list_type/2, latest/1,
describe/1, provides/2, list_deps/1, search/2,
list/0, list/1, list/2, list/3, list_type/1, latest/1,
describe/1, provides/2, list_deps/1, search/1,
list_sysops/1,
fetch/1, install/1, build/1,
wait_result/1, wait_results/1]).
@ -187,7 +187,7 @@
dropped = maps:new() :: requests(),
timer = none :: none | reference(),
mx = mx_new() :: monitor_index(),
cx = new_cx() :: conn_index()}).
cx = offline :: conn_index()}).
-record(conf,
{realms = zx_lib:list_realms() :: [zx:realm()],
@ -233,7 +233,7 @@
| {request, pid(), id(), action()}.
-type requests() :: #{id() := {pid(), action()}}.
-type monitor_index() :: #{pid() := {reference(), category()}}.
-type conn_index() :: #cx{} | zomp | proxy.
-type conn_index() :: #cx{} | zomp | proxied | offline.
-type realm_meta() :: #rmeta{}.
-type connection() :: #conn{}.
-type category() :: {Reqs :: [id()], Subs :: [zx:package()]}
@ -446,12 +446,11 @@ list(Realm, Name, Version) ->
request({list, Realm, Name, Version}).
-spec list_type(Realm, Type) -> {ok, RequestID}
when Realm :: zx:realm(),
Type :: zx:package_type(),
-spec list_type(Target) -> {ok, RequestID}
when Target :: {zx:realm(), zx:package_type()},
RequestID :: term().
list_type(Realm, Type) ->
list_type({Realm, Type}) ->
true = zx_lib:valid_lower0_9(Realm),
request({list_type, Realm, Type}).
@ -499,12 +498,11 @@ provides(Realm, Module) ->
request({provides, Realm, Module}).
-spec search(Realm, String) -> {ok, RequestID}
when Realm :: zx:realm(),
String :: string(),
-spec search(Target) -> {ok, RequestID}
when Target :: {zx:realm(), string()},
RequestID :: integer().
search(Realm, String) ->
search({Realm, String}) ->
request({search, Realm, String}).
@ -751,7 +749,7 @@ start_link() ->
init(none) ->
State = #s{},
{ok, cx_load(State)}.
{ok, State}.
-spec connect() -> ok.
@ -957,8 +955,11 @@ terminate(_, _) -> ok.
do_idle(#s{id = ID, cx = zomp}) ->
ok = log(info, "Idling as zomp with ID: ~p.", [ID]),
ok = retire(ID);
do_idle(#s{cx = proxy}) ->
ok = log(info, "Idling as proxy."),
do_idle(#s{cx = offline}) ->
ok = log(info, "Idling while offline."),
ok;
do_idle(#s{cx = proxied}) ->
ok = log(info, "Idling while proxied."),
ok;
do_idle(#s{id = ID, cx = CX}) ->
ok = log(info, "Idling as prime with ID: ~p.", [ID]),
@ -1035,10 +1036,12 @@ do_unsubscribe(Pid, Package, State = #s{actions = Actions, mx = MX}) ->
%% @private
%% Enqueue requests and update relevant index.
do_request(Requestor, Action, State = #s{id = ID, cx = proxy}) ->
do_request(Requestor, Action, State = #s{id = ID, cx = proxied}) ->
Result = zx_proxy:request(Action),
Requestor ! {result, ID, Result},
State;
do_request(_, _, #s{cx = offline}) ->
throw("Trying to perform request while offline. Impossible! I am ded.");
do_request(Requestor, Action, State = #s{id = ID, actions = Actions, mx = MX}) ->
NewActions = [{request, Requestor, ID, Action} | Actions],
NewMX = mx_add_monitor(Requestor, {requestor, ID}, MX),
@ -1057,9 +1060,12 @@ do_request(Requestor, Action, State = #s{id = ID, actions = Actions, mx = MX}) -
do_report(Conn, Report, State = #s{cx = zomp}) ->
ok = log(warning, "Zomp mode: Discarding report ~tp ~200tp", [Conn, Report]),
State;
do_report(Conn, Report, State = #s{cx = proxy}) ->
do_report(Conn, Report, State = #s{cx = proxied}) ->
ok = log(warning, "Proxied: Discarding report ~tp ~200tp", [Conn, Report]),
State;
do_report(Conn, Report, State = #s{cx = offline}) ->
ok = log(warning, "Offline: Discarding report ~tp ~200tp", [Conn, Report]),
State;
do_report(Conn, {connected, Realms}, State = #s{mx = MX, cx = CX}) ->
{NewMX, NewCX} =
case cx_connected(Realms, Conn, CX) of
@ -1154,21 +1160,24 @@ dequeue(Pending) ->
connect(State = #s{cx = zomp}) ->
State;
connect(State) ->
connect(State = #s{cx = offline}) ->
LockFile = lockfile(),
case file:read_file(LockFile) of
{ok, PS} ->
Port = binary_to_integer(string:trim(PS)),
ok = log(info, "Connecting to local proxy."),
proxy_connect(Port, State);
{error, enoent} ->
remote_connect(State)
end.
remote_connect(cx_load(State))
end;
connect(State) ->
remote_connect(State).
proxy_connect(Port, State) ->
case zx_proxy:connect(Port) of
ok -> State#s{cx = proxy};
error -> remote_connect(State)
ok -> State#s{cx = proxied};
error -> remote_connect(cx_load(State))
end.
@ -1191,7 +1200,7 @@ init_connections([], State) ->
ensure_connections(State = #s{cx = zomp}) ->
State;
ensure_connections(State = #s{cx = proxy}) ->
ensure_connections(State = #s{cx = proxied}) ->
State;
ensure_connections(State = #s{conf = Conf, mx = MX, cx = CX}) ->
#conf{realms = Realms, managed = Managed} = Conf,
@ -1212,14 +1221,20 @@ ensure_connections(State = #s{conf = Conf, mx = MX, cx = CX}) ->
-spec wipe_connections(state()) -> ok.
wipe_connections(State = #s{cx = proxy}) ->
wipe_connections(State = #s{cx = proxied}) ->
ok = log(warning, "Proxied: No connections to wipe."),
State;
wipe_connections(State = #s{cx = offline}) ->
ok = log(warning, "Offline: No connections to wipe."),
State;
wipe_connections(State = #s{cx = zomp}) ->
ok = log(warning, "Zomp: No connections to wipe."),
State;
wipe_connections(State = #s{mx = MX, cx = CX}) ->
{Pids, NewCX} = cx_wipe(CX),
Pids = cx_wipe(CX),
Remove = fun(P, M) -> mx_del_monitor(P, conn, M) end,
NewMX = lists:foldl(Remove, MX, Pids),
State#s{mx = NewMX, cx = NewCX}.
State#s{mx = NewMX, cx = offline}.
-spec do_result(ID, Result, State) -> NewState
@ -1321,7 +1336,7 @@ do_notify(Conn, Channel, Message, #s{cx = CX}) ->
%% This function must iterate as far as it can into the request queue, adding response
%% entries to the pending response structure as it goes.
eval_queue(State = #s{cx = proxy}) ->
eval_queue(State = #s{cx = proxied}) ->
State;
eval_queue(State = #s{actions = Actions}) ->
InOrder = lists:reverse(Actions),
@ -1390,7 +1405,8 @@ local_request(R, {describe, N, V}) -> zomp_realm:describe(R, {N, V});
local_request(R, {provides, M}) -> zomp_realm:provides(R, M);
local_request(R, {search, S}) -> zomp_realm:search(R, S);
local_request(R, {list_deps, N, V}) -> zomp_realm:list_deps(R, {N, V});
local_request(R, {list_sysops}) -> zomp_realm:list_sysops(R).
local_request(R, {list_sysops}) -> zomp_realm:list_sysops(R);
local_request(R, {list_type, T}) -> zomp_realm:list_type(R, T).
remote_dispatch([], State) ->
@ -2014,23 +2030,6 @@ do_drop_realm(Realm, State) ->
do_abdicate(Realm, State).
-spec become_proxy(State) -> NewState
when State :: state(),
NewState :: state().
become_proxy(State = #s{cx = zomp}) ->
ok = log(warning, "Already set as zomp node."),
State;
become_proxy(State = #s{cx = proxy}) ->
{ok, Port} = zx_peer_man:listen(),
ok = write_lockfile(Port),
{ok, ID} = zx_proxy:youre_fired(),
State#s{id = ID, cx = zomp};
become_proxy(State) ->
ok = log(warning, "Already acting proxy."),
State.
-spec become_zomp_node(State) -> NewState
when State :: state(),
NewState :: state().
@ -2038,9 +2037,13 @@ become_proxy(State) ->
become_zomp_node(State = #s{cx = zomp}) ->
ok = log(warning, "Already set as zomp node."),
State;
become_zomp_node(State = #s{cx = proxy}) ->
NewState = become_proxy(State),
NewState#s{cx = zomp};
become_zomp_node(State = #s{cx = offline}) ->
State#s{cx = zomp};
become_zomp_node(State = #s{cx = proxied}) ->
{ok, Port} = zx_peer_man:listen(),
ok = write_lockfile(Port),
{ok, ID} = zx_proxy:youre_fired(),
State#s{id = ID, cx = zomp};
become_zomp_node(State) ->
NewState = wipe_connections(State),
NewState#s{cx = zomp}.
@ -2178,10 +2181,6 @@ mx_drop_monitor(Pid, MX) ->
%%%
%%% Return values often carry some status information with them.
-spec new_cx() -> conn_index().
new_cx() -> #cx{}.
-spec cx_load(state()) -> state().
%% @private
@ -2320,9 +2319,11 @@ cx_hosts_cache() ->
cx_realms(#cx{realms = Realms}) ->
maps:keys(Realms);
cx_realms(offline) ->
zx_lib:list_realms();
cx_realms(zomp) ->
zx_lib:list_realms();
cx_realms(proxy) ->
cx_realms(proxied) ->
{ok, Realms} = zx_proxy:request(list),
Realms.
@ -2330,12 +2331,12 @@ cx_realms(proxy) ->
-spec cx_mirrors(CX) -> Result
when CX :: conn_index(),
Result :: {ok, [zx:host()]}
| {error, zomp}.
| {error, offline | zomp | proxied}.
cx_mirrors(#cx{mirrors = Mirrors}) ->
{ok, queue:to_list(Mirrors)};
cx_mirrors(zomp) ->
{error, zomp}.
cx_mirrors(Status) ->
{error, Status}.
-spec cx_check_service(Realms, CX) -> Result
@ -2453,14 +2454,14 @@ cx_redirect([], CX) ->
CX.
-spec cx_wipe(CX) -> ok
when CX :: conn_index().
-spec cx_wipe(CX) -> Pids
when CX :: conn_index(),
Pids :: [pid()].
cx_wipe(CX = #cx{conns = Conns, mirrors = Mirrors}) ->
cx_wipe(#cx{conns = Conns}) ->
Pids = [P || #conn{pid = P} <- Conns],
ok = lists:foreach(fun zx_conn:retire/1, Pids),
NewCX = CX#cx{conns = [], hosts = Mirrors},
{Pids, NewCX}.
Pids.
-spec cx_disconnected(Conn, CX) -> {Requests, Subs, NewCX}

View File

@ -8,7 +8,7 @@
%%% @end
-module(zx_key).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -10,7 +10,7 @@
%%% @end
-module(zx_lib).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -6,7 +6,7 @@
%%% @end
-module(zx_local).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").
@ -757,16 +757,27 @@ list_type(Type) ->
Realms = zx_lib:list_realms(),
MakeRequest =
fun(Realm) ->
{ok, ID} = zx_daemon:list_type(Realm, Type),
{ok, ID} = zx_daemon:list_type({Realm, Type}),
ID
end,
Index = [{MakeRequest(R), R} || R <- Realms],
IDs = [element(1, I) || I <- Index],
case zx_daemon:wait_results(IDs) of
{ok, Results} -> print_multirealm(lists:sort(Index), lists:sort(Results));
Error -> Error
{ok, Results} ->
Packages = scrub_errors(lists:sort(Index), lists:sort(Results), []),
lists:foreach(fun print_packages/1, lists:sort(Packages));
Error ->
Error
end.
scrub_errors([{ID, _} | Index], [{ID, {ok, PackageID}} | Results], Acc) ->
scrub_errors(Index, Results, [PackageID | Acc]);
scrub_errors([{ID, Realm} | Index], [{ID, Error} | Results], Acc) ->
ok = tell(warning, "Received weird result from realm ~tp: ~tp", [Realm, Error]),
scrub_errors(Index, Results, Acc);
scrub_errors([], [], Acc) ->
lists:append(Acc).
-spec latest(PackageString :: string()) -> zx:outcome().
@ -845,7 +856,7 @@ search(String) ->
Realms = zx_lib:list_realms(),
MakeRequest =
fun(Realm) ->
{ok, ID} = zx_daemon:search(Realm, String),
{ok, ID} = zx_daemon:search({Realm, String}),
ID
end,
Index = [{MakeRequest(R), R} || R <- Realms],

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_net).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -8,7 +8,7 @@
%%% @end
-module(zx_peer).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").
@ -181,7 +181,8 @@ handle_message(<<Command:8, Bin/binary>>) ->
24 -> list_keys(Payload);
25 -> zx_daemon:takeover(Payload);
26 -> zx_daemon:abdicate(Payload);
27 -> zx_daemon:drop_realm(Payload)
27 -> zx_daemon:drop_realm(Payload);
28 -> deferred(fun zx_daemon:list_type/1, Payload)
end,
pack(Result).

View File

@ -9,7 +9,7 @@
%%% @end
-module(zx_peer_man).
-vsn("0.6.0").
-vsn("0.6.1").
-behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end
-module(zx_peer_sup).
-vsn("0.6.0").
-vsn("0.6.1").
-behaviour(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -10,7 +10,7 @@
%%% @end
-module(zx_peers).
-vsn("0.6.0").
-vsn("0.6.1").
-behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_proxy).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").
@ -167,6 +167,7 @@ dispatch(Socket, Action) ->
{takeover, R} -> make_query(Socket, 25, R);
{abdicate, R} -> make_query(Socket, 26, R);
{drop_realm, R} -> make_query(Socket, 27, R);
{list_type, R, T} -> make_query(Socket, 28, {R, T});
Unexpected ->
Message = "Received unexpected request action. Action: ~200tp",
ok = log(warning, Message, [Unexpected]),

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_sup).
-vsn("0.6.0").
-vsn("0.6.1").
-behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end
-module(zx_tty).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end
-module(zx_userconf).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end
-module(zx_zsp).
-vsn("0.6.0").
-vsn("0.6.1").
-author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").

View File

@ -8,7 +8,7 @@
{license,"MIT"}.
{modules,[]}.
{name,"zx"}.
{package_id,{"otpr","zx",{0,6,0}}}.
{package_id,{"otpr","zx",{0,6,1}}}.
{prefix,"zx_"}.
{repo_url,"https://gitlab.com/zxq9/zx"}.
{tags,["tools","package manager","erlang"]}.