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

Handle single file sync#152

Merged
cmoog merged 1 commit intomasterfromsinglefile-sync
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsinternal/cmd/sync.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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("remotemisformatted")
return xerrors.New("remotemalformatted")
}
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 deletionsinternal/sync/singlefile.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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

cmoog reacted with heart emoji
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
}

[8]ページ先頭

©2009-2025 Movatter.jp