This repository was archived by the owner on Jan 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Some refactoring II #65
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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...") | ||
|
|
@@ -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) | ||
|
|
@@ -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 { | ||
| 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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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