Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Add option insiders with env-settings #40

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ on follow-up connections to the same server.

To disable this feature entirely, pass the `--skipsync` flag.

### Custom settings directories

If you're using an alternative release of VS Code such as VS Code Insiders, you
must specify your settings directories through the `VSCODE_CONFIG_DIR` and
`VSCODE_EXTENSIONS_DIR` environment variables.

The following will make `sshcode` work with VS Code Insiders:

```bash
export VSCODE_CONFIG_DIR="$HOME/.config/Code - Insiders/User"
export VSCODE_EXTENSIONS_DIR="$HOME/.vscode-insiders/extensions"
```

### Sync-back

By default, VS Code changes on the remote server won't be synced back
Expand Down
49 changes: 15 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
Expand All @@ -30,12 +29,18 @@ func main() {
skipSyncFlag = flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host")
sshFlags = flag.String("ssh-flags", "", "custom SSH flags")
syncBack = flag.Bool("b", false, "sync extensions back on termination")
insiders = flag.Bool("insiders", false, "use configs and extensions from VSCode Insiders")
)

flag.Usage = func() {
fmt.Printf(`Usage: [-skipsync] %v HOST [DIR] [SSH ARGS...]
fmt.Printf(`Usage: [-skipsync] [-insiders] %v HOST [DIR] [SSH ARGS...]

Start code-server over SSH.

Environment variables:
`+vsCodeConfigDirEnv+` use special VS Code settings dir.
`+vsCodeExtensionsDirEnv+` use special VS Code extensions dir.

More info: https://github.com/codercom/sshcode
`, os.Args[0],
)
Expand Down Expand Up @@ -87,14 +92,14 @@ chmod +x ` + codeServerPath
if !*skipSyncFlag {
start := time.Now()
flog.Info("syncing settings")
err = syncUserSettings(*sshFlags, host, false)
err = syncUserSettings(*sshFlags, host, false, *insiders)
if err != nil {
flog.Fatal("failed to sync settings: %v", err)
}
flog.Info("synced settings in %s", time.Since(start))

flog.Info("syncing extensions")
err = syncExtensions(*sshFlags, host, false)
err = syncExtensions(*sshFlags, host, false, *insiders)
if err != nil {
flog.Fatal("failed to sync extensions: %v", err)
}
Expand Down Expand Up @@ -164,12 +169,12 @@ chmod +x ` + codeServerPath

flog.Info("synchronizing VS Code back to local")

err = syncExtensions(*sshFlags, host, true)
err = syncExtensions(*sshFlags, host, true, *insiders)
if err != nil {
flog.Fatal("failed to sync extensions back: %v", err)
}

err = syncUserSettings(*sshFlags, host, true)
err = syncUserSettings(*sshFlags, host, true, *insiders)
if err != nil {
flog.Fatal("failed to user settigns extensions back: %v", err)
}
Expand Down Expand Up @@ -239,8 +244,8 @@ func randomPort() (string, error) {
return "", xerrors.Errorf("max number of tries exceeded: %d", maxTries)
}

func syncUserSettings(sshFlags string, host string, back bool) error {
localConfDir, err := configDir()
func syncUserSettings(sshFlags string, host string, back bool, insiders bool) error {
localConfDir, err := configDir(insiders)
if err != nil {
return err
}
Expand All @@ -259,8 +264,8 @@ func syncUserSettings(sshFlags string, host string, back bool) error {
return rsync(src, dest, sshFlags, "workspaceStorage", "logs", "CachedData")
}

func syncExtensions(sshFlags string, host string, back bool) error {
localExtensionsDir, err := extensionsDir()
func syncExtensions(sshFlags string, host string, back bool, insiders bool) error {
localExtensionsDir, err := extensionsDir(insiders)
if err != nil {
return err
}
Expand Down Expand Up @@ -301,27 +306,3 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err

return nil
}

func configDir() (string, error) {
var path string
switch runtime.GOOS {
case "linux":
path = os.ExpandEnv("$HOME/.config/Code/User/")
case "darwin":
path = os.ExpandEnv("$HOME/Library/Application Support/Code/User/")
default:
return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS)
}
return filepath.Clean(path), nil
}

func extensionsDir() (string, error) {
var path string
switch runtime.GOOS {
case "linux", "darwin":
path = os.ExpandEnv("$HOME/.vscode/extensions/")
default:
return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS)
}
return filepath.Clean(path), nil
}
56 changes: 56 additions & 0 deletions settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"os"
"path/filepath"
"runtime"

"golang.org/x/xerrors"
)

const (
vsCodeConfigDirEnv = "VSCODE_CONFIG_DIR"
vsCodeExtensionsDirEnv = "VSCODE_EXTENSIONS_DIR"
)

func configDir(insiders bool) (string, error) {
if env, ok := os.LookupEnv(vsCodeConfigDirEnv); ok {
return os.ExpandEnv(env), nil
}

var basePath string
switch runtime.GOOS {
case "linux":
basePath = os.ExpandEnv("$HOME/.config")
case "darwin":
basePath = os.ExpandEnv("$HOME/Library/Application Support")
default:
return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS)
}

if insiders {
return filepath.Join(basePath, "Code - Insiders", "User"), nil
}

return filepath.Join(basePath, "Code", "User"), nil
}

func extensionsDir(insiders bool) (string, error) {
if env, ok := os.LookupEnv(vsCodeExtensionsDirEnv); ok {
return os.ExpandEnv(env), nil
}

var basePath string
switch runtime.GOOS {
case "linux", "darwin":
basePath = os.ExpandEnv("$HOME")
default:
return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS)
}

if insiders {
return filepath.Join(basePath, ".vscode-insiders", "extensions"), nil
}

return filepath.Join(basePath, ".vscode", "extensions"), nil
}