So much foo

This commit is contained in:
Craig Everett 2018-05-29 22:16:11 +09:00
parent bd6c747207
commit 98f4b6bdf3
10 changed files with 1501 additions and 1254 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.eunit
deps
tester
*.o
*.beam
*.plt

File diff suppressed because it is too large Load Diff

View File

@ -37,7 +37,7 @@ list_pending(PackageName) ->
{ok, {R, N, {z, z, z}}} ->
{R, N};
{error, invalid_package_string} ->
zx:error_exit("~tp is not a valid package name.", [PackageName], ?LINE)
error_exit("~tp is not a valid package name.", [PackageName], ?LINE)
end,
case zx_daemon:list_pending(Package) of
{ok, []} ->
@ -53,12 +53,12 @@ list_pending(PackageName) ->
ok = lists:foreach(Print, Versions),
halt(0);
{error, bad_realm} ->
zx:error_exit("Bad realm name.", ?LINE);
error_exit("Bad realm name.", ?LINE);
{error, bad_package} ->
zx:error_exit("Bad package name.", ?LINE);
error_exit("Bad package name.", ?LINE);
{error, network} ->
Message = "Network issues are preventing connection to the realm.",
zx:error_exit(Message, ?LINE)
error_exit(Message, ?LINE)
end.
@ -82,12 +82,12 @@ list_resigns(Realm) ->
ok = lists:foreach(Print, PackageIDs),
halt(0);
{error, bad_realm} ->
zx:error_exit("Bad realm name.", ?LINE);
error_exit("Bad realm name.", ?LINE);
{error, no_realm} ->
zx:error_exit("Realm \"~ts\" is not configured.", ?LINE);
error_exit("Realm \"~ts\" is not configured.", ?LINE);
{error, network} ->
Message = "Network issues are preventing connection to the realm.",
zx:error_exit(Message, ?LINE)
error_exit(Message, ?LINE)
end.
@ -220,7 +220,7 @@ add_package(PackageName) ->
{ok, {Realm, Name, {z, z, z}}} ->
add_package(Realm, Name);
_ ->
zx:error_exit("~tp is not a valid package name.", [PackageName], ?LINE)
error_exit("~tp is not a valid package name.", [PackageName], ?LINE)
end.
@ -263,14 +263,14 @@ recv_or_die(Socket) ->
{ok, Response} ->
{ok, Response};
{error, Reason} ->
zx:error_exit("Action failed with: ~tp", [Reason], ?LINE);
error_exit("Action failed with: ~tp", [Reason], ?LINE);
Unexpected ->
zx:error_exit("Unexpected message: ~tp", [Unexpected], ?LINE)
error_exit("Unexpected message: ~tp", [Unexpected], ?LINE)
end;
{tcp_closed, Socket} ->
zx:error_exit("Lost connection to node unexpectedly.", ?LINE)
error_exit("Lost connection to node unexpectedly.", ?LINE)
after 5000 ->
zx:error_exit("Connection timed out.", ?LINE)
error_exit("Connection timed out.", ?LINE)
end.
@ -450,3 +450,29 @@ disconnect(Socket) ->
Message = "Shutdown connection ~p failed with: ~p",
log(warning, Message, [Socket, Error])
end.
%%% Error exits
-spec error_exit(Error, Line) -> no_return()
when Error :: term(),
Line :: non_neg_integer().
%% @private
%% Format an error message in a way that makes it easy to locate.
error_exit(Error, Line) ->
error_exit(Error, [], Line).
-spec error_exit(Format, Args, Line) -> no_return()
when Format :: string(),
Args :: [term()],
Line :: non_neg_integer().
%% @private
%% Format an error message in a way that makes it easy to locate.
error_exit(Format, Args, Line) ->
File = filename:basename(?FILE),
ok = log(error, "~ts:~tp: " ++ Format, [File, Line | Args]),
halt(1).

View File

@ -11,6 +11,9 @@
-copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0").
% FIXME
-compile(export_all).
-export([subscribe/2, unsubscribe/2, fetch/3, query/3]).
-export([start/1, stop/1]).
-export([start_link/1, init/2]).
@ -20,9 +23,9 @@
%%% Types
-type incoming() :: ping
| {sub, Channel :: term(), Message :: term()}
| term().
%-type incoming() :: ping
% | {sub, Channel :: term(), Message :: term()}
% | term().
%%% Interface
@ -250,7 +253,8 @@ handle_message(Socket, Bin) ->
Invalid ->
{ok, {Addr, Port}} = zomp:peername(Socket),
Host = inet:ntoa(Addr),
ok = log(warning, "Invalid message from ~tp:~p: ", [Invalid]),
Warning = "Invalid message from ~s:~p: ~tp",
ok = log(warning, Warning, [Host, Port, Invalid]),
ok = zx_net:disconnect(Socket),
terminate()
end.

View File

@ -13,7 +13,7 @@
-license("GPL-3.0").
-export([ensure_keypair/1, have_public_key/1, have_private_key/1,
prompt_keygen/0, create_keypair/0, generate_rsa/1,
prompt_keygen/0, grow_a_pair/0, generate_rsa/1,
load/2, verify/3]).
-include("zx_logger.hrl").
@ -102,16 +102,16 @@ prompt_keygen() ->
end.
-spec create_keypair() -> no_return().
-spec grow_a_pair() -> no_return().
%% @private
%% Execute the key generation procedure for 16k RSA keys once and then terminate.
create_keypair() ->
grow_a_pair() ->
ok = file:set_cwd(zx_lib:zomp_dir()),
KeyID = prompt_keygen(),
case generate_rsa(KeyID) of
{ok, _, _} -> halt(0);
Error -> zx:error_exit("create_keypair/0 error: ~tp", [Error], ?LINE)
Error -> error_exit("grow_a_pair/0 error: ~tp", [Error], ?LINE)
end.
@ -230,7 +230,7 @@ openssl() ->
false ->
ok = log(error, "OpenSSL could not be found in this system's PATH."),
ok = log(error, "Install OpenSSL and then retry."),
zx:error_exit("Missing system dependenct: OpenSSL", ?LINE);
error_exit("Missing system dependenct: OpenSSL", ?LINE);
Path ->
log(info, "OpenSSL executable found at: ~ts", [Path])
end,
@ -276,5 +276,31 @@ load(Type, {Realm, KeyName}) ->
verify(Data, Signature, PubKey) ->
case public_key:verify(Data, sha512, Signature, PubKey) of
true -> ok;
false -> zx:error_exit("Bad package signature!", ?LINE)
false -> error_exit("Bad package signature!", ?LINE)
end.
%%% Error exits
-spec error_exit(Error, Line) -> no_return()
when Error :: term(),
Line :: non_neg_integer().
%% @private
%% Format an error message in a way that makes it easy to locate.
error_exit(Error, Line) ->
error_exit(Error, [], Line).
-spec error_exit(Format, Args, Line) -> no_return()
when Format :: string(),
Args :: [term()],
Line :: non_neg_integer().
%% @private
%% Format an error message in a way that makes it easy to locate.
error_exit(Format, Args, Line) ->
File = filename:basename(?FILE),
ok = log(error, "~ts:~tp: " ++ Format, [File, Line | Args]),
halt(1).

View File

@ -29,7 +29,9 @@
namify_zsp/1, namify_tgz/1,
find_latest_compatible/2, installed/1,
realm_conf/1, load_realm_conf/1,
extract_zsp_or_die/1, halt_if_exists/1]).
extract_zsp_or_die/1, halt_if_exists/1,
build/0,
rm_rf/1, rm/1]).
-include("zx_logger.hrl").
@ -203,7 +205,7 @@ read_project_meta(Dir) ->
Path = filename:join(Dir, "zomp.meta"),
case file:consult(Path) of
{ok, Meta} ->
maps:from_list(Meta);
{ok, maps:from_list(Meta)};
Error ->
ok = log(error, "Failed to open \"zomp.meta\" with ~tp", [Error]),
ok = log(error, "Wrong directory?"),
@ -670,13 +672,13 @@ extract_zsp_or_die(FileName) ->
Files;
{error, {FileName, enoent}} ->
Message = "Can't find file ~ts.",
zx:error_exit(Message, [FileName], ?LINE);
error_exit(Message, [FileName], ?LINE);
{error, invalid_tar_checksum} ->
Message = "~ts is not a valid zsp archive.",
zx:error_exit(Message, [FileName], ?LINE);
error_exit(Message, [FileName], ?LINE);
{error, Reason} ->
Message = "Extracting package file failed with: ~160tp.",
zx:error_exit(Message, [Reason], ?LINE)
error_exit(Message, [Reason], ?LINE)
end.
@ -687,6 +689,70 @@ extract_zsp_or_die(FileName) ->
halt_if_exists(Path) ->
case filelib:is_file(Path) of
true -> zx:error_exit("~ts already exists! Halting.", [Path], ?LINE);
true -> error_exit("~ts already exists! Halting.", [Path], ?LINE);
false -> ok
end.
-spec build() -> ok.
%% @private
%% Run any local `zxmake' script needed by the project for non-Erlang code (if present),
%% then add the local `ebin/' directory to the runtime search path, and finally build
%% the Erlang part of the project with make:all/0 according to the local `Emakefile'.
build() ->
ZxMake = "zxmake",
ok =
case filelib:is_regular(ZxMake) of
true ->
Out = os:cmd(ZxMake),
log(info, Out);
false ->
ok
end,
true = code:add_patha(filename:absname("ebin")),
up_to_date = make:all(),
ok.
-spec rm_rf(file:filename()) -> ok | {error, file:posix()}.
%% @private
%% Recursively remove files and directories. Equivalent to `rm -rf'.
rm_rf(Path) ->
case filelib:is_dir(Path) of
true ->
Pattern = filename:join(Path, "**"),
Contents = lists:reverse(lists:sort(filelib:wildcard(Pattern))),
ok = lists:foreach(fun rm/1, Contents),
file:del_dir(Path);
false ->
file:delete(Path)
end.
-spec rm(file:filename()) -> ok | {error, file:posix()}.
%% @private
%% An omnibus delete helper.
rm(Path) ->
case filelib:is_dir(Path) of
true -> file:del_dir(Path);
false -> file:delete(Path)
end.
%%% Error exits
-spec error_exit(Format, Args, Line) -> no_return()
when Format :: string(),
Args :: [term()],
Line :: non_neg_integer().
%% @private
%% Format an error message in a way that makes it easy to locate.
error_exit(Format, Args, Line) ->
File = filename:basename(?FILE),
ok = log(error, "~ts:~tp: " ++ Format, [File, Line | Args]),
halt(1).

File diff suppressed because it is too large Load Diff

View File

@ -7,4 +7,4 @@ ZX_DIR="$ZOMP_DIR/lib/otpr/zx/$VERSION"
pushd "$ZX_DIR" > /dev/null
./make_zx
popd > /dev/null
erl -pa "$ZX_DIR/ebin" -run zx start $@
erl -pa "$ZX_DIR/ebin" -run zx run $@

View File

@ -6,4 +6,4 @@ set ZX_DIR="%ZOMP_DIR%\lib\otpr\zx\%VERSION%"
pushd "%ZX_DIR%"
escript.exe make_zx
popd
erl.exe -pa "%ZX_DIR%/ebin" -run zx start "%*"
erl.exe -pa "%ZX_DIR%/ebin" -run zx run "%*"

10
zx_dev
View File

@ -1,13 +1,15 @@
#!/bin/bash
pushd $(dirname $BASH_SOURCE) > /dev/null
export ZX_DEV_ROOT=$PWD
ZX_DEV_ROOT=$PWD
popd > /dev/null
export ZOMP_DIR="$ZX_DEV_ROOT/zomp"
export VERSION=$(cat "$ZOMP_DIR/etc/version.txt")
export ZOMP_DIR="$ZX_DEV_ROOT/tester"
rm -rf "$ZOMP_DIR"
cp -r "$ZX_DEV_ROOT/zomp" "$ZOMP_DIR"
VERSION=$(cat "$ZOMP_DIR/etc/version.txt")
export ZX_DIR="$ZOMP_DIR/lib/otpr/zx/$VERSION"
pushd "$ZX_DIR" > /dev/null
./make_zx
popd > /dev/null
erl -pa "$ZX_DIR/ebin" -run zx start $@
erl -pa "$ZX_DIR/ebin" -run zx run $@