Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Rad registry #652

Open
wants to merge 16 commits into
base: v2.0.0
Choose a base branch
from
178 changes: 178 additions & 0 deletions bin/rad-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/usr/bin/env radicle

(load! (find-module-file! "prelude.rad"))
(load! (find-module-file! "monadic/registry.rad"))
(file-module! "prelude/io-utils.rad")
(file-module! "prelude/error-messages.rad")
(file-module! "monadic/items.rad")

(import prelude/error-messages :as 'error)
(import monadic/items :as 'items)
(import prelude/strings '[split-by] :unqualified)
(import prelude/validation :as 'validation)
(import prelude/machine :as 'machine)
(import prelude/io :as 'io)
(import prelude/io-utils :as 'io)

(def path!
(fn []
(string-append (io/base-path!) "/registry.rad")))

(def init-registry-file!
(fn []
(io/shell! (string-append "mkdir -p " (io/base-path!)) "")
(io/init-file-dict! (path!))))

;; TODO Replace with server hosted machine
(def default-machine-name "12D3KooWNdyJ1QpNy5RbBfjKEx1gPvRJ72tgYo1vs6jvXU8yuQN2")

(def machine-name
(fn []
(init-registry-file!)
(match (io/read-file-key! (path!) :registry)
:nothing default-machine-name
[:just 'r] r)))

(def help
(string-append
"rad registry - Radicle Registry CLI

Usage:
rad registry list [--unread]
rad registry new
rad registry [show | delete | mark-read | mark-unread] <registered-machine-number>
rad registry all-read
rad registry init
rad registry set <registry-id>
rad registry help

list - Lists all registered Radicle machines
If the option '--unread' is appended, only unread machines
are listed.
Unread machines are marked with a * after the timestamp.
new - Register a new Radicle machine in $EDITOR
show - Show a registered Radicle machine
show-id - Show the registered Radicle machine id
delete - Delete a Radicle machine from the registry
This command is restricted to the maintainer of the registry
and the registrant.
mark-read - Mark registered Radicle machine as read
mark-unread - Mark registered Radicle machine as unread
all-read - Mark all registered Radicle machines as read
init - Initialize a new registry machine
set - Set the registry that is used for the commands instead of
the default. Delete the file to use the default registry.
help - Print this help and exit
"))

(def cmd-parse-failure
(fn [error]
(parse-failure error help)))

(def get-registered-machine
"Returns the machine of given number `n`."
(fn [n machine]
(items/verify-item-number n (list-registered-machines machine) :registered-machine)))

(def show-registered-machine!
"Shows a single MACHINE `n`"
(fn [n machine]
(def registered-machine (get-registered-machine n machine))
(put-str! (items/pretty-item-view (items/enrich-item machine registered-machine)))
(newness/mark-read! machine n)))

(def show-registered-machine-id!
"Shows the id of a single MACHINE `n`"
(fn [n machine]
(def registered-machine (get-registered-machine n machine))
(put-str! (lookup :registered-machine-id registered-machine))
(newness/mark-read! machine n)))

(def delete-registered-machine
(fn [n machine]
(catch 'daemon-error
(do
(get-registered-machine n machine)
(delete-registered-machine! machine {:registered-machine-number n})
(put-str! (string-append "Radicle machine #" (show n) " has been deleted."))
(newness/mark-read! machine n))
(fn [_]
(put-str! (error/state-change-failure :registered-machine "deleted"))
(exit! 1)))))

(def list
(fn [machine options]
(def registered-machines (values (list-registered-machines machine)))
(items/list-items-plain machine options registered-machines :registered-machine)))

(def prompt-for-metadata!
(fn []
(def name (prompt-validated!
"? What's the name of your Radicle machine: "
\(not (eq? ? ""))
"Radicle machine name can't be empty!"))
(def desc (prompt! "? Briefly describe the Radicle machine: "))
(def id (prompt-validated!
"? What's the id of your Radicle machine: "
\(not (eq? ? ""))
"Radicle machine id can't be empty!"))
(def labels (prompt! "? Do you want to add any labels (comma seperated): "))
{:name name :description desc :registered-machine-id id :labels (split-by (fn [x] (eq? x ",")) labels)}))

(def register-machine
(fn [machine]
(def meta (prompt-for-metadata!))
(def author {:git-username (get-git-username!)})
(match (register-machine! machine (simple-registered-machine (<> meta author)))
['n] (do (put-str! (string-append "Registered Radicle machine #" (show n) " in " machine))
(newness/mark-read! machine n))
_ (put-str! (error/no-number-returned :registered-machine)))))

(def cmd-options
[
{ :key :unread :type :flag :options ["--unread"] :default #f }
])

(def /list-cmd
(fn [opts]
(/cmd-opts "list" opts cmd-options help)))

(def args (get-args!))

(def whole-registered-machine-num
(fn [action num-str f]
(whole-num help :registered-machine action num-str f)))

(def mark-all-read!
(fn [machine]
(map (fn [i] (newness/mark-read! machine (lookup :number i)))
(values (list-registered-machines machine)))))

(def new-registry!
(fn []
(def registry (create-registry-machine!))
(put-str! "=> Assembled rad registry machine")
(put-str! (string-append "=> registry id: " registry))))

(def set-registry!
(fn [id]
(init-registry-file!)
(io/write-file-key! (path!) :registry id)
(put-str! (string-append "=> Set registry machine to " id))))

(machine/catch-daemon!
(fn []
(match args
(/list-cmd 'options) (list (machine-name) options)
(/cmd-0 "new" help) (register-machine (machine-name))
(/cmd-0 "init" help) (new-registry!)
(/cmd-1 "set" 'n help) (set-registry! n)
(/cmd-1 "delete" 'n help) (whole-registered-machine-num "delete" n (fn [n] (delete-registered-machine n (machine-name))))
(/cmd-1 "show" 'n help) (whole-registered-machine-num "show" n (fn [n] (show-registered-machine! n (machine-name))))
(/cmd-1 "show-id" 'n help) (whole-registered-machine-num "show-id" n (fn [n] (show-registered-machine-id! n (machine-name))))
(/cmd-1 "mark-read" 'n help) (whole-registered-machine-num "mark as read" n (fn [n] (newness/mark-read! (machine-name) n)))
(/cmd-1 "mark-unread" 'n help) (whole-registered-machine-num "mark as unread" n (fn [n] (newness/mark-unread! (machine-name) n)))
(/cmd-0 "all-read" help) (mark-all-read! (machine-name))
(/cmd-help) (put-str! help)
(/cons 'cmd _) (cmd-parse-failure (error/unknown-command cmd))
[] (put-str! help))))
4 changes: 3 additions & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ steps:
- |
set -euxo pipefail

RADPATH="$(pwd)/rad" stack exec radicle-doc docs/source/guide/Basics.lrad
RADPATH="$(pwd)/rad"
stack exec radicle-doc docs/source/guide/Basics.lrad
stack exec radicle-doc docs/source/guide/Issues.lrad

- id: "Integration tests"
waitFor:
Expand Down
58 changes: 27 additions & 31 deletions docs/source/guide/Issues.lrad
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,17 @@ arguments. Finally we print the issues so that we can see how the state evolves.
(print! (read-ref issues))
:ok))

Now we update the eval. We will check for a special command `update` for
updating the semantics of our machine (by executing arbitrary code), but otherwise
we delegate to `process-command`. In general of course you wouldn't want to
leave such an `update` command with no restrictions; we'll talk about that
later.

(load! "rad/machine.rad")

(def eval
(updatable-eval
(fn [expr state]
(eval-fn-app state 'process-command expr print!))))
Now we update the `tx` function. We will check for a special command `update`
for updating the semantics of our machine (by executing arbitrary code, by just
returning it), but otherwise we delegate to `process-command`. In general of
course you wouldn't want to leave such an `update` command with no restrictions;
we'll talk about that later.

(def tx
(fn [expr]
(match expr
(/cons (/= 'update) (/cons 'e /nil)) e
_ (process-command expr))))

Now we can start creating some issues. To generate some UUIDs, use the `uuid!`
function at the REPL.
Expand Down Expand Up @@ -260,26 +259,23 @@ To generate a signed issue we can call, after loading that code into a REPL:
And submit the result to the machine:

(create
{:author [:public-key
{:public_curve [:curve-fp
[:curve-prime
1.15792089237316195423570985008687907853269984665640564039457584007908834671663e77
[:curve-common
{:ecc_a 0.0
:ecc_b 7.0
:ecc_g [:Point
5.506626302227734366957871889516853432625060345377759417550018736038911672924e76
3.2670510020758816978083085130507043184471273380659243275938904335757337482424e76]
:ecc_h 1.0
:ecc_n 1.15792089237316195423570985008687907852837564279074904382605163141518161494337e77}]]]
:public_q [:Point
1.11054692487265016800292649812157504820937148585989526304621614094061257232989e77
3.6059272479591624612420581719526072934261866833779446725219340058438651734523e76]}]
{:author {:public_curve [:curve-fP
[:curve-prime
115792089237316195423570985008687907853269984665640564039457584007908834671663
{:ecc_a 0
:ecc_b 7
:ecc_g [:point
55066263022277343669578718895168534326250603453777594175500187360389116729240
32670510020758816978083085130507043184471273380659243275938904335757337482424]
:ecc_h 1
:ecc_n 115792089237316195423570985008687907852837564279074904382605163141518161494337}]]
:public_q [:point
1586540975871139910492059063500089494477229513147906504918938970723766793669
74983891113881023096743645247308456896420847655960927256341242535397070787987]}
:body "This is the body of the first issue with a verified author."
:id "76dd218b-fbc1-4384-9962-8bfbec5da2a2"
:signature [:Signature
{:sign_r 1.07685492960818947345554835683887719269111710108141784367526228085824476440077e77
:sign_s 3.740767076693925401519339475669557891360406440839428851888372253368058490896e76}]
:id "9acde0c6-9947-4487-b21d-c4098eeec148"
:signature {:sign_r 49074705514350374841918106922124016178680772245428117282000810858109960040776
:sign_s 91323997871126045836257037323734538249052522716973625287059503099904115325393}
:title "This issue has a verified author"})

Signing comments would be done in a similar way.
39 changes: 22 additions & 17 deletions docs/source/guide/IssuesHelper.rad
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
;; (def chain-id "some-unique-id")
(def chain-id "some-unique-id")

;; (def kp (gen-key-pair! (default-ecc-curve)))
(def kp (gen-key-pair! (default-ecc-curve)))

;; (def sk (lookup :private-key kp))
;; (def pk (lookup :public-key kp))
(def sk (lookup :private-key kp))
(def pk (lookup :public-key kp))

;; (def make-issue
;; (fn [title body]
;; (def id (uuid!))
;; (def msg
;; (string-append chain-id
;; id
;; title
;; body))
;; {:id id
;; :author pk
;; :title title
;; :body body
;; :signature (gen-signature! sk msg)}))
(def make-issue
(fn [title body]
(def id (uuid!))
(def msg
(string-append chain-id
id
title
body))
{:id id
:author pk
:title title
:body body
:signature (gen-signature! sk msg)}))

(put-str!
(show
(make-issue "This issue has a verified author"
"This is the body of the first issue with a verified author.")))
10 changes: 5 additions & 5 deletions docs/source/guide/Testing.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Testing
=======

Radicle provides a ``:test`` macro which allows you to write tests next
Radicle provides a ``test`` macro which allows you to write tests next
to your code.

.. code-block:: radicle

(def not
(fn [x] (if x #f #t)))

(:test "not"
(test "not"
[ (not #t) ==> #f ]
[ (not #f) ==> #t ]
)
Expand All @@ -24,7 +24,7 @@ Each test definition consists of a test name and a list of steps

.. code-block:: radicle

(:test "my test" step1 step2 ...)
(test "my test" step1 step2 ...)

Each step is a vector triple with the symbol ``==>`` in the middle. For
example
Expand Down Expand Up @@ -53,7 +53,7 @@ environment that tests steps run in.

.. code-block:: radicle

(:test "with setup"
(test "with setup"
[ :setup (do
(def foo 5)
)]
Expand All @@ -70,6 +70,6 @@ steps.
Running Tests
-------------

All tests defined with the ``:test`` macro are collected in the ``tests``
All tests defined with the ``test`` macro are collected in the ``tests``
reference. Tests can be executed using the ``run-all`` function from
the ``prelude/test`` module.
Loading