From 5d7d6599a96048807d05df6ca7984bfbe6c68df3 Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Fri, 24 Nov 2017 11:53:11 +0900 Subject: [PATCH] wip --- zx | 122 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 27 deletions(-) diff --git a/zx b/zx index 46c2555..bfc7072 100755 --- a/zx +++ b/zx @@ -23,6 +23,7 @@ version = {z, z, z} :: version(), type = app :: app | lib, deps = [] :: [package_id()], + serial = 0 :: serial(), dir = none :: none | file:filename(), socket = none :: none | gen_tcp:socket(), pid = none :: none | pid(), @@ -30,7 +31,7 @@ -type state() :: #s{}. -%-type serial() :: pos_integer(). +-type serial() :: pos_integer(). -type package_id() :: {realm(), name(), version()}. -type package() :: {realm(), name()}. -type realm() :: lower0_9(). @@ -44,9 +45,9 @@ % Type :: public | private, % DER :: binary()}. -type key_id() :: {realm(), key_name()}. --type key_name() :: lower0_9(). +-type key_name() :: label(). -type lower0_9() :: [$a..$z | $0..$9 | $_]. -%-type label() :: [$a..$z | $0..$9 | $_ | $- | $.]. +-type label() :: [$a..$z | $0..$9 | $_ | $- | $.]. -type package_meta() :: #{}. @@ -106,12 +107,16 @@ start(["package", TargetDir]) -> end; start(["submit", PackageFile]) -> submit(PackageFile); -start(["keygen"]) -> - keygen(); -start(["genplt"]) -> - genplt(); start(["dialyze"]) -> dialyze(); +start(["create", "keypair"]) -> + create_keypair(); +start(["create", "plt"]) -> + create_plt(); +start(["create", "realm"]) -> + create_realm(); +start(["create", "sysop"]) -> + create_sysop(); start(_) -> usage_exit(22). @@ -119,7 +124,6 @@ start(_) -> %%% Execution of application - -spec run(Identifier, Args) -> no_return() when Identifier :: string(), Args :: [string()]. @@ -159,7 +163,6 @@ run(Identifier, Args) -> %%% Project initialization - -spec initialize(Type, PackageID) -> no_return() when Type :: app | lib, PackageID :: package_id(). @@ -192,7 +195,6 @@ initialize(Type, PackageID) -> %%% Add a package from a local file - -spec assimilate(PackageFile) -> PackageID when PackageFile :: file:filename(), PackageID :: package_id(). @@ -229,7 +231,6 @@ assimilate(PackageFile) -> %%% Set dependency - -spec set_dep(package_id()) -> no_return(). %% @private %% 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 - -spec drop_dep(package_id()) -> no_return(). %% @private %% Remove the indicate dependency from the local project's zomp.meta record. @@ -564,7 +564,6 @@ drop_dep(PackageID) -> %%% Drop key - -spec drop_key(key_id()) -> no_return(). %% @private %% 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 - -spec verup(Level) -> no_return() when Level :: string(). %% @private @@ -797,7 +795,6 @@ check_update(State) -> %%% Package submission - -spec submit(PackageFile) -> no_return() when PackageFile :: file:filename(). %% @private @@ -895,7 +892,8 @@ connect_user(Realm, Hosts = [Node = {Host, Port} | Rest]) -> Socket :: gen_tcp:socket(), Hosts :: [host()]. %% @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) -> {ok, {Addr, Port}} = inet:peername(Socket), @@ -1120,8 +1118,8 @@ realm_meta(Realm) -> end. -%%% Key generation +%%% Key generation -spec prompt_keygen() -> key_id(). %% @private @@ -1137,7 +1135,7 @@ prompt_keygen() -> " key name separated by a space.~n~n" " Example: some.realm my.key~n", ok = io:format(Message), - Input = string:trim(io:get_line("(^C to quit): ")), + Input = get_input(), {Realm, KeyName} = case string:lexemes(Input, " ") of [R, K] -> {R, K}; @@ -1155,16 +1153,16 @@ prompt_keygen() -> end. --spec keygen() -> no_return(). +-spec create_keypair() -> no_return(). %% @private %% Execute the key generation procedure for 16k RSA keys once and then terminate. -keygen() -> +create_keypair() -> ok = file:set_cwd(zomp_dir()), KeyID = prompt_keygen(), case generate_rsa(KeyID) of 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. @@ -1333,14 +1331,13 @@ loadkey(Type, {Realm, KeyName}) -> %%% Generate PLT - --spec genplt() -> no_return(). +-spec create_plt() -> no_return(). %% @private %% 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, %% and exit. -genplt() -> +create_plt() -> ok = build_plt(), 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 @@ -1822,6 +1879,14 @@ namify(PackageID, Suffix) -> %%% 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 when Options :: [option()], Selected :: term(). @@ -2089,6 +2154,7 @@ usage() -> "~n" "Usage:~n" " zx help~n" + " zx run~n" " zx run PackageID [Args]~n" " zx init Type PackageID~n" " zx install PackageID~n" @@ -2097,11 +2163,13 @@ usage() -> " zx drop dep PackageID~n" " zx drop key Realm KeyName~n" " zx verup Level~n" - " zx runlocal~n" + " zx runlocal [Args]~n" " zx package [Path]~n" " zx submit Path~n" - " zx keygen~n" - " zx genplt~n" + " zx create keypair~n" + " zx create plt~n" + " zx create realm~n" + " zx create sysop~n" "~n" "Where~n" " PackageID :: A string of the form Realm-Name[-Version]~n"