This commit is contained in:
Craig Everett 2017-11-24 11:53:11 +09:00
parent 7b3fc3f3a9
commit 5d7d6599a9

122
zx
View File

@ -23,6 +23,7 @@
version = {z, z, z} :: version(), version = {z, z, z} :: version(),
type = app :: app | lib, type = app :: app | lib,
deps = [] :: [package_id()], deps = [] :: [package_id()],
serial = 0 :: serial(),
dir = none :: none | file:filename(), dir = none :: none | file:filename(),
socket = none :: none | gen_tcp:socket(), socket = none :: none | gen_tcp:socket(),
pid = none :: none | pid(), pid = none :: none | pid(),
@ -30,7 +31,7 @@
-type state() :: #s{}. -type state() :: #s{}.
%-type serial() :: pos_integer(). -type serial() :: pos_integer().
-type package_id() :: {realm(), name(), version()}. -type package_id() :: {realm(), name(), version()}.
-type package() :: {realm(), name()}. -type package() :: {realm(), name()}.
-type realm() :: lower0_9(). -type realm() :: lower0_9().
@ -44,9 +45,9 @@
% Type :: public | private, % Type :: public | private,
% DER :: binary()}. % DER :: binary()}.
-type key_id() :: {realm(), key_name()}. -type key_id() :: {realm(), key_name()}.
-type key_name() :: lower0_9(). -type key_name() :: label().
-type lower0_9() :: [$a..$z | $0..$9 | $_]. -type lower0_9() :: [$a..$z | $0..$9 | $_].
%-type label() :: [$a..$z | $0..$9 | $_ | $- | $.]. -type label() :: [$a..$z | $0..$9 | $_ | $- | $.].
-type package_meta() :: #{}. -type package_meta() :: #{}.
@ -106,12 +107,16 @@ start(["package", TargetDir]) ->
end; end;
start(["submit", PackageFile]) -> start(["submit", PackageFile]) ->
submit(PackageFile); submit(PackageFile);
start(["keygen"]) ->
keygen();
start(["genplt"]) ->
genplt();
start(["dialyze"]) -> start(["dialyze"]) ->
dialyze(); dialyze();
start(["create", "keypair"]) ->
create_keypair();
start(["create", "plt"]) ->
create_plt();
start(["create", "realm"]) ->
create_realm();
start(["create", "sysop"]) ->
create_sysop();
start(_) -> start(_) ->
usage_exit(22). usage_exit(22).
@ -119,7 +124,6 @@ start(_) ->
%%% Execution of application %%% Execution of application
-spec run(Identifier, Args) -> no_return() -spec run(Identifier, Args) -> no_return()
when Identifier :: string(), when Identifier :: string(),
Args :: [string()]. Args :: [string()].
@ -159,7 +163,6 @@ run(Identifier, Args) ->
%%% Project initialization %%% Project initialization
-spec initialize(Type, PackageID) -> no_return() -spec initialize(Type, PackageID) -> no_return()
when Type :: app | lib, when Type :: app | lib,
PackageID :: package_id(). PackageID :: package_id().
@ -192,7 +195,6 @@ initialize(Type, PackageID) ->
%%% Add a package from a local file %%% Add a package from a local file
-spec assimilate(PackageFile) -> PackageID -spec assimilate(PackageFile) -> PackageID
when PackageFile :: file:filename(), when PackageFile :: file:filename(),
PackageID :: package_id(). PackageID :: package_id().
@ -229,7 +231,6 @@ assimilate(PackageFile) ->
%%% Set dependency %%% Set dependency
-spec set_dep(package_id()) -> no_return(). -spec set_dep(package_id()) -> no_return().
%% @private %% @private
%% Set a specific dependency in the current project. If the project currently has a %% Set a specific dependency in the current project. If the project currently has a
@ -538,7 +539,6 @@ update_version(Realm, Name, OldVersion, NewVersion, OldMeta) ->
%%% Drop dependency %%% Drop dependency
-spec drop_dep(package_id()) -> no_return(). -spec drop_dep(package_id()) -> no_return().
%% @private %% @private
%% Remove the indicate dependency from the local project's zomp.meta record. %% Remove the indicate dependency from the local project's zomp.meta record.
@ -564,7 +564,6 @@ drop_dep(PackageID) ->
%%% Drop key %%% Drop key
-spec drop_key(key_id()) -> no_return(). -spec drop_key(key_id()) -> no_return().
%% @private %% @private
%% Given a KeyID, remove the related public and private keys from the keystore, if they %% Given a KeyID, remove the related public and private keys from the keystore, if they
@ -588,7 +587,6 @@ drop_key({Realm, KeyName}) ->
%%% Update version %%% Update version
-spec verup(Level) -> no_return() -spec verup(Level) -> no_return()
when Level :: string(). when Level :: string().
%% @private %% @private
@ -797,7 +795,6 @@ check_update(State) ->
%%% Package submission %%% Package submission
-spec submit(PackageFile) -> no_return() -spec submit(PackageFile) -> no_return()
when PackageFile :: file:filename(). when PackageFile :: file:filename().
%% @private %% @private
@ -895,7 +892,8 @@ connect_user(Realm, Hosts = [Node = {Host, Port} | Rest]) ->
Socket :: gen_tcp:socket(), Socket :: gen_tcp:socket(),
Hosts :: [host()]. Hosts :: [host()].
%% @private %% @private
%% Confirm the zomp node can handle "OTPR USER 1" and is accepting connections or try another node. %% Confirm the zomp node can handle "OTPR USER 1" and is accepting connections or try
%% another node.
confirm_user(Realm, Socket, Hosts) -> confirm_user(Realm, Socket, Hosts) ->
{ok, {Addr, Port}} = inet:peername(Socket), {ok, {Addr, Port}} = inet:peername(Socket),
@ -1120,8 +1118,8 @@ realm_meta(Realm) ->
end. end.
%%% Key generation
%%% Key generation
-spec prompt_keygen() -> key_id(). -spec prompt_keygen() -> key_id().
%% @private %% @private
@ -1137,7 +1135,7 @@ prompt_keygen() ->
" key name separated by a space.~n~n" " key name separated by a space.~n~n"
" Example: some.realm my.key~n", " Example: some.realm my.key~n",
ok = io:format(Message), ok = io:format(Message),
Input = string:trim(io:get_line("(^C to quit): ")), Input = get_input(),
{Realm, KeyName} = {Realm, KeyName} =
case string:lexemes(Input, " ") of case string:lexemes(Input, " ") of
[R, K] -> {R, K}; [R, K] -> {R, K};
@ -1155,16 +1153,16 @@ prompt_keygen() ->
end. end.
-spec keygen() -> no_return(). -spec create_keypair() -> no_return().
%% @private %% @private
%% Execute the key generation procedure for 16k RSA keys once and then terminate. %% Execute the key generation procedure for 16k RSA keys once and then terminate.
keygen() -> create_keypair() ->
ok = file:set_cwd(zomp_dir()), ok = file:set_cwd(zomp_dir()),
KeyID = prompt_keygen(), KeyID = prompt_keygen(),
case generate_rsa(KeyID) of case generate_rsa(KeyID) of
ok -> halt(0); ok -> halt(0);
Error -> error_exit("keygen failed with ~tp", [Error], ?FILE, ?LINE) Error -> error_exit("create_keypair/0 failed with ~tp", [Error], ?FILE, ?LINE)
end. end.
@ -1333,14 +1331,13 @@ loadkey(Type, {Realm, KeyName}) ->
%%% Generate PLT %%% Generate PLT
-spec create_plt() -> no_return().
-spec genplt() -> no_return().
%% @private %% @private
%% Generate a fresh PLT file that includes most basic core applications needed to %% Generate a fresh PLT file that includes most basic core applications needed to
%% make a resonable estimate of a type system, write the name of the PLT to stdout, %% make a resonable estimate of a type system, write the name of the PLT to stdout,
%% and exit. %% and exit.
genplt() -> create_plt() ->
ok = build_plt(), ok = build_plt(),
halt(0). halt(0).
@ -1400,6 +1397,66 @@ dialyze() ->
%%% Create Realm & Sysop
create_realm() ->
RealmMessage =
"~n Enter a name for your new realm.~n"
" Valid names can contain only lower-case letters, numbers and the underscore.~n"
" Valid names must begin with a lower-case letter.~n",
ok = io:format(RealmMessage),
Realm = get_input(),
case valid_lower0_9(Realm) of
true ->
RealmFile = filename:join(zomp_dir(), Realm ++ ".realm"),
case filelib:is_regular(RealmFile) of
false ->
create_realm(Realm);
true ->
ok = io:format("That realm already exists. Be more original.~n"),
create_realm()
end;
false ->
ok = io:format("Bad realm name ~tp. Try again.~n", [Realm]),
create_realm()
end.
create_realm(Realm) ->
UserNameMessage =
"~n Enter a username for the realm sysop.~n"
" Valid names can contain only lower-case letters, numbers and the underscore.~n"
" Valid names must begin with a lower-case letter.~n",
ok = io:format(UserNameMessage),
UserName = get_input(),
case valid_lower0_9(UserName) of
true ->
create_realm(Realm, UserName);
false ->
ok = io:format("Bad username ~tp. Try again.~n", [UserName]),
create_realm(Realm)
end.
create_realm(Realm, UserName, UserRecord, Prime, RealmKey, PackageKey) ->
RealmMeta =
[{realm, Realm},
{prime, Prime},
{realm_keys, [RealmKey],
{package_keys, [PacakageKey]
{revision, 0},
{serial, 0},
{mirrors, []}],
ok = log(info, "Seriously, we would be creating a realm now."),
halt(0).
create_sysop() ->
ok = log(info, "Fo' realz, yo! We be sysoppin up in hurr!"),
halt(0).
%%% Network operations and package utilities %%% Network operations and package utilities
@ -1822,6 +1879,14 @@ namify(PackageID, Suffix) ->
%%% User menu interface (terminal) %%% User menu interface (terminal)
-spec get_input() -> string().
%% @private
%% Provide a standard input prompt and newline sanitized return value.
get_input() ->
string:trim(io:get_line("(^C to quit): ")).
-spec select(Options) -> Selected -spec select(Options) -> Selected
when Options :: [option()], when Options :: [option()],
Selected :: term(). Selected :: term().
@ -2089,6 +2154,7 @@ usage() ->
"~n" "~n"
"Usage:~n" "Usage:~n"
" zx help~n" " zx help~n"
" zx run~n"
" zx run PackageID [Args]~n" " zx run PackageID [Args]~n"
" zx init Type PackageID~n" " zx init Type PackageID~n"
" zx install PackageID~n" " zx install PackageID~n"
@ -2097,11 +2163,13 @@ usage() ->
" zx drop dep PackageID~n" " zx drop dep PackageID~n"
" zx drop key Realm KeyName~n" " zx drop key Realm KeyName~n"
" zx verup Level~n" " zx verup Level~n"
" zx runlocal~n" " zx runlocal [Args]~n"
" zx package [Path]~n" " zx package [Path]~n"
" zx submit Path~n" " zx submit Path~n"
" zx keygen~n" " zx create keypair~n"
" zx genplt~n" " zx create plt~n"
" zx create realm~n"
" zx create sysop~n"
"~n" "~n"
"Where~n" "Where~n"
" PackageID :: A string of the form Realm-Name[-Version]~n" " PackageID :: A string of the form Realm-Name[-Version]~n"