Skip to content

Commit 16e5fc2

Browse files
committed
Add machine type selection for codespace creation
- Add `codespaces--get-machine-types` function to retrieve available machine types for a repository. - Add `codespaces--select-machine` function to prompt the user to select a machine type by display name. - Use `codespaces--select-machine` in `codespaces-create`.
1 parent 573e985 commit 16e5fc2

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

codespaces.el

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,34 @@ allowing for faster startup. Validation happens lazily on first use."
251251
(message "Codespace created successfully!")
252252
(user-error "Command `gh codespace create` failed... [See *codespaces-output* buffer for details]"))))
253253

254+
(defun codespaces--get-machine-types (repo)
255+
"Return an alist of machine types for REPO with name as key."
256+
(let* ((output (shell-command-to-string
257+
(format
258+
"gh api -H 'Accept: application/vnd.github+json' \
259+
-H 'X-GitHub-Api-Version: 2022-11-28' /repos/%s/codespaces/machines 2>%s"
260+
repo
261+
codespaces--null-device)))
262+
(machines (json-read-from-string output))
263+
(machine-list
264+
(mapcar (lambda (machine)
265+
(cons (alist-get 'name machine)
266+
(alist-get 'display_name machine)))
267+
(alist-get 'machines machines))))
268+
machine-list))
269+
270+
(defun codespaces--select-machine (repo)
271+
"Prompt user to select a machine type for REPO by display name."
272+
(let* ((machine-alist (codespaces--get-machine-types repo))
273+
(display-alist (mapcar (lambda (entry)
274+
(cons (cdr entry) (car entry)))
275+
machine-alist))
276+
(choice (completing-read
277+
"Select machine type: "
278+
display-alist
279+
nil t)))
280+
(cdr (assoc choice display-alist))))
281+
254282
;;; Public interface
255283

256284
(defun codespaces-create ()
@@ -259,7 +287,7 @@ allowing for faster startup. Validation happens lazily on first use."
259287
(codespaces--validate-gh)
260288
(let* ((repo (read-string "Repository (user/repo): "))
261289
(branch (read-string "Branch: " (or (codespaces--get-default-branch repo) "main")))
262-
(machine (read-string "Machine type: " "basicLinux32gb")))
290+
(machine (codespaces--select-machine repo)))
263291
(when (string-empty-p repo)
264292
(user-error "Repository must not be empty"))
265293
(message "Creating codespace...")

0 commit comments

Comments
 (0)