Skip to content

Commit 86db4e5

Browse files
committed
Add to create a Codespace from user input
1 parent 542c029 commit 86db4e5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ And to disable TRAMP's default ControlMaster settings in your Emacs config, add:
7676
- =codespaces-connect= brings up a list of codespaces, and upon selection opens a Dired buffer in =/workspaces= (the default Codespaces location).
7777
- =codespaces-start= brings up a list of inactive codespaces and upon selection spawns a task that starts the selected codespace.
7878
- =codespaces-stop= does the same but for stopping active codespaces.
79+
- =codespaces-create= prompts for a repository, branch, and machine to then create a Codespace. Default branch is fetched via gh.
7980

8081
* Missing features
8182
- Completion should sort codespaces by most-recently-used.

codespaces.el

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,43 @@ allowing for faster startup. Validation happens lazily on first use."
226226
(cl-loop for v being the hash-values of (codespaces--all-codespaces)
227227
collect (list nil (codespaces-space-name v))))
228228

229+
(defun codespaces--get-default-branch (repo)
230+
"Return the default branch for REPO or signal an error if not found."
231+
(let* ((null-device (if (eq system-type 'windows-nt) "NUL" "/dev/null"))
232+
(command (format "gh repo view %s --json defaultBranchRef -q .defaultBranchRef.name 2>%s"
233+
repo null-device))
234+
(branch (shell-command-to-string command)))
235+
(if (string-empty-p branch)
236+
(user-error "Error getting default branch. Likely, no repository exists or it's inaccessible")
237+
(string-trim branch))))
238+
239+
(defun codespaces--create-from-args (repo branch machine)
240+
"Create a codespace for args REPO BRANCH and MACHINE."
241+
(message "Creating codespace...")
242+
(let ((status
243+
(codespaces--locally
244+
(let ((inhibit-read-only t))
245+
(shell-command
246+
(format "gh codespace create --machine %s --repo %s --branch %s"
247+
machine repo branch)
248+
nil "*codespaces-output*")))))
249+
(if (zerop status)
250+
(message "Codespace created successfully!")
251+
(user-error "Command `gh codespace create` failed... [See *codespaces-output* buffer for details]"))))
252+
229253
;;; Public interface
230254

255+
(defun codespaces-create ()
256+
"Prompt for repository, branch, and machine type to create a Codespace."
257+
(interactive)
258+
(let* ((repo (read-string "Repository (user/repo): "))
259+
(branch (read-string "Branch: " (or (codespaces--get-default-branch repo) "main")))
260+
(machine (read-string "Machine type: " "basicLinux32gb")))
261+
(when (string-empty-p repo)
262+
(user-error "Repository must not be empty"))
263+
(message "Creating codespace...")
264+
(codespaces--create-from-args repo branch machine)))
265+
231266
(defun codespaces-stop ()
232267
"Stop a codespace chosen by `completing-read'."
233268
(interactive)

0 commit comments

Comments
 (0)