Skip to content

Commit 3dea9f4

Browse files
committed
bugfix: defer gh codespaces list until needed
My startup invokes `(codespaces-setup)` to enable the TRAMP type, but that fails if the gh command line's authentication is not working -- say, the token it uses has been expired. This change defers the check to on-demand, validating it when the codespaces are listed for the first time.
1 parent 07b9b2f commit 3dea9f4

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

codespaces.el

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,37 @@ When this is nil, the default of '/workspaces/<repo-name>' is used."
5353
:group 'codespaces
5454
:type 'string)
5555

56+
(defvar codespaces--validated nil
57+
"Whether the gh CLI has been validated for codespaces access.")
58+
59+
(defun codespaces--validate-gh ()
60+
"Validate that `gh' is available and properly configured.
61+
This check is performed lazily on first use rather than at setup time."
62+
(unless codespaces--validated
63+
(with-current-buffer (get-buffer-create "*codespaces-output*")
64+
(special-mode))
65+
(unless (executable-find "gh")
66+
(user-error "Could not find `gh' program in your PATH"))
67+
(unless (featurep 'json)
68+
(user-error "Emacs JSON support not available; your Emacs is too old"))
69+
(let ((status
70+
(let ((inhibit-read-only t))
71+
(shell-command
72+
(if (eq system-type 'windows-nt)
73+
"gh codespace list 1>NUL"
74+
"gh codespace list 1>/dev/null")
75+
nil "*codespaces-output*"))))
76+
(unless (zerop status)
77+
(user-error
78+
(concat "Command `gh codespace list` failed ... "
79+
"[See *codespaces-output* buffer for details]"))))
80+
(setq codespaces--validated t)))
81+
5682
(defun codespaces-setup ()
57-
"Set up the ghcs tramp-method. Should be called after requiring this package."
83+
"Set up the ghcs tramp-method. Should be called after requiring this package.
84+
This registers the TRAMP method without validating gh CLI availability,
85+
allowing for faster startup. Validation happens lazily on first use."
5886
(interactive)
59-
(with-current-buffer (get-buffer-create "*codespaces-output*")
60-
(special-mode))
61-
(unless (executable-find "gh")
62-
(user-error "Could not find `gh' program in your PATH"))
63-
(unless (featurep 'json)
64-
(user-error "Emacs JSON support not available; your Emacs is too old"))
65-
(let ((status
66-
(let ((inhibit-read-only t))
67-
(shell-command
68-
(if (eq system-type 'windows-nt)
69-
"gh codespace list 1>NUL"
70-
"gh codespace list 1>/dev/null")
71-
nil "*codespaces-output*"))))
72-
(unless (zerop status)
73-
(user-error
74-
(concat "Command `gh codespace list` failed ... "
75-
"[See *codespaces-output* buffer for details]"))))
7687
(let ((ghcs (assoc "ghcs" tramp-methods))
7788
(ghcs-methods '((tramp-login-program "gh")
7889
(tramp-login-args (("codespace") ("ssh") ("-c") ("%h")))
@@ -143,6 +154,7 @@ When this is nil, the default of '/workspaces/<repo-name>' is used."
143154

144155
(defun codespaces--all-codespaces ()
145156
"Fetch all user codespaces by executing `gh'."
157+
(codespaces--validate-gh)
146158
(let ((gh-invocation "gh codespace list --json name,displayName,repository,state,gitStatus,lastUsedAt"))
147159
(codespaces--locally
148160
(codespaces--build-table (json-parse-string (shell-command-to-string gh-invocation))))))

0 commit comments

Comments
 (0)