11import axios from "axios"
22import { getUser , getWorkspaces , updateWorkspaceVersion } from "coder/site/src/api/api"
3- import { Workspace } from "coder/site/src/api/typesGenerated"
3+ import { Workspace , WorkspaceAgent } from "coder/site/src/api/typesGenerated"
44import * as vscode from "vscode"
55import { Remote } from "./remote"
66import { Storage } from "./storage"
@@ -104,6 +104,7 @@ export class Commands {
104104 public async open ( ...args : string [ ] ) : Promise < void > {
105105 let workspaceOwner : string
106106 let workspaceName : string
107+ let folderPath : string | undefined
107108
108109 if ( args . length === 0 ) {
109110 const quickPick = vscode . window . createQuickPick ( )
@@ -156,9 +157,20 @@ export class Commands {
156157 }
157158 workspaceOwner = workspace . owner_name
158159 workspaceName = workspace . name
160+
161+ // TODO: multiple agent support
162+ const agents = workspace . latest_build . resources . reduce ( ( acc , resource ) => {
163+ return acc . concat ( resource . agents || [ ] )
164+ } , [ ] as WorkspaceAgent [ ] )
165+
166+ if ( agents . length === 1 ) {
167+ folderPath = agents [ 0 ] . expanded_directory
168+ }
159169 } else {
160170 workspaceOwner = args [ 0 ]
161171 workspaceName = args [ 1 ]
172+ // workspaceAgent is reserved for args[2], but multiple agents aren't supported yet.
173+ folderPath = args [ 3 ]
162174 }
163175
164176 // A workspace can have multiple agents, but that's handled
@@ -171,39 +183,46 @@ export class Commands {
171183 newWindow = false
172184 }
173185
174- const output : {
175- workspaces : { folderUri : vscode . Uri ; remoteAuthority : string } [ ]
176- } = await vscode . commands . executeCommand ( "_workbench.getRecentlyOpened" )
177- const opened = output . workspaces . filter (
178- // Filter out `/` since that's added below.
179- ( opened ) => opened . folderUri ?. authority === remoteAuthority ,
180- )
181- if ( opened . length > 0 ) {
182- let selected : ( typeof opened ) [ 0 ]
186+ // If a folder isn't specified, we can try to open a recently opened folder.
187+ if ( ! folderPath ) {
188+ const output : {
189+ workspaces : { folderUri : vscode . Uri ; remoteAuthority : string } [ ]
190+ } = await vscode . commands . executeCommand ( "_workbench.getRecentlyOpened" )
191+ const opened = output . workspaces . filter (
192+ // Filter out `/` since that's added below.
193+ ( opened ) => opened . folderUri ?. authority === remoteAuthority ,
194+ )
195+ if ( opened . length > 0 ) {
196+ let selected : ( typeof opened ) [ 0 ]
183197
184- if ( opened . length > 1 ) {
185- const items : vscode . QuickPickItem [ ] = opened . map ( ( folder ) : vscode . QuickPickItem => {
186- return {
187- label : folder . folderUri . path ,
198+ if ( opened . length > 1 ) {
199+ const items : vscode . QuickPickItem [ ] = opened . map ( ( folder ) : vscode . QuickPickItem => {
200+ return {
201+ label : folder . folderUri . path ,
202+ }
203+ } )
204+ const item = await vscode . window . showQuickPick ( items , {
205+ title : "Select a recently opened folder" ,
206+ } )
207+ if ( ! item ) {
208+ return
188209 }
189- } )
190- const item = await vscode . window . showQuickPick ( items , {
191- title : "Select a recently opened folder" ,
192- } )
193- if ( ! item ) {
194- return
210+ selected = opened [ items . indexOf ( item ) ]
211+ } else {
212+ selected = opened [ 0 ]
195213 }
196- selected = opened [ items . indexOf ( item ) ]
197- } else {
198- selected = opened [ 0 ]
214+
215+ folderPath = selected . folderUri . path
199216 }
217+ }
200218
219+ if ( folderPath ) {
201220 await vscode . commands . executeCommand (
202221 "vscode.openFolder" ,
203222 vscode . Uri . from ( {
204223 scheme : "vscode-remote" ,
205224 authority : remoteAuthority ,
206- path : selected . folderUri . path ,
225+ path : folderPath ,
207226 } ) ,
208227 // Open this in a new window!
209228 newWindow ,
0 commit comments