Skip to content

A peer-to-peer stack for code collaboration

Sys args, open-file, and unify util scripts (#296)

Julian Arnia9e4eb14cee5
diff --git a/bin/rad-issues b/bin/rad-issues
new file mode 100755
index 0000000..d646aef
--- /dev/null
+++ b/bin/rad-issues
@@ -0,0 +1,72 @@
+#!/usr/bin/env radicle
+
+(load! "rad/prelude.rad")
+
+(def help
+ "rad-issues - Radicle issue CLI
+
+ Usage:
+ rad [list|new] <chain-name>
+ rad help
+
+ list - Select from all issues to view or comment on
+ new - Create a new issue in $EDITOR
+ help - Print this help and exit")
+
+(def base-name "http://machine.radicle.xyz/chains")
+
+(def list-issues
+ (fn []
+ (load! "rad/prelude/io-utils.rad")
+ (load! "rad/monadic/issues.rad")
+
+ (def mk-key (fn [v]
+ (def key (nth 0 v))
+ (def val (nth 1 v))
+ [(string-append (show key) " - " (lookup :title val)) val]))
+ (def iss (dict-from-seq (map mk-key (seq (list-issues)))))
+ (def selected (fzf-select-by-key! iss))
+ (def pretty-comment (fn [c]
+ (string-append
+ (lookup :github-username c) "[" (lookup :created-at c) "]: "
+ (lookup :body c)
+ "\n\n")))
+ (def pretty-issue (fn [i]
+ (string-append
+ "Issue " (show (lookup :number i)) " : " (lookup :title i)
+ "\nDescription: " (lookup :body i)
+ "\nState: " (if (eq? (lookup :state i) :open) "open" "closed")
+ "\nLabels: " (show (lookup :labels i))
+ "\nComments:\n\n\n" (unlines (map pretty-comment (lookup :comments i)))
+ )))
+ (put-str! (pretty-issue selected))))
+
+(def new-issue
+ (fn []
+ (load! "rad/prelude/io-utils.rad")
+ (load! "rad/monadic/issues.rad")
+ (def template
+";; Issue template. An empty or invalid value will abort.
+{:title \"Pick a title\"
+ :body \"Pick a body\"
+ :labels []
+}
+")
+ (def iss (read (edit-in-editor! template)))
+ (create-issue!
+ (insert
+ :state :open
+ (insert
+ :comments []
+ (insert
+ :created-at (now!)
+ iss))))))
+
+(def args (get-args!))
+(match args
+ ["list"] (list-issues)
+ ["new" ] (new-issue)
+ ["help"] (put-str! help)
+ _ (do
+ (put-str! (string-append "Unrecognized command" (show args)))
+ (put-str! help)))
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 4788109..9736350 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -434,6 +434,12 @@ Given an elliptic curve, generates a cryptographic key-pair. Use
Given a private key and a message (a string), generates a cryptographic
signature for the message.
+``get-args!``
+~~~~~~~~~~~~~
+
+Returns the list of the command-line arguments the script was called
+with
+
``put-str!``
~~~~~~~~~~~~
@@ -461,6 +467,12 @@ Reads the contents of a file and returns it as a string.
Read a single line from a handle. Returns the string read, or the
keyword ``:eof`` if an EOF is encountered.
+``open-file!``
+~~~~~~~~~~~~~~
+
+Open file in the specified mode (``:read``, ``:write``, ``:append``,
+``:read-write``).
+
``close-handle!``
~~~~~~~~~~~~~~~~~
@@ -700,11 +712,23 @@ Read code (as data) from a file. Returns a vector of expressions
Like ``shell!``, but captures the stdout and returns it.
+``(shell-no-stdin! command to-write)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Like ``shell!``, but inherits stdin. WARNING: using ``shell!`` with
+unsanitized user input is a security hazard! Example:
+``(shell-no-stdin! "ls -Glah")``.
+
``(process-with-stdout! command args to-write)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Like ``process!``, but captures stdout.
+``(write-file! filename contents)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Write ``contents`` to file ``filename``.
+
``prelude/bool``
----------------
diff --git a/exe/Doc.hs b/exe/Doc.hs
index de4f788..e0a9a00 100644
--- a/exe/Doc.hs
+++ b/exe/Doc.hs
@@ -26,7 +26,7 @@ run f = do
let extensions = def { readerExtensions = githubMarkdownExtensions }
pand <- runIOorExplode $ readMarkdown extensions txt
let code = T.append "(load! \"rad/prelude.rad\")" (getCode pand)
- res <- runLang replBindings $ interpretMany (toS f) $ code
+ res <- runLang (replBindings []) $ interpretMany (toS f) $ code
case res of
(Left err, _) -> die . toS $ "Error: " ++ show err
_ -> pure ()
diff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hs
index d63f221..c1b5c0b 100644
--- a/exe/RadicleExe.hs
+++ b/exe/RadicleExe.hs
@@ -17,7 +17,7 @@ main = do
then do
src <- getContents
let prog = interpretMany (toS $ sourceFile opts') src
- bindings <- createBindings
+ bindings <- createBindings (toS <$> scriptArgs opts')
(result, _state) <- runLang bindings prog
case result of
Left (LangError _ Exit) -> pure ()
@@ -25,17 +25,18 @@ main = do
exitWith (ExitFailure 1)
Right v -> putPrettyAnsi v
else do
- src <- readSource (sourceFile opts')
+ src <- ignoreShebang <$> readSource (sourceFile opts')
hist <- case histFile opts' of
Nothing -> getHistoryFile
Just h -> pure h
- bindings <- createBindings
+ bindings <- createBindings (toS <$> scriptArgs opts')
repl (Just hist) (toS $ sourceFile opts') src bindings
where
allOpts = info (opts <**> helper)
( fullDesc
<> progDesc radDesc
<> header "The radicle intepreter"
+ <> noIntersperse
)
readSource :: String -> IO Text
@@ -58,6 +59,7 @@ radDesc
data Opts = Opts
{ sourceFile :: FilePath
, histFile :: Maybe FilePath
+ , scriptArgs :: [String]
}
opts :: Parser Opts
@@ -76,9 +78,10 @@ opts = Opts
<> "where $DIR is $XDG_DATA_HOME (%APPDATA% on Windows "
<> "if that is set, or else ~/.local/share."
)
- ))
+ ))
+ <*> many (strArgument mempty)
-createBindings :: (MonadIO m, ReplM m) => IO (Bindings (PrimFns m))
-createBindings = do
+createBindings :: (MonadIO m, ReplM m) => [Text] -> IO (Bindings (PrimFns m))
+createBindings scriptArgs' = do
storage <- createHttpStoragePrimFns
- pure $ addPrimFns (replPrimFns <> storage) pureEnv
+ pure $ addPrimFns (replPrimFns scriptArgs' <> storage) pureEnv
diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hs
index 5b3c488..6103c46 100644
--- a/exe/ReferenceDoc.hs
+++ b/exe/ReferenceDoc.hs
@@ -38,7 +38,7 @@ main = do
<> "(file-module! \"rad/prelude/test-eval.rad\") (import prelude/test-eval '[eval tests] :unqualified)"
<> foldMap (\m -> "(file-module! \"rad/" <> m <> ".rad\")") (modules content)
<> "(get-current-env))")
- replBindings
+ (replBindings [])
let res = res_ `lPanic` "Error running the prelude."
let env = bindingsEnv $ bindingsFromRadicle res `lPanic` "Couldn't convert radicle state."
let rst = runPure (writeRST Default.def $ Pandoc nullMeta (doc content env)) `lPanic` "Couldn't generate RST"
diff --git a/rad/prelude/io-utils.rad b/rad/prelude/io-utils.rad
index abad727..c71728c 100644
--- a/rad/prelude/io-utils.rad
+++ b/rad/prelude/io-utils.rad
@@ -1,8 +1,8 @@
{:module 'prelude/io-utils
:doc "IO-related utilities"
- :exports '[fzf-select-by-key!]}
+ :exports '[fzf-select-by-key! edit-in-editor!]}
-(import prelude/io '[shell-with-stdout!] :unqualified)
+(import prelude/io '[shell-with-stdout! write-file!] :unqualified)
(import prelude/strings '[unlines] :unqualified)
(def fzf-select-by-key!
@@ -12,3 +12,12 @@
(fn [dict]
(def result (process-with-stdout! "fzf" [] (string-append (unlines (keys dict)) "\n")))
(lookup (string-replace "\n" "" (nth 0 result)) dict)))
+
+(def edit-in-editor!
+ "Open `$EDITOR` on a file prepopulated with `orig`. Returns the contents of the edited file when the editor exits."
+ (fn [orig]
+ (def file (unlines (shell-with-stdout! "mktemp" "")))
+ (put-str! file)
+ (write-file! file orig)
+ (shell-no-stdin! (string-append "$EDITOR " file) "")
+ (read-file! file)))
diff --git a/rad/prelude/io.rad b/rad/prelude/io.rad
index 5279e9e..2af5f28 100644
--- a/rad/prelude/io.rad
+++ b/rad/prelude/io.rad
@@ -1,7 +1,7 @@
{:module 'prelude/io
:doc "Some basic I/O functions."
:exports '[print! shell! process! read-line! send-code! read-code!
- shell-with-stdout! process-with-stdout!
+ shell-with-stdout! shell-no-stdin! process-with-stdout! write-file!
]}
(import prelude/patterns :unqualified)
@@ -26,6 +26,13 @@
(fn [x]
(put-str! (show x))))
+(def write-file!
+ "Write `contents` to file `filename`."
+ (fn [filename contents]
+ (def hdl (open-file! filename :write))
+ (write-handle! hdl contents)
+ (close-handle! hdl)))
+
(def-rec read-contents-handle!
"Read all the contents of a handle. A *vector* of strings is returned, one
string for each line of output."
@@ -60,6 +67,24 @@ Example: `(shell! \"ls -Glah\" \"\")`. "
[ (shell! "false" "") ==> [:error 1] ]
)
+(def shell-no-stdin!
+ "Like `shell!`, but inherits stdin.
+WARNING: using `shell!` with unsanitized user input is a security hazard!
+Example: `(shell-no-stdin! \"ls -Glah\")`. "
+ (fn [command to-write]
+ (def cp
+ { :cmdspec [:shell command]
+ :stdin :inherit
+ :stdout :inherit
+ :stderr :inherit
+ })
+ (match (system! cp)
+ {:stdin :nothing
+ :stdout :nothing
+ :stderr :nothing
+ :proc 'ph
+ } (wait-for-process! ph)
+ _ (throw 'io-error "expected no handles to be open"))))
(def shell-with-stdout!
"Like `shell!`, but captures the stdout and returns it."
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 96ebe16..400d5bd 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -99,11 +99,13 @@ primFns:
- gen-key-pair!
- gen-signature!
# I/O
+- get-args!
- put-str!
- get-line!
- load!
- read-file!
- read-line-handle!
+- open-file!
- close-handle!
- now!
- system!
diff --git a/src/Radicle.hs b/src/Radicle.hs
index 3a1f7c4..3df9557 100644
--- a/src/Radicle.hs
+++ b/src/Radicle.hs
@@ -93,6 +93,7 @@ module Radicle
, ($$)
, kwLookup
, (??)
+ , ignoreShebang
) where
diff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hs
index 57a0695..586fccd 100644
--- a/src/Radicle/Internal/Effects.hs
+++ b/src/Radicle/Internal/Effects.hs
@@ -65,7 +65,7 @@ completion = completeWord Nothing ['(', ')', ' ', '\n'] go
where
-- Any type for first param will do
bnds :: Bindings (PrimFns (InputT IO))
- bnds = replBindings
+ bnds = replBindings []
go s = pure $ fmap simpleCompletion
$ filter (s `isPrefixOf`)
@@ -73,12 +73,18 @@ completion = completeWord Nothing ['(', ')', ' ', '\n'] go
$ (Map.keys . fromEnv $ bindingsEnv bnds)
<> Map.keys (getPrimFns $ bindingsPrimFns bnds)
-replBindings :: ReplM m => Bindings (PrimFns m)
-replBindings = addPrimFns replPrimFns pureEnv
+replBindings :: ReplM m => [Text] -> Bindings (PrimFns m)
+replBindings sysArgs = addPrimFns (replPrimFns sysArgs) pureEnv
-replPrimFns :: ReplM m => PrimFns m
-replPrimFns = fromList $ allDocs $
- [ ("put-str!"
+replPrimFns :: ReplM m => [Text] -> PrimFns m
+replPrimFns sysArgs = fromList $ allDocs $
+ [ ( "get-args!"
+ , "Returns the list of the command-line arguments the script was called with"
+ , \case
+ [] -> pure . Vec $ fromList (String <$> sysArgs)
+ xs -> throwErrorHere $ WrongNumberOfArgs "get-args!" 0 (length xs)
+ )
+ , ("put-str!"
, "Prints a string."
, oneArg "put-str!" $ \case
(String x) -> do
@@ -174,13 +180,35 @@ replPrimFns = fromList $ allDocs $
Right text -> pure $ String text
v -> throwErrorHere $ TypeError "read-file!" 0 TString v
)
+ , ( "open-file!"
+ , "Open file in the specified mode (`:read`, `:write`, `:append`, `:read-write`)."
+ , twoArg "open-file!" $ \case
+ (String file, Keyword (Ident mode)) -> do
+ mode' <- case mode of
+ "read" -> pure ReadMode
+ "write" -> pure WriteMode
+ "append" -> pure AppendMode
+ "read-write" -> pure ReadWriteMode
+ x -> throwErrorHere
+ . OtherError
+ $ "Expected one of :read, :write, :append, or :read-write."
+ <> "Got: " <> x
+ res <- openFileS file mode'
+ case res of
+ Left e -> throwErrorHere . OtherError $ "open-file!:" <> e
+ Right v -> newHandle v
+ (v, Keyword _) -> throwErrorHere $ TypeError "open-file!" 0 TString v
+ (_, v) -> throwErrorHere $ TypeError "open-file!" 1 TKeyword v
+ )
, ( "load!"
, "Evaluates the contents of a file. Each seperate radicle expression is\
\ `eval`uated according to the current definition of `eval`."
, oneArg "load!" $ \case
String filename -> readFileS filename >>= \case
Left err -> throwErrorHere . OtherError $ "Error reading file: " <> err
- Right text -> interpretMany ("[load! " <> filename <> "]") text
+ Right text -> interpretMany
+ ("[load! " <> filename <> "]")
+ (ignoreShebang text)
v -> throwErrorHere $ TypeError "load!" 0 TString v
)
, ( "gen-key-pair!"
@@ -297,10 +325,17 @@ replPrimFns = fromList $ allDocs $
\ That is, the file is evaluated as if the code was wrapped in `(module ...)`."
, oneArg "file-module!" $ \case
String filename -> do
- t_ <- readFileS filename
+ t_ <- fmap ignoreShebang <$> readFileS filename
t <- hoistEither . first (toLangError . OtherError) $ t_
vs <- readValues ("file-module!: " <> filename) t
createModule vs
v -> throwErrorHere $ TypeError "file-module!" 0 TString v
)
]
+
+-- | Since `#` is not a comment in radicle, we need to explictly remove shebang
+-- lines.
+ignoreShebang :: Text -> Text
+ignoreShebang src = case T.lines src of
+ f:rest -> T.unlines $ if "#!" `T.isPrefixOf` f then rest else f:rest
+ _ -> src
diff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hs
index 9e479d8..b5b87c9 100644
--- a/src/Radicle/Internal/Effects/Capabilities.hs
+++ b/src/Radicle/Internal/Effects/Capabilities.hs
@@ -68,6 +68,7 @@ class (Monad m) => System m where
-- | hGetLine should return Nothing on EOF.
hGetLineS :: Handle -> m (Maybe Text)
hCloseS :: Handle -> m ()
+ openFileS :: Text -> IOMode -> m (Either Text Handle)
instance System m => System (Lang m) where
systemS proc = lift $ systemS proc
@@ -75,6 +76,8 @@ instance System m => System (Lang m) where
hPutStrS a b = lift $ hPutStrS a b
hGetLineS = lift . hGetLineS
hCloseS = lift . hCloseS
+ openFileS f mode = lift $ openFileS f mode
+
instance System IO where
systemS = createProcess
waitForProcessS = waitForProcess
@@ -87,12 +90,16 @@ instance System IO where
hSetBuffering h LineBuffering
Just . toS <$> hGetLine h
hCloseS = hClose
+ openFileS f mode = catch (Right <$> openFile (toS f) mode)
+ (\e -> pure . Left $ show (e :: IOException))
+
instance System m => System (InputT m) where
systemS proc = lift $ systemS proc
waitForProcessS = lift . waitForProcessS
hPutStrS a b = lift $ hPutStrS a b
hGetLineS = lift . hGetLineS
hCloseS = lift . hCloseS
+ openFileS f mode = lift $ openFileS f mode
class (Monad m) => Exit m where
exitS :: m ()
diff --git a/src/Radicle/Internal/Interpret.hs b/src/Radicle/Internal/Interpret.hs
index 11ed601..146e453 100644
--- a/src/Radicle/Internal/Interpret.hs
+++ b/src/Radicle/Internal/Interpret.hs
@@ -64,6 +64,4 @@ interpretMany sourceName src = case parseValues sourceName src of
case lastMay es of
Just e -> pure e
_ -> throwErrorHere
- $ OtherError "InterpretMany should be called with at least one expression."
-
-
+ $ OtherError "interpretMany should be called with at least one expression."
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 3c531a6..0b479fa 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -116,7 +116,7 @@ sourceFiles = do
-- | Bindings with REPL and client stuff mocked.
testBindings :: Bindings (PrimFns (StateT WorldState IO))
-testBindings = addPrimFns storagePrimFns replBindings
+testBindings = addPrimFns storagePrimFns (replBindings [])
-- | Provides @send!@ and @receive!@ functions that store chains in a
-- 'Map' in the 'WorldState'.
@@ -188,3 +188,4 @@ instance System (StateT WorldState IO) where
hPutStrS a b = lift $ hPutStrS a b
hGetLineS = lift . hGetLineS
hCloseS = lift . hCloseS
+ openFileS a b = lift $ openFileS a b
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 90b8093..7a4af63 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -675,6 +675,14 @@ test_repl_primops =
files = Map.singleton "foo.rad" "(def foo 42) (def bar 8)"
res <- runCodeWithFiles files prog
res @?= Right (int 50)
+ , testCase "load! ignores shebangs" $ do
+ let prog = [s|
+ (load! "foo.rad")
+ taxi
+ |]
+ files = Map.singleton "foo.rad" "#!/blah\n(def taxi 1797)"
+ res <- runCodeWithFiles files prog
+ res @?= Right (int 1797)
, testCase "generating and verifying cryptographic signatures works" $ do
let prog = [s|
(def my-keys (gen-key-pair! (default-ecc-curve)))
diff --git a/utils/list-issues b/utils/list-issues
deleted file mode 100755
index 27e95b4..0000000
--- a/utils/list-issues
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/env bash
-
-FILE="$(mktemp)"
-cat << SRC > $FILE
-(load! "rad/prelude.rad")
-(load! "rad/prelude/io-utils.rad")
-(load! "rad/monadic/issues.rad")
-
-(def mk-key (fn [v]
- (def key (nth 0 v))
- (def val (nth 1 v))
- [(string-append (show key) " - " (lookup :title val)) val]))
-(def iss (dict-from-seq (map mk-key (seq (list-issues)))))
-(def selected (fzf-select-by-key! iss))
-(def pretty-comment (fn [c]
- (string-append
- (lookup :github-username c) "[" (lookup :created-at c) "]: "
- (lookup :body c)
- "\n\n")))
-(def pretty-issue (fn [i]
- (string-append
- "Issue " (show (lookup :number i)) " : " (lookup :title i)
- "\nDescription: " (lookup :body i)
- "\nState: " (if (eq? (lookup :state i) :open) "open" "closed")
- "\nLabels: " (show (lookup :labels i))
- "\nComments:\n\n\n" (unlines (map pretty-comment (lookup :comments i)))
-)))
-(put-str! (pretty-issue selected))
-SRC
-
-if [ "$IS_NIX_SHELL" = "true" ]; then
- stack exec --silent radicle --nix-packages fzf -- $FILE
-else
- stack exec --silent radicle -- $FILE
-fi
diff --git a/utils/new-issue b/utils/new-issue
deleted file mode 100755
index 2332cbe..0000000
--- a/utils/new-issue
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/env bash
-
-TEMPLATE="$(mktemp)"
-cat << SRC > $TEMPLATE
-;; Issue template. An empty or invalid value will abort.
-{:title "Pick a title"
- :body "Pick a body"
- :labels []
- :state :open
- :comments []
- :created-at (now!)}
-SRC
-
-SRC_FILE="$(mktemp)"
-cat << SRC > $SRC_FILE
-(load! "rad/monadic/issues.rad")
-(create-issue!
- $(cat $TEMPLATE | vipe))
-SRC
-radicle $SRC_FILE
-