Skip to content

A peer-to-peer stack for code collaboration

1.8 KB
Raw Blame
#!/usr/bin/env radicle

(load! (find-module-file! "prelude.rad"))
(load! (find-module-file! "monadic/project.rad"))

(import prelude/strings '[ends-with?] :unqualified)
(import prelude/http :as 'http)

(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"))
    (catch 'http
        (do (http/json-post! url {:expression "(+ 3 2)"})
            :ok)
      (fn [err]
        (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
          (/cmd-help) (put-str! help)
          ['url] (replicate-all [:just url])
          [] (replicate-all :nothing)
          )))