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

Commit 1aa7091

Browse files
committed
Add support for SSH flags
- Clean up `if-else` monster in `openBrowser`. - Wrap SSH command with `sh -c` for readability & debugability. - Download code-server bin to /tmp/ so `sh` doesn't interpret the ~ locally. - This particular functionality will be replaced soon by the caching download.
1 parent db3f6cc commit 1aa7091

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

main.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"runtime"
1414
"strconv"
15+
"strings"
1516
"time"
1617

1718
"golang.org/x/xerrors"
@@ -25,12 +26,14 @@ func init() {
2526

2627
func main() {
2728
skipSyncFlag := flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host")
29+
sshFlags := flag.String("ssh-flags", "", "custom SSH flags")
2830
flag.Usage = func() {
29-
fmt.Printf(`Usage: [-skipsync] %v HOST [SSH ARGS...]
31+
fmt.Printf(`Usage: [-skipsync] %v HOST [DIR] [SSH ARGS...]
3032
3133
Start code-server over SSH.
3234
More info: https://github.com/codercom/sshcode
33-
`, os.Args[0])
35+
`, os.Args[0],
36+
)
3437
}
3538

3639
flag.Parse()
@@ -42,16 +45,23 @@ More info: https://github.com/codercom/sshcode
4245
os.Exit(1)
4346
}
4447

48+
dir := flag.Arg(1)
49+
if dir == "" {
50+
dir = "\\~"
51+
}
52+
4553
flog.Info("ensuring code-server is updated...")
4654

55+
const codeServerPath = "/tmp/codessh-code-server"
56+
4757
// Downloads the latest code-server and allows it to be executed.
4858
sshCmd := exec.Command("ssh",
4959
"-tt",
5060
host,
5161
`/bin/bash -c 'set -euxo pipefail || exit 1
5262
mkdir -p ~/bin
53-
wget -q https://codesrv-ci.cdr.sh/latest-linux -O ~/bin/code-server
54-
chmod +x ~/bin/code-server
63+
wget -q https://codesrv-ci.cdr.sh/latest-linux -O `+codeServerPath+`
64+
chmod +x `+codeServerPath+`
5565
mkdir -p ~/.local/share/code-server
5666
'`,
5767
)
@@ -83,15 +93,18 @@ mkdir -p ~/.local/share/code-server
8393
flog.Fatal("failed to find available port: %v", err)
8494
}
8595

96+
// Escaped so interpreted by the remote shell, not local.
97+
dir = strings.Replace(dir, "~", "\\~", 1)
98+
99+
sshCmdStr := fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'",
100+
localPort+":localhost:"+localPort, *sshFlags, host, dir, codeServerPath, localPort,
101+
)
102+
86103
// Starts code-server and forwards the remote port.
87-
sshCmd = exec.Command("ssh",
88-
"-tt",
89-
"-q",
90-
"-L",
91-
localPort+":localhost:"+localPort,
92-
host,
93-
"~/bin/code-server --host 127.0.0.1 --allow-http --no-auth --port="+localPort,
104+
sshCmd = exec.Command("sh", "-c",
105+
sshCmdStr,
94106
)
107+
sshCmd.Stdin = os.Stdin
95108
sshCmd.Stdout = os.Stdout
96109
sshCmd.Stderr = os.Stderr
97110
err = sshCmd.Start()
@@ -123,22 +136,17 @@ mkdir -p ~/.local/share/code-server
123136

124137
func openBrowser(url string) {
125138
var openCmd *exec.Cmd
126-
if commandExists("google-chrome") {
139+
switch {
140+
case commandExists("google-chrome"):
127141
openCmd = exec.Command("google-chrome", fmtChromeOptions(url)...)
128-
129-
} else if commandExists("chromium") {
142+
case commandExists("chromium"):
130143
openCmd = exec.Command("chromium", fmtChromeOptions(url)...)
131-
132-
} else if commandExists("chromium-browser") {
144+
case commandExists("chromium-browser"):
133145
openCmd = exec.Command("chromium-browser", fmtChromeOptions(url)...)
134-
135-
} else if commandExists("firefox") {
146+
case commandExists("firefox"):
136147
openCmd = exec.Command("firefox", "--url="+url, "-safe-mode")
137-
138-
} else {
148+
default:
139149
flog.Info("unable to find a browser to open: sshcode only supports firefox, chrome, and chromium")
140-
141-
return
142150
}
143151

144152
err := openCmd.Start()

0 commit comments

Comments
 (0)