- Notifications
You must be signed in to change notification settings - Fork907
feat(cli): support opening devcontainers in vscode#17189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
3735e7e
feat(cli): support opening devcontainers in vscode
DanielleMaywood3632b1b
chore: run 'make gen'
DanielleMaywoodad6ddca
chore: ensure we use unix path
DanielleMaywooda1e95e8
chore: fix oopsie on test
DanielleMaywoodf499bb1
chore: muppet
DanielleMaywood8abf883
chore: filepath from slash
DanielleMaywood9cc49f8
chore: run test on linux only
DanielleMaywood93bf7b7
chore: suggestions
DanielleMaywoode63db0c
chore: replace windowsToUnixPath with filepath.ToSlash
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
145 changes: 127 additions & 18 deletionscli/open.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -42,6 +42,7 @@ func (r *RootCmd) openVSCode() *serpent.Command { | ||
generateToken bool | ||
testOpenError bool | ||
appearanceConfig codersdk.AppearanceConfig | ||
containerName string | ||
) | ||
client := new(codersdk.Client) | ||
@@ -112,27 +113,48 @@ func (r *RootCmd) openVSCode() *serpent.Command { | ||
if len(inv.Args) > 1 { | ||
directory = inv.Args[1] | ||
} | ||
if containerName != ""{ | ||
containers, err := client.WorkspaceAgentListContainers(ctx, workspaceAgent.ID, map[string]string{"devcontainer.local_folder": ""}) | ||
if err != nil { | ||
return xerrors.Errorf("list workspace agent containers: %w", err) | ||
} | ||
var foundContainer bool | ||
for _, container := range containers.Containers { | ||
if container.FriendlyName != containerName { | ||
continue | ||
} | ||
foundContainer = true | ||
if directory == "" { | ||
localFolder, ok := container.Labels["devcontainer.local_folder"] | ||
if !ok { | ||
return xerrors.New("container missing `devcontainer.local_folder` label") | ||
} | ||
directory, ok = container.Volumes[localFolder] | ||
if !ok { | ||
return xerrors.New("container missing volume for `devcontainer.local_folder`") | ||
} | ||
} | ||
break | ||
} | ||
if !foundContainer { | ||
return xerrors.New("no container found") | ||
} | ||
} | ||
directory, err = resolveAgentAbsPath(workspaceAgent.ExpandedDirectory, directory, workspaceAgent.OperatingSystem, insideThisWorkspace) | ||
if err != nil { | ||
return xerrors.Errorf("resolve agent path: %w", err) | ||
} | ||
var token string | ||
// We always set the token if we believe we can open without | ||
// printing the URI, otherwise the token must be explicitly | ||
// requested as it will be printed in plain text. | ||
@@ -145,10 +167,31 @@ func (r *RootCmd) openVSCode() *serpent.Command { | ||
if err != nil { | ||
return xerrors.Errorf("create API key: %w", err) | ||
} | ||
token =apiKey.Key | ||
} | ||
var ( | ||
u *url.URL | ||
qp url.Values | ||
) | ||
if containerName != "" { | ||
u, qp = buildVSCodeWorkspaceDevContainerLink( | ||
token, | ||
client.URL.String(), | ||
workspace, | ||
workspaceAgent, | ||
containerName, | ||
directory, | ||
) | ||
} else { | ||
u, qp = buildVSCodeWorkspaceLink( | ||
token, | ||
client.URL.String(), | ||
workspace, | ||
workspaceAgent, | ||
directory, | ||
) | ||
} | ||
openingPath := workspaceName | ||
if directory != "" { | ||
@@ -204,6 +247,13 @@ func (r *RootCmd) openVSCode() *serpent.Command { | ||
), | ||
Value: serpent.BoolOf(&generateToken), | ||
}, | ||
{ | ||
Flag: "container", | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
FlagShorthand: "c", | ||
Description: "Container name to connect to in the workspace.", | ||
Value: serpent.StringOf(&containerName), | ||
Hidden: true, // Hidden until this features is at least in beta. | ||
}, | ||
{ | ||
Flag: "test.open-error", | ||
Description: "Don't run the open command.", | ||
@@ -344,6 +394,65 @@ func (r *RootCmd) openApp() *serpent.Command { | ||
return cmd | ||
} | ||
func buildVSCodeWorkspaceLink( | ||
token string, | ||
clientURL string, | ||
workspace codersdk.Workspace, | ||
workspaceAgent codersdk.WorkspaceAgent, | ||
directory string, | ||
) (*url.URL, url.Values) { | ||
qp := url.Values{} | ||
qp.Add("url", clientURL) | ||
qp.Add("owner", workspace.OwnerName) | ||
qp.Add("workspace", workspace.Name) | ||
qp.Add("agent", workspaceAgent.Name) | ||
if directory != "" { | ||
qp.Add("folder", directory) | ||
} | ||
if token != "" { | ||
qp.Add("token", token) | ||
} | ||
return &url.URL{ | ||
Scheme: "vscode", | ||
Host: "coder.coder-remote", | ||
Path: "/open", | ||
RawQuery: qp.Encode(), | ||
}, qp | ||
} | ||
func buildVSCodeWorkspaceDevContainerLink( | ||
token string, | ||
clientURL string, | ||
workspace codersdk.Workspace, | ||
workspaceAgent codersdk.WorkspaceAgent, | ||
containerName string, | ||
containerFolder string, | ||
) (*url.URL, url.Values) { | ||
containerFolder = filepath.ToSlash(containerFolder) | ||
qp := url.Values{} | ||
qp.Add("url", clientURL) | ||
qp.Add("owner", workspace.OwnerName) | ||
qp.Add("workspace", workspace.Name) | ||
qp.Add("agent", workspaceAgent.Name) | ||
qp.Add("devContainerName", containerName) | ||
qp.Add("devContainerFolder", containerFolder) | ||
if token != "" { | ||
qp.Add("token", token) | ||
} | ||
return &url.URL{ | ||
Scheme: "vscode", | ||
Host: "coder.coder-remote", | ||
Path: "/openDevContainer", | ||
RawQuery: qp.Encode(), | ||
}, qp | ||
} | ||
// waitForAgentCond uses the watch workspace API to update the agent information | ||
// until the condition is met. | ||
func waitForAgentCond(ctx context.Context, client *codersdk.Client, workspace codersdk.Workspace, workspaceAgent codersdk.WorkspaceAgent, cond func(codersdk.WorkspaceAgent) bool) (codersdk.Workspace, codersdk.WorkspaceAgent, error) { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.