diff --git a/codeserver.go b/codeserver.go index f2559da..afefe2c 100644 --- a/codeserver.go +++ b/codeserver.go @@ -24,7 +24,7 @@ import ( func loadCodeServer(ctx context.Context) (string, error) { start := time.Now() - const cachePath = "/tmp/sail-code-server-cache/code-server" + cachePath := filepath.Join(os.TempDir(), "sail-code-server-cache/code-server") // downloadURLPath stores the download URL, so we know whether we should update // the binary. diff --git a/config.go b/config.go index 5468eb4..4a1f653 100644 --- a/config.go +++ b/config.go @@ -18,15 +18,14 @@ func resolvePath(homedir string, path string) string { return path } - list := strings.Split(path, string(filepath.Separator)) - - for i, seg := range list { - if seg == "~" { - list[i] = homedir - } + // Replace tilde notation in path with homedir. + if path == "~" { + path = homedir + } else if strings.HasPrefix(path, "~/") { + path = filepath.Join(homedir, path[2:]) } - return filepath.Join(list...) + return filepath.Clean(path) } // config describes the config.toml. diff --git a/runner.go b/runner.go index 75802d1..d8199a3 100644 --- a/runner.go +++ b/runner.go @@ -236,12 +236,12 @@ func (r *runner) mounts(mounts []mount.Mount, image string) ([]mount.Mount, erro // Mount in VS Code configs. mounts = append(mounts, mount.Mount{ Type: "bind", - Source: "~/.config/Code", + Source: vscodeConfigDir(), Target: "~/.config/Code", }) mounts = append(mounts, mount.Mount{ Type: "bind", - Source: "~/.vscode/extensions", + Source: vscodeExtensionsDir(), Target: hostExtensionsDir, }) @@ -251,8 +251,7 @@ func (r *runner) mounts(mounts []mount.Mount, image string) ([]mount.Mount, erro // socket to the container allows for using the user's existing setup for // ssh authentication instead of having to create a new keys or explicity // pass them in. - sshAuthSock, exists := os.LookupEnv("SSH_AUTH_SOCK") - if exists { + if sshAuthSock, exists := os.LookupEnv("SSH_AUTH_SOCK"); exists { mounts = append(mounts, mount.Mount{ Type: "bind", Source: sshAuthSock, diff --git a/vscode.go b/vscode.go new file mode 100644 index 0000000..b76a8d3 --- /dev/null +++ b/vscode.go @@ -0,0 +1,33 @@ +package main + +import ( + "os" + "path/filepath" + "runtime" +) + +const ( + vsCodeConfigDirEnv = "VSCODE_CONFIG_DIR" + vsCodeExtensionsDirEnv = "VSCODE_EXTENSIONS_DIR" +) + +func vscodeConfigDir() string { + if env, ok := os.LookupEnv(vsCodeConfigDirEnv); ok { + return os.ExpandEnv(env) + } + + path := os.ExpandEnv("$HOME/.config/Code/") + if runtime.GOOS == "darwin" { + path = os.ExpandEnv("$HOME/Library/Application Support/Code/") + } + return filepath.Clean(path) +} + +func vscodeExtensionsDir() string { + if env, ok := os.LookupEnv(vsCodeExtensionsDirEnv); ok { + return os.ExpandEnv(env) + } + + path := os.ExpandEnv("$HOME/.vscode/extensions/") + return filepath.Clean(path) +}