Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 161768c

Browse files
committed
Add a simple bash test
1 parent 7e5b44a commit 161768c

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor
22
bin
3+
.vscode

main.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func main() {
6464
sshFlags = flag.String("ssh-flags", "", "custom SSH flags")
6565
syncBack = flag.Bool("b", false, "sync extensions back on termination")
6666
printVersion = flag.Bool("version", false, "print version information and exit")
67+
port = flag.String("port", "", "Start VS Code on the provided port. If one is not provided a random one is selected")
68+
noOpen = flag.Bool("no-open", false, "Start VS Code but don't open a Chrome app window")
6769
)
6870

6971
flag.Usage = func() {
@@ -107,14 +109,8 @@ Arguments:
107109

108110
const codeServerPath = "/tmp/codessh-code-server"
109111

110-
downloadScript := `set -euxo pipefail || exit 1
112+
dlScript := downloadScript(codeServerPath)
111113

112-
mkdir -p ~/.local/share/code-server
113-
cd ` + filepath.Dir(codeServerPath) + `
114-
wget -N https://codesrv-ci.cdr.sh/latest-linux
115-
[ -f ` + codeServerPath + ` ] && rm ` + codeServerPath + `
116-
ln latest-linux ` + codeServerPath + `
117-
chmod +x ` + codeServerPath
118114
// Downloads the latest code-server and allows it to be executed.
119115
sshCmdStr := fmt.Sprintf("ssh" +
120116
" " + *sshFlags + " " +
@@ -123,7 +119,7 @@ chmod +x ` + codeServerPath
123119
sshCmd := exec.Command("sh", "-c", sshCmdStr)
124120
sshCmd.Stdout = os.Stdout
125121
sshCmd.Stderr = os.Stderr
126-
sshCmd.Stdin = strings.NewReader(downloadScript)
122+
sshCmd.Stdin = strings.NewReader(dlScript)
127123
err := sshCmd.Run()
128124
if err != nil {
129125
flog.Fatal("failed to update code-server: %v\n---ssh cmd---\n%s\n---download script---\n%s", err,
@@ -150,7 +146,11 @@ chmod +x ` + codeServerPath
150146
}
151147

152148
flog.Info("starting code-server...")
153-
localPort, err := randomPort()
149+
150+
localPort := *port
151+
if localPort == "" {
152+
localPort, err = randomPort()
153+
}
154154
if err != nil {
155155
flog.Fatal("failed to find available port: %v", err)
156156
}
@@ -160,9 +160,7 @@ chmod +x ` + codeServerPath
160160
)
161161

162162
// Starts code-server and forwards the remote port.
163-
sshCmd = exec.Command("sh", "-c",
164-
sshCmdStr,
165-
)
163+
sshCmd = exec.Command("sh", "-c", sshCmdStr)
166164
sshCmd.Stdin = os.Stdin
167165
sshCmd.Stdout = os.Stdout
168166
sshCmd.Stderr = os.Stderr
@@ -190,7 +188,10 @@ chmod +x ` + codeServerPath
190188
}
191189

192190
ctx, cancel = context.WithCancel(context.Background())
193-
openBrowser(url)
191+
192+
if !*noOpen {
193+
openBrowser(url)
194+
}
194195

195196
go func() {
196197
defer cancel()
@@ -352,3 +353,22 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
352353

353354
return nil
354355
}
356+
357+
func downloadScript(codeServerPath string) string {
358+
return fmt.Sprintf(
359+
`set -euxo pipefail || exit 1
360+
361+
mkdir -p ~/.local/share/code-server
362+
cd %v
363+
wget -N https://codesrv-ci.cdr.sh/latest-linux
364+
[ -f %v ] && rm %v
365+
ln latest-linux %v
366+
chmod +x %v`,
367+
filepath.Dir(codeServerPath),
368+
codeServerPath,
369+
codeServerPath,
370+
codeServerPath,
371+
codeServerPath,
372+
)
373+
374+
}

test.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
REMOTE_IP="$1"
6+
VS_CODE_PORT=10010
7+
8+
result() {
9+
rv="$?"
10+
ssh "$REMOTE_IP" "pkill codessh" || true
11+
pkill sshcode || true
12+
exit "$rv"
13+
}
14+
trap "result" EXIT
15+
16+
go install
17+
18+
sshcode --port="$VS_CODE_PORT" --no-open "$REMOTE_IP" &
19+
20+
ATTEMPTS=0
21+
MAX_ATTEMPTS=10
22+
23+
# Try to curl VS Code locally on a backoff retry.
24+
until curl -o /dev/null --connect-timeout 5 -s "localhost:$VS_CODE_PORT" || [ "$ATTEMPTS" -eq "$MAX_ATTEMPTS" ]; do
25+
sleep $(( ATTEMPTS++ ))
26+
done
27+
28+
if [ "$ATTEMPTS" -eq "$MAX_ATTEMPTS" ];
29+
then
30+
echo "Timed out waiting for code server to start"
31+
exit 1
32+
fi
33+
34+
# Curl VS Code remotely.
35+
ssh "$REMOTE_IP" "curl -o /dev/null --connect-timeout 5 -s localhost:$VS_CODE_PORT/"

0 commit comments

Comments
 (0)