From 57ccf3a9139a50590d3743f9e16e60c5ce182f0a Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Tue, 30 Apr 2019 14:31:18 -0500 Subject: [PATCH] Support multiple users using same machine - code-server is now downloaded to ~/.cache/sshcode --- sshcode.go | 9 +++++---- sshcode_test.go | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/sshcode.go b/sshcode.go index 1a39e31..c644c7d 100644 --- a/sshcode.go +++ b/sshcode.go @@ -19,6 +19,8 @@ import ( "golang.org/x/xerrors" ) +const codeServerPath = "~/.cache/sshcode/sshcode-server" + type options struct { skipSync bool syncBack bool @@ -30,8 +32,6 @@ type options struct { func sshCode(host, dir string, o options) error { flog.Info("ensuring code-server is updated...") - const codeServerPath = "/tmp/sshcode-code-server" - dlScript := downloadScript(codeServerPath) // Downloads the latest code-server and allows it to be executed. @@ -97,7 +97,7 @@ func sshCode(host, dir string, o options) error { sshCmd.Stderr = os.Stderr err = sshCmd.Start() if err != nil { - flog.Fatal("failed to start code-server: %v", err) + return xerrors.Errorf("failed to start code-server: %w", err) } url := "http://127.0.0.1:" + o.localPort @@ -313,12 +313,13 @@ func downloadScript(codeServerPath string) string { return fmt.Sprintf( `set -euxo pipefail || exit 1 -mkdir -p ~/.local/share/code-server +mkdir -p ~/.local/share/code-server %v cd %v wget -N https://codesrv-ci.cdr.sh/latest-linux [ -f %v ] && rm %v ln latest-linux %v chmod +x %v`, + filepath.Dir(codeServerPath), filepath.Dir(codeServerPath), codeServerPath, codeServerPath, diff --git a/sshcode_test.go b/sshcode_test.go index 3c87d3c..e486460 100644 --- a/sshcode_test.go +++ b/sshcode_test.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "strconv" "sync" "testing" @@ -50,7 +51,11 @@ func TestSSHCode(t *testing.T) { waitForSSHCode(t, localPort, time.Second*30) waitForSSHCode(t, remotePort, time.Second*30) - out, err := exec.Command("pkill", "sshcode-code").CombinedOutput() + // Typically we'd do an os.Stat call here but the os package doesn't expand '~' + out, err := exec.Command("sh", "-c", "stat "+codeServerPath).CombinedOutput() + require.NoError(t, err, "%s", out) + + out, err = exec.Command("pkill", filepath.Base(codeServerPath)).CombinedOutput() require.NoError(t, err, "%s", out) wg.Wait()