Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Verify rsync protocol version match prior to proceeding - Rebased on current master.#71

Merged
Merged
Show file tree
Hide file tree
Changes from5 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
31 changes: 31 additions & 0 deletionscmd/coder/sync.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
package main

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

Expand DownExpand Up@@ -29,6 +33,23 @@ func (cmd *syncCmd) RegisterFlags(fl *pflag.FlagSet) {
fl.BoolVarP(&cmd.init, "init", "i", false, "do initial transfer and exit")
}

// Returns local rsync protocol version as a string.
Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies for missing this earlier in the review. These comments should start with the function/method name. So this would be "version returns local rsync protocol version as a string."

(And same for the otherVersion method)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

No problem. I appreciate you teaching me the style you prefer prior to the interview next week so if I get the job, I'm already style trained.

func (_ *syncCmd) version() string {
cmd := exec.Command("rsync", "--version")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}

firstLine, err := bytes.NewBuffer(out).ReadString('\n')
if err != nil {
log.Fatal(err)
}
versionString := strings.Split(firstLine, "protocol version ")

return versionString[1]
}

func (cmd *syncCmd) Run(fl *pflag.FlagSet) {
var (
local = fl.Arg(0)
Expand DownExpand Up@@ -71,6 +92,16 @@ func (cmd *syncCmd) Run(fl *pflag.FlagSet) {
LocalDir: absLocal,
Client: entClient,
}

localVersion := cmd.version()
remoteVersion, rsyncErr := s.Version()

if rsyncErr != nil {
flog.Info("Unable to determine remote rsync version. Proceeding cautiously.")
} else if localVersion != remoteVersion {
flog.Fatal(fmt.Sprintf("rsync protocol mismatch. local is %s; remote is %s.", localVersion, remoteVersion))
}

for err == nil || err == sync.ErrRestartSync {
err = s.Run()
}
Expand Down
43 changes: 43 additions & 0 deletionsinternal/sync/sync.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package sync

import (
"bytes"
"context"
"errors"
"fmt"
Expand All@@ -10,6 +11,7 @@ import (
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
Expand DownExpand Up@@ -261,6 +263,47 @@ const (
maxAcceptableDispatch = time.Millisecond * 50
)

// Returns remote protocol version as a string.
// Or, an error if one exists.
func (s Sync) Version() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

conn, err := s.Client.DialWsep(ctx, s.Env)
if err != nil {
return "", err
}
defer conn.Close(websocket.CloseNormalClosure, "")

execer := wsep.RemoteExecer(conn)
process, err := execer.Start(ctx, wsep.Command{
Command: "rsync",
Args: []string{"--version"},
})
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
io.Copy(buf, process.Stdout())

err = process.Wait()
if code, ok := err.(wsep.ExitError); ok {
return "", fmt.Errorf("Version check exit status: %v", code)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return"",fmt.Errorf("Version check exit status: %v",code)
return"",fmt.Errorf("version check exit status: %v",code)

}
if err != nil {
return "", fmt.Errorf("Server version mismatch")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer returning the error here instead.

stephenwithav reacted with thumbs up emoji
}

firstLine, err := buf.ReadString('\n')
if err != nil {
return "", err
}

versionString := strings.Split(firstLine, "protocol version ")

return versionString[1], nil
}

// Run starts the sync synchronously.
// Use this command to debug what wasn't sync'd correctly:
// rsync -e "coder sh" -nicr ~/Projects/cdr/coder-cli/. ammar:/home/coder/coder-cli/
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp