@@ -12,6 +12,7 @@ import (
12
12
"path/filepath"
13
13
"runtime"
14
14
"strconv"
15
+ "strings"
15
16
"time"
16
17
17
18
"golang.org/x/xerrors"
@@ -25,12 +26,14 @@ func init() {
25
26
26
27
func main () {
27
28
skipSyncFlag := flag .Bool ("skipsync" , false , "skip syncing local settings and extensions to remote host" )
29
+ sshFlags := flag .String ("ssh-flags" , "" , "custom SSH flags" )
28
30
flag .Usage = func () {
29
- fmt .Printf (`Usage: [-skipsync] %v HOST [SSH ARGS...]
31
+ fmt .Printf (`Usage: [-skipsync] %v HOST [DIR] [ SSH ARGS...]
30
32
31
33
Start code-server over SSH.
32
34
More info: https://github.com/codercom/sshcode
33
- ` , os .Args [0 ])
35
+ ` , os .Args [0 ],
36
+ )
34
37
}
35
38
36
39
flag .Parse ()
@@ -42,16 +45,23 @@ More info: https://github.com/codercom/sshcode
42
45
os .Exit (1 )
43
46
}
44
47
48
+ dir := flag .Arg (1 )
49
+ if dir == "" {
50
+ dir = "\\ ~"
51
+ }
52
+
45
53
flog .Info ("ensuring code-server is updated..." )
46
54
55
+ const codeServerPath = "/tmp/codessh-code-server"
56
+
47
57
// Downloads the latest code-server and allows it to be executed.
48
58
sshCmd := exec .Command ("ssh" ,
49
59
"-tt" ,
50
60
host ,
51
61
`/bin/bash -c 'set -euxo pipefail || exit 1
52
62
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 + `
55
65
mkdir -p ~/.local/share/code-server
56
66
'` ,
57
67
)
@@ -83,15 +93,18 @@ mkdir -p ~/.local/share/code-server
83
93
flog .Fatal ("failed to find available port: %v" , err )
84
94
}
85
95
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
+
86
103
// 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 ,
94
106
)
107
+ sshCmd .Stdin = os .Stdin
95
108
sshCmd .Stdout = os .Stdout
96
109
sshCmd .Stderr = os .Stderr
97
110
err = sshCmd .Start ()
@@ -123,22 +136,17 @@ mkdir -p ~/.local/share/code-server
123
136
124
137
func openBrowser (url string ) {
125
138
var openCmd * exec.Cmd
126
- if commandExists ("google-chrome" ) {
139
+ switch {
140
+ case commandExists ("google-chrome" ):
127
141
openCmd = exec .Command ("google-chrome" , fmtChromeOptions (url )... )
128
-
129
- } else if commandExists ("chromium" ) {
142
+ case commandExists ("chromium" ):
130
143
openCmd = exec .Command ("chromium" , fmtChromeOptions (url )... )
131
-
132
- } else if commandExists ("chromium-browser" ) {
144
+ case commandExists ("chromium-browser" ):
133
145
openCmd = exec .Command ("chromium-browser" , fmtChromeOptions (url )... )
134
-
135
- } else if commandExists ("firefox" ) {
146
+ case commandExists ("firefox" ):
136
147
openCmd = exec .Command ("firefox" , "--url=" + url , "-safe-mode" )
137
-
138
- } else {
148
+ default :
139
149
flog .Info ("unable to find a browser to open: sshcode only supports firefox, chrome, and chromium" )
140
-
141
- return
142
150
}
143
151
144
152
err := openCmd .Start ()
0 commit comments