Skip to content

Commit

Permalink
Request missing parameters for add-contact command
Browse files Browse the repository at this point in the history
  • Loading branch information
kwannoel committed Sep 16, 2021
1 parent 311b807 commit 20c751b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
21 changes: 15 additions & 6 deletions cli/contacts.ss
Expand Up @@ -6,6 +6,7 @@
:clan/poo/brace :clan/poo/cli :clan/poo/io :clan/poo/mop :clan/poo/object :clan/poo/type
(only-in :clan/poo/number Nat)
:mukn/ethereum/cli :mukn/ethereum/hex :mukn/ethereum/ethereum :mukn/ethereum/known-addresses
:mukn/glow/cli/utils
(only-in ./identities Identity)
(rename-in ../contacts/db (add-contact add-contact.db) (list-contacts list-contacts.db)))

Expand Down Expand Up @@ -58,12 +59,20 @@
getopt: (make-options
[(option 'name "-N" "--name" help: "name of contact")]
[]
[options/contacts]))
(unless name (error "missing name"))
(def cid (add-contact.db name []))
(if json
(displayln (string<-json (hash (cid cid) (name name))))
(displayln "Added contact " name ", cid " cid)))
[options/contacts options/help]))
;; TODO: Provide some way for entrypoint to introspect
;; options, maybe via gerbil-poo?
;; e.g. (.@ self options)
(def options
(hash
(name name)
(json json)))
;; NOTE: Added scope to avoid `name` being bound to #!void during macro expansion.
(let ((name (get-or-ask options 'name (lambda () (ask-string "Enter contact name: "))))
(cid (add-contact.db name [])))
(if json
(displayln (string<-json (hash (cid cid) (name name))))
(displayln "Added contact " name ", cid " cid))))

(define-entry-point (remove-contact
cid: (cid #f)
Expand Down
20 changes: 2 additions & 18 deletions cli/interaction.ss
Expand Up @@ -15,7 +15,8 @@
:mukn/glow/runtime/program :mukn/glow/runtime/terminal-codes :mukn/glow/runtime/glow-path
(only-in :mukn/glow/compiler/alpha-convert/alpha-convert init-syms)
:mukn/glow/compiler/passes :mukn/glow/compiler/multipass :mukn/glow/compiler/syntax-context
:mukn/glow/cli/contacts :mukn/glow/cli/identities)
:mukn/glow/cli/contacts :mukn/glow/cli/identities
./utils)

(def (ask-option name options)
(def options-count (length options))
Expand Down Expand Up @@ -50,12 +51,6 @@
(displayln FAIL "Invalid selection\n" END)
(ask-option name options)))))

(def (ask-string name)
(display-prompt name)
(def result (read-line))
(displayln)
result)

(def (ask-number default-number: (default-number #f) prompt)
(display-prompt prompt)
(def input (read-line))
Expand Down Expand Up @@ -162,13 +157,6 @@
(displayln)
(force-output))

(def (get-or-ask options option ask-function)
(if-let (option-value (hash-get options option))
option-value
(let (input (apply ask-function []))
(hash-put! options option input)
input)))

(def (ask-role options role-names)
(get-or-ask options
'role
Expand Down Expand Up @@ -254,10 +242,6 @@
;; more ergonomic syntax than JSON, like --param foo=bar. We want to be
;; able to specify this mutliple times, which will require upstream
;; changes in gerbil's getopt.
(option 'params "-P" "--params" default: #f
help: "contract parameters as JSON")
(option 'participants "-p" "--participants" default: #f
help: "participant mapping as JSON")
(option 'interaction "-I" "--interaction" default: #f
help: "path and name of interaction")
(option 'identity "-M" "--my-identity" default: #f
Expand Down
23 changes: 23 additions & 0 deletions cli/utils.ss
@@ -0,0 +1,23 @@
(export #t)
;; TODO the contents of this file should be upstreamed to gerbil-utils/cli
;; Not upstreamed yet because there is a dependency on
;; :glow/runtime/terminal-codes (which should be upstreamed eventually as well).
(import :clan/base :gerbil/gambit/ports :mukn/glow/runtime/terminal-codes)

(def (display-prompt name)
(displayln CYAN name)
(display (string-append "> " END))
(force-output))

(def (ask-string name)
(display-prompt name)
(def result (read-line))
(displayln)
result)

;; If option is undefined, request it
(def (get-or-ask options option ask-function)
(or (hash-get options option)
(let (input (apply ask-function []))
(hash-put! options option input)
input)))

0 comments on commit 20c751b

Please sign in to comment.