Skip to content

Commit 734a7f5

Browse files
authored
Merge pull request #73 from stuartleeks/sl/set-browser
Set browser env var
2 parents d5623b9 + ac9f555 commit 734a7f5

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

internal/pkg/devcontainers/dockerutils.go

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
239239
if err != nil {
240240
return err
241241
}
242+
if userName == "" {
243+
userName, err = getUserNameFromRunningContainer(containerID)
244+
if err != nil {
245+
return err
246+
}
247+
}
242248

243249
statusWriter.Printf("Checking for SSH_AUTH_SOCK")
244250
sshAuthSockValue, err := getSshAuthSockValue(containerID)
@@ -251,10 +257,11 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
251257

252258
statusWriter.Printf("Getting container PATH")
253259
containerPath, err := getContainerEnvVar(containerID, "PATH")
260+
vscodeServerPath := ""
254261
if err == nil {
255262
// Got the PATH
256263
statusWriter.Printf("Getting code server path")
257-
vscodeServerPath, err := getVscodeServerPath(containerID)
264+
vscodeServerPath, err = getVscodeServerPath(containerID, userName)
258265
if err == nil {
259266
// Got the VS Code server location - add bin subfolder to PATH
260267
containerPath = strings.TrimSpace(containerPath)
@@ -271,6 +278,13 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
271278
fmt.Println("Continuing without overriding PATH...")
272279
}
273280

281+
browser := ""
282+
if vscodeServerPath == "" {
283+
fmt.Printf("Warning: VS Code Server location not found. Continuing without setting BROWSER...")
284+
} else {
285+
browser = fmt.Sprintf("%s/helpers/browser.sh", vscodeServerPath)
286+
}
287+
274288
statusWriter.Printf("Getting VSCODE_IPC_HOOK_CLI")
275289
vscodeIpcSock, err := getVscodeIpcSock(containerID)
276290
if err != nil {
@@ -349,6 +363,9 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
349363
if vscodeGitIpcSock != "" {
350364
dockerArgs = append(dockerArgs, "--env", "VSCODE_GIT_IPC_HANDLE="+vscodeGitIpcSock)
351365
}
366+
if browser != "" {
367+
dockerArgs = append(dockerArgs, "--env", "BROWSER="+browser)
368+
}
352369
dockerArgs = append(dockerArgs, containerID)
353370
dockerArgs = append(dockerArgs, args...)
354371

@@ -382,34 +399,38 @@ func getSshAuthSockValue(containerID string) (string, error) {
382399
// Host has SSH_AUTH_SOCK set, so expecting the dev container to have forwarding set up
383400
// Find the latest /tmp/vscode-ssh-auth-<...>.sock
384401

385-
return getLatestFileMatch(containerID, "\"${TMPDIR:-/tmp}\"/vscode-ssh-auth-*")
402+
return getLatestFileMatch(containerID, "", "\"${TMPDIR:-/tmp}\"/vscode-ssh-auth-*")
386403
}
387404

388-
func getVscodeServerPath(containerID string) (string, error) {
389-
path, err := getLatestFileMatch(containerID, "${HOME}/.vscode-server/bin/*")
405+
func getVscodeServerPath(containerID string, userName string) (string, error) {
406+
path, err := getLatestFileMatch(containerID, userName, "${HOME}/.vscode-server/bin/*")
390407
if err == nil {
391408
return path, err
392409
}
393-
path, err = getLatestFileMatch(containerID, "/vscode/vscode-server/bin/linux-x64/*")
410+
path, err = getLatestFileMatch(containerID, "", "/vscode/vscode-server/bin/linux-x64/*")
394411
if err == nil {
395412
return path, err
396413
}
397-
return getLatestFileMatch(containerID, "/vscode/vscode-server/bin/x64/*")
414+
return getLatestFileMatch(containerID, "", "/vscode/vscode-server/bin/x64/*")
398415
}
399416
func getVscodeIpcSock(containerID string) (string, error) {
400-
return getLatestFileMatch(containerID, "\"${TMPDIR:-/tmp}\"/vscode-ipc-*")
417+
return getLatestFileMatch(containerID, "", "\"${TMPDIR:-/tmp}\"/vscode-ipc-*")
401418
}
402419
func getRemoteContainersIpcSock(containerID string) (string, error) {
403-
return getLatestFileMatch(containerID, "\"${TMPDIR:-/tmp}\"/vscode-remote-containers-ipc-*")
420+
return getLatestFileMatch(containerID, "", "\"${TMPDIR:-/tmp}\"/vscode-remote-containers-ipc-*")
404421
}
405422
func getGitIpcSock(containerID string, userID string) (string, error) {
406-
return getLatestFileMatch(containerID, fmt.Sprintf("\"${TMPDIR:-/tmp}\"/user/%s/vscode-git-*", userID))
423+
return getLatestFileMatch(containerID, "", fmt.Sprintf("\"${TMPDIR:-/tmp}\"/user/%s/vscode-git-*", userID))
407424
}
408425

409426
// getLatestFileMatch lists files matching `pattern` in the container and returns the latest filename
410-
func getLatestFileMatch(containerID string, pattern string) (string, error) {
427+
func getLatestFileMatch(containerID string, userName string, pattern string) (string, error) {
411428

412-
dockerArgs := []string{"exec", containerID, "bash", "-c", fmt.Sprintf("ls -t -d -1 %s", pattern)}
429+
dockerArgs := []string{"exec"}
430+
if userName != "" {
431+
dockerArgs = append(dockerArgs, "--user", userName)
432+
}
433+
dockerArgs = append(dockerArgs, containerID, "bash", "-c", fmt.Sprintf("ls -t -d -1 %s", pattern))
413434
dockerCmd := exec.Command("docker", dockerArgs...)
414435
buf, err := dockerCmd.CombinedOutput()
415436
if err != nil {
@@ -470,3 +491,28 @@ func testContainerPathExists(containerID string, path string) (bool, error) {
470491
response := strings.TrimSpace(string(buf))
471492
return response == "0", nil
472493
}
494+
495+
func getUserNameFromRunningContainer(containerID string) (string, error) {
496+
dockerArgs := []string{"inspect", containerID, "--format", "{{index .Config.Labels \"devcontainer.metadata\" }}"}
497+
dockerCmd := exec.Command("docker", dockerArgs...)
498+
buf, err := dockerCmd.CombinedOutput()
499+
if err != nil {
500+
errMessage := string(buf)
501+
return "", fmt.Errorf("Docker exec error: %s (%s)", err, strings.TrimSpace(errMessage))
502+
}
503+
504+
var metadata []interface{}
505+
err = json.Unmarshal(buf, &metadata)
506+
if err != nil {
507+
return "", nil
508+
}
509+
510+
for _, value := range metadata {
511+
if valueMap, ok := value.(map[string]interface{}); ok {
512+
if userName, ok := valueMap["remoteUser"]; ok {
513+
return userName.(string), nil
514+
}
515+
}
516+
}
517+
return "", nil
518+
}

0 commit comments

Comments
 (0)