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

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Handle single file sync #152

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
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
22 changes: 13 additions & 9 deletions internal/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func rsyncVersion() string {
func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var (
ctx = cmd.Context()
local = args[0]
remote = args[1]
)
Expand All @@ -56,17 +57,9 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
return err
}

info, err := os.Stat(local)
if err != nil {
return err
}
if !info.IsDir() {
return xerrors.Errorf("%s must be a directory", local)
}

remoteTokens := strings.SplitN(remote, ":", 2)
if len(remoteTokens) != 2 {
return xerrors.New("remote misformatted")
return xerrors.New("remote malformatted")
}
var (
envName = remoteTokens[0]
Expand All @@ -78,6 +71,17 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
return err
}

info, err := os.Stat(local)
if err != nil {
return err
}
if info.Mode().IsRegular() {
return sync.SingleFile(ctx, local, remoteDir, env, client)
}
if !info.IsDir() {
return xerrors.Errorf("local path must lead to a regular file or directory: %w", err)
}

absLocal, err := filepath.Abs(local)
if err != nil {
return xerrors.Errorf("make abs path out of %s, %s: %w", local, absLocal, err)
Expand Down
58 changes: 58 additions & 0 deletions internal/sync/singlefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package sync

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/wsep"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
)

// SingleFile copies the given file into the remote dir or remote path of the given coder.Environment.
func SingleFile(ctx context.Context, local, remoteDir string, env *coder.Environment, client *coder.Client) error {
conn, err := client.DialWsep(ctx, env)
if err != nil {
return xerrors.Errorf("dial remote execer: %w", err)
}
defer func() { _ = conn.Close(websocket.StatusNormalClosure, "normal closure") }()

if strings.HasSuffix(remoteDir, string(filepath.Separator)) {
remoteDir += filepath.Base(local)
}

execer := wsep.RemoteExecer(conn)
cmd := fmt.Sprintf(`[ -d %s ] && cat > %s/%s || cat > %s`, remoteDir, remoteDir, filepath.Base(local), remoteDir)
Copy link
Member

Choose a reason for hiding this comment

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

shellmaster moog

process, err := execer.Start(ctx, wsep.Command{
Command: "sh",
Args: []string{"-c", cmd},
Stdin: true,
})
if err != nil {
return xerrors.Errorf("start sync command: %w", err)
}

sourceFile, err := os.Open(local)
if err != nil {
return xerrors.Errorf("open source file: %w", err)
}

go func() { _, _ = io.Copy(ioutil.Discard, process.Stdout()) }()
go func() { _, _ = io.Copy(ioutil.Discard, process.Stderr()) }()
go func() {
stdin := process.Stdin()
defer stdin.Close()
_, _ = io.Copy(stdin, sourceFile)
}()

if err := process.Wait(); err != nil {
return xerrors.Errorf("copy process: %w", err)
}
return nil
}