@@ -13,18 +13,7 @@ type SandboxContext = GlobalOptions & {
1313export const sandboxListCommand = new Command < SandboxContext > ( )
1414 . description ( "List all sandboxes in an organization" )
1515 . action ( async ( options ) => {
16- const configContent = await readConfig ( Deno . cwd ( ) , options . config ) ;
17- let { org } = getAppFromConfig ( configContent ) ;
18- org ??= options . org ;
19-
20- const orgAndApp = await withApp (
21- options . debug ,
22- options . endpoint ,
23- false ,
24- org ,
25- null ,
26- ) ;
27-
16+ const org = await ensureOrg ( options ) ;
2817 const client = createTrpcClient ( options . debug , options . endpoint ) ;
2918
3019 const list : Array < {
@@ -33,9 +22,7 @@ export const sandboxListCommand = new Command<SandboxContext>()
3322 created_at : Date ;
3423 stopped_at : Date | null ;
3524 // deno-lint-ignore no-explicit-any
36- } > = await ( client . sandboxes as any ) . list . query ( {
37- org : orgAndApp . org ,
38- } ) ;
25+ } > = await ( client . sandboxes as any ) . list . query ( { org } ) ;
3926
4027 let createdAtHeaderLength = 0 ;
4128 let statusHeaderLength = 0 ;
@@ -95,29 +82,18 @@ export const sandboxKillCommand = new Command<SandboxContext>()
9582 . description ( "Kill a running sandbox" )
9683 . arguments ( "<sandbox-id:string>" )
9784 . action ( async ( options , sandboxId ) => {
98- const configContent = await readConfig ( Deno . cwd ( ) , options . config ) ;
99- let { org } = getAppFromConfig ( configContent ) ;
100- org ??= options . org ;
101-
102- const orgAndApp = await withApp (
103- options . debug ,
104- options . endpoint ,
105- false ,
106- org ,
107- null ,
108- ) ;
109-
85+ const org = await ensureOrg ( options ) ;
11086 const client = createTrpcClient ( options . debug , options . endpoint ) ;
11187
11288 // deno-lint-ignore no-explicit-any
11389 const cluster = await ( client . sandboxes as any ) . findHostname . query ( {
114- org : orgAndApp . org ,
90+ org,
11591 sandboxId,
11692 } ) ;
11793
11894 // deno-lint-ignore no-explicit-any
11995 const res = await ( client . sandboxes as any ) . kill . mutate ( {
120- org : orgAndApp . org ,
96+ org,
12197 sandboxId,
12298 clusterHostname : [ cluster . hostname ] ,
12399 } ) ;
@@ -131,29 +107,15 @@ export const sandboxSshCommand = new Command<SandboxContext>()
131107 . description ( "SSH into a running sandbox" )
132108 . arguments ( "<sandbox-id:string>" )
133109 . action ( async ( options , sandboxId ) => {
134- const configContent = await readConfig ( Deno . cwd ( ) , options . config ) ;
135- let { org } = getAppFromConfig ( configContent ) ;
136- org ??= options . org ;
137-
138- const orgAndApp = await withApp (
139- options . debug ,
140- options . endpoint ,
141- false ,
142- org ,
143- null ,
144- ) ;
145-
110+ const org = await ensureOrg ( options ) ;
146111 const client = createTrpcClient ( options . debug , options . endpoint ) ;
147112
148113 const [ cluster , token ] = await Promise . all ( [
149114 // deno-lint-ignore no-explicit-any
150- ( client . sandboxes as any ) . findHostname . query ( {
151- org : orgAndApp . org ,
152- sandboxId,
153- } ) ,
115+ ( client . sandboxes as any ) . findHostname . query ( { org, sandboxId } ) ,
154116 // deno-lint-ignore no-explicit-any
155117 ( client . orgs as any ) . accessTokens . create . mutate ( {
156- org : orgAndApp . org ,
118+ org,
157119 description : "$$DENO_DEPLOY_CLI_SSH_TOKEN$$" ,
158120 expiresAt : new Date ( Date . now ( ) + 1000 * 60 * 60 ) . toISOString ( ) ,
159121 } ) ,
@@ -193,10 +155,28 @@ export const sandboxSshCommand = new Command<SandboxContext>()
193155 }
194156 } ) ;
195157
158+ async function ensureOrg ( options : SandboxContext ) {
159+ let { org } = getAppFromConfig ( await readConfig ( Deno . cwd ( ) , options . config ) ) ;
160+ org ??= options . org ;
161+
162+ return ( await withApp (
163+ options . debug ,
164+ options . endpoint ,
165+ false ,
166+ org ,
167+ null ,
168+ ) ) . org ;
169+ }
170+
196171/**
197172 * Format duration in ms to human readable string
198173 *
199- * e.g. 1d 2h 3m 4s 500ms
174+ * @example
175+ * 86400000 => 1d
176+ * 7200000 => 2h
177+ * 180000 => 3m
178+ * 4000 => 4s
179+ * 5 => 5ms
200180 *
201181 * @param ms
202182 */
0 commit comments