A peer-to-peer stack for code collaboration
#!/usr/bin/env radicle
(load! (find-module-file! "prelude.rad"))
(file-module! "prelude/io-utils.rad")
(file-module! "prelude/error-messages.rad")
(file-module! "monadic/issue.rad")
(load! (find-module-file! "monadic/patch.rad"))
(file-module! "monadic/project.rad")
(import prelude/strings '[unlines] :unqualified)
(import prelude/error-messages :as 'error)
(import prelude/validation :as 'validation)
(import prelude/cmd-parsing :unqualified)
(import prelude/io :as 'io)
(import prelude/io-utils
'[set-git-config! get-git-config! process-git-with-exit!]
:unqualified)
(import monadic/issue '[create-issue-machine!] :unqualified)
(import monadic/project
'[get-project-url! get-meta! create-project! add-rsm! first-rsm-of-type!]
:unqualified)
(def help
"rad project - Radicle project CLI
Usage:
rad project init
rad project show-id
rad project checkout <PROJECT-ID>
init - Initialize a new project
show-id - Show the project id
checkout - Checkout a project
")
(def prompt-for-metadata!
(fn []
(def name (prompt! "? What's the name of your project: "))
(def desc (prompt! "? Briefly describe the project: "))
{:name name :description desc}))
(def cid-of-empty-repo "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
(def init-git-ipfs-repo
"Creates a key for a repo. Sets the remote to that. Points the IPNS link to
the provided CID. Returns the remote."
(fn [orig-cid]
(def keyname (uuid!))
(def key
(match
(process-with-stdout-stderr-exitcode!
"rad" ["ipfs" "key" "gen" "--type=ed25519" keyname] "")
['stdout _ :ok] (unlines stdout)
['stdout 'stderr _]
(do
(put-str! (error/rad-ipfs-key-gen-failure (unlines stderr)))
(exit! 1))))
(match
(process-with-stdout-stderr-exitcode!
"rad" ["ipfs" "name" "publish" "--key" key orig-cid] "")
['stdout _ :ok] :ok
['stdout 'stderr _]
(do
(put-str! (error/rad-ipfs-name-publish-failure (unlines stderr)))
(exit! 1)))
(def remote (string-append "ipfs://ipns/" key))
remote))
(def ipfs-api-url
"IPFS API url to use."
(match (shell-with-stdout! "echo $RAD_IPFS_API_URL" "")
[""] "http://localhost:9301"
['url] url))
(def config-ipfs-api-url!
"For git-ipfs repositories, set the `ipfs.apiurl` git-config so that the git
remote helper uses the radicle IPFS network."
(fn [origin]
(if (starts-with? origin "ipfs://")
(set-git-config! "ipfs.apiurl" ipfs-api-url)
:ok)))
(def checkout!
"Checkout a project."
(fn [project]
(def name (lookup :name (get-meta! project)))
(def origin (lookup :id (first-rsm-of-type! project :rad-repo)))
(match (process! "test" ["-d" name] "")
:ok (do
(put-str! (error/dir-already-exists name))
(exit! 1))
_ (put-str! name))
(process! "mkdir" [name] "")
(cd! name)
(process-git-with-exit!
["init"]
(error/git-clone-failure origin name))
(config-ipfs-api-url! origin)
(process-git-with-exit!
["remote" "add" "origin" origin]
(error/git-clone-failure origin name))
(process-git-with-exit!
["fetch"]
(error/git-clone-failure origin name))
(process-git-with-exit!
["checkout" "master"]
(error/git-clone-failure origin name))
(set-git-config! "radicle.project-id" project)))
(def setup-repo!
(fn []
(def current-remote (get-git-config! "remote.origin.url"))
(def question
(unlines
(concat
[["? What kind of repository would you like to use?"
"1. New peer-to-peer repository (git-ipfs **EXPERIMENTAL**)"
"2. Add your own remote (e.g.: github / gitlab / ...)"]
(match current-remote
"" []
'url ["3. Remote origin of existing current git repository:"
(string-append " " url)])
])))
(match (prompt! (string-append question "\n"))
"1" (init-git-ipfs-repo cid-of-empty-repo)
"2" (do
(def remote-url (prompt! "Please input the remote git repo URL: "))
(io/process-with-stdout-strict!
"git" ["remote" "add" "origin" remote-url] "")
remote-url)
"3" (if (eq? current-remote "")
(do (put-str! "No current remote origin set; please select 1 or 2.")
(exit! 1))
current-remote)
_ (do (put-str! "You must select either 1 or 2.")
(exit! 1)))))
(def setup-upstream!
"Creates a commit to set the upstream to master, if master does not exist yet."
(fn []
(match (process-with-stdout! "git" ["show-ref" "refs/heads/master"] "")
[] (do
(process-git-with-exit!
["commit" "--allow-empty" "--message" "Radicle Setup"]
(error/upstream-commit-failure))
(process-git-with-exit!
["push" "-u" "origin" "master"]
(error/upstream-push-failure)))
_ :ok)))
(def detect-git!
"Detect if the directory is already a git repository."
(fn []
(match (process! "test" ["-d" ".git"] "")
:ok #t
_ #f)))
(def new-project!
"Create a new project."
(fn []
(get-keys!) ;; exit with error when key file does not exist
(def meta (prompt-for-metadata!))
(def project (create-project! meta))
(if (not (detect-git!))
(process! "git" ["init"] "")
:ok)
(def repo (setup-repo!))
(config-ipfs-api-url! repo)
(def issues (create-issue-machine!))
(put-str! "=> Assembled rad-issue machine")
(def patch (create-patch-machine!))
(put-str! "=> Assembled rad-patch machine")
(put-str! (string-append "=> project id: " project))
(if (eq? "" (get-git-config! "remote.origin.url"))
(do (put-str! (string-append "=> adding \"origin\" remote: " repo))
(process! "git" ["remote" "add" "origin" repo] ""))
:ok)
(setup-upstream!)
(add-rsm! project { :id repo :type :rad-repo })
(add-rsm! project { :id issues :type :rad-issue })
(add-rsm! project { :id patch :type :rad-patch })
(set-git-config! "radicle.project-id" project)
(put-str! "=> project created!")))
(def show-project-id
(fn []
(put-str! (string-append "=> " (get-project-url!)))
(put-str! "=> Share this project id for people to collaborate with you on your project.")))
(def args (get-args!))
(machine/catch-daemon!
(fn []
(match args
(/cmd-0 "init" help) (new-project!)
(/cmd-0 "show-id" help) (show-project-id)
(/cmd-1 "checkout" 'project-id help) (checkout! project-id)
(/cmd-help) (put-str! help)
(/cons 'cmd _) (parse-failure (error/unknown-command cmd) help)
[] (put-str! help))))