A peer-to-peer stack for code collaboration
Replicate app (#454)
Julian Arni816dc8297116
diff --git a/bin/rad-replicate b/bin/rad-replicatenew file mode 100755index 0000000..093cb24--- /dev/null+++ b/bin/rad-replicate@@ -0,0 +1,73 @@+#!/usr/bin/env radicle++(load! (find-module-file! "prelude.rad"))+(load! (find-module-file! "monadic/project.rad"))++(import prelude/strings '[reverse-string] :unqualified)++(def default-url+ "Default replication URL"+ "http://replicate.radicle.xyz")++(def help+ (string-append+ "rad-replicate - Replicate radicle projects.++ This can be used to make your machines more available (e.g., readable when+ you are offline, or writable and readable if you are behind a firewall).++ Usage:+ rad-replicate [URL]++ If not provided, the URL defaults to " default-url))++(def append-slash-if-missing+ (fn [str]+ (if (ends-with? str "/")+ str+ (string-append str "/"))))++(def get-machines+ "Gets all machines in the project, including the project itself but excluding git."+ (fn []+ (def proj (get-project-url!))+ (def others (list-rsms! proj))+ (def otherss (filter (fn [x] (not (eq? (lookup :type x) :rad-repo))) others))+ (cons proj (map (fn [x] (lookup :id x)) otherss))))++(def replicate+ "Replicates the given machine."+ (fn [murl machine]+ (def url-base+ (match murl+ :nothing default-url+ [:just 'x] x))+ (def url+ (string-append+ (append-slash-if-missing url-base)+ "v0/machines/"+ machine+ "/query"))+ (match (process-with-stdout-stderr-exitcode!+ "curl"+ ["-s" url "-d" "{ \"expression\": (+ 3 2) }" "-H" "Content-Type: application/json"]+ "")+ [_ _ :ok] :ok+ [_ _ _ ] (do+ (put-str! (string-append "Machine " machine ": error reaching replicator"))+ (exit! 1)))))++(def replicate-all+ (fn [murl]+ (map (fn [x] (replicate murl x)) (get-machines))))++(def args (get-args!))+(machine/catch-daemon!+ (fn []+ (match args+ ["help"] (put-str! help)+ ["-h"] (put-str! help)+ ["--help"] (put-str! help)+ ['url] (replicate-all [:just url])+ [] (replicate-all :nothing)+ )))diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex 7dee4e8..7718e27 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -760,6 +760,16 @@ Returns a string consisting of the results of applying ``f`` to eachcharacter of ``xs``. Throws a type error if ``f`` returns somethingother than a string+``(reverse-string str)``+~~~~~~~~~~~~~~~~~~~~~~~~++Reverses ``str``. E.g.: ``(reverse-string "abc")`` == ``"cba"``.++``(ends-with? str substr)``+~~~~~~~~~~~~~~~~~~~~~~~~~~~++True if ``str`` ends with ``substr``+``prelude/dict``----------------diff --git a/rad/prelude/strings.rad b/rad/prelude/strings.radindex 5950092..71ab125 100644--- a/rad/prelude/strings.rad+++ b/rad/prelude/strings.rad@@ -1,7 +1,7 @@{:module 'prelude/strings:doc "String manipulation functions."- :exports '[intercalate unlines unwords split-by words lines map-string]- }+ :exports '[intercalate unlines unwords split-by words lines map-string+ reverse-string ends-with?] }(import prelude/basic :unqualified)(import prelude/patterns :unqualified)@@ -123,3 +123,22 @@[ (map-string (fn [x] "A") "") ==> ""][ (map-string (fn [x] "") "foo") ==> ""][ (catch 'type-error (map-string (fn [x] 2) "foo") (fn [x] :caught)) ==> :caught])++(def reverse-string+ "Reverses `str`. E.g.: `(reverse-string \"abc\")` == `\"cba\"`."+ (fn [str]+ (def g (fn [a b] (string-append b a)))+ (foldl-string g "" str)))++(:test "reverse-string"+ [ (reverse-string "abc") ==> "cba" ]+)++(def ends-with?+ "True if `str` ends with `substr`"+ (fn [str substr]+ (starts-with? (reverse-string str) substr)))++(:test "ends-with?"+ [ (ends-with? "abc" "c") ==> #t]+)