X Tutup
Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.
Closed
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
132 changes: 74 additions & 58 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,22 @@ Arguments:
)
}

flag.Parse()
if *printVersion {
fmt.Printf("%v\n", version)
os.Exit(0)
}

host := flag.Arg(0)

if host == "" {
// If no host is specified output the usage.
flag.Usage()
os.Exit(1)
}

dir := flag.Arg(1)
if dir == "" {
dir = "~"
}
host, dir := arrangeArgs(printVersion)

flog.Info("ensuring code-server is updated...")

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

downloadScript := `set -euxo pipefail || exit 1

mkdir -p ~/.local/share/code-server
cd ` + filepath.Dir(codeServerPath) + `
wget -N https://codesrv-ci.cdr.sh/latest-linux
[ -f ` + codeServerPath + ` ] && rm ` + codeServerPath + `
ln latest-linux ` + codeServerPath + `
chmod +x ` + codeServerPath
mkdir -p ~/.local/share/code-server
cd ` + filepath.Dir(codeServerPath) + `
wget -N https://codesrv-ci.cdr.sh/latest-linux
[ -f ` + codeServerPath + ` ] && rm ` + codeServerPath + `
ln latest-linux ` + codeServerPath + `
chmod +x ` + codeServerPath
// Downloads the latest code-server and allows it to be executed.
sshCmdStr := fmt.Sprintf("ssh" +
" " + *sshFlags + " " +
host + " /bin/bash",
)
sshCmdStr := fmt.Sprintf("ssh" + " " + *sshFlags + " " + host + " /bin/bash")
sshCmd := exec.Command("sh", "-c", sshCmdStr)
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
Expand All @@ -133,20 +113,7 @@ chmod +x ` + codeServerPath
}

if !*skipSyncFlag {
start := time.Now()
flog.Info("syncing settings")
err = syncUserSettings(*sshFlags, host, false)
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)
if err != nil {
flog.Fatal("failed to sync extensions: %v", err)
}
flog.Info("synced extensions in %s", time.Since(start))
syncConfigurations(sshFlags, host)
}

flog.Info("starting code-server...")
Expand All @@ -171,23 +138,10 @@ chmod +x ` + codeServerPath
flog.Fatal("failed to start code-server: %v", err)
}

url := "http://127.0.0.1:" + localPort
url := fmt.Sprintf("http://127.0.0.1:%v", localPort)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
for {
if ctx.Err() != nil {
flog.Fatal("code-server didn't start in time %v", ctx.Err())
}
// Waits for code-server to be available before opening the browser.
r, _ := http.NewRequest("GET", url, nil)
r = r.WithContext(ctx)
resp, err := http.DefaultClient.Do(r)
if err != nil {
continue
}
resp.Body.Close()
break
}
waitForCodeServer(ctx, url)

ctx, cancel = context.WithCancel(context.Background())
openBrowser(url)
Expand Down Expand Up @@ -223,6 +177,68 @@ chmod +x ` + codeServerPath
}
}

func arrangeArgs(printVersion *bool) (string, string) {
flag.Parse()
if *printVersion {
fmt.Printf("%v\n", version)
os.Exit(0)
}
host := flag.Arg(0)
if host == "" {
// If no host is specified output the usage.
flag.Usage()
os.Exit(1)
}
dir := flag.Arg(1)
if dir == "" {
dir = "~"
}
return host, dir
}

func createCommand(sshCmd *exec.Cmd, sshCmdStr string) *exec.Cmd {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function appears to be unused

sshCmd = exec.Command("sh", "-c",
sshCmdStr,
)
sshCmd.Stdin = os.Stdin
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
return sshCmd
}

func syncConfigurations(sshFlags *string, host string) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if someone wished to sync from the remote back to the local machine?

start := time.Now()
flog.Info("syncing settings")
err := syncUserSettings(*sshFlags, host, false)
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)
if err != nil {
flog.Fatal("failed to sync extensions: %v", err)
}
flog.Info("synced extensions in %s", time.Since(start))
}

func waitForCodeServer(ctx context.Context, url string) {
for {
if ctx.Err() != nil {
flog.Fatal("code-server didn't start in time %v", ctx.Err())
}
// Waits for code-server to be available before opening the browser.
r, _ := http.NewRequest("GET", url, nil)
r = r.WithContext(ctx)
resp, err := http.DefaultClient.Do(r)
if err != nil {
continue
}
resp.Body.Close()
break
}
}

func openBrowser(url string) {
var openCmd *exec.Cmd
switch {
Expand Down
X Tutup