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

Commit4c65d17

Browse files
committed
Handle single file sync
1 parent8917fb8 commit4c65d17

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

‎internal/cmd/sync.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func rsyncVersion() string {
4747
funcmakeRunSync(init*bool)func(cmd*cobra.Command,args []string)error {
4848
returnfunc(cmd*cobra.Command,args []string)error {
4949
var (
50+
ctx=cmd.Context()
5051
local=args[0]
5152
remote=args[1]
5253
)
@@ -56,17 +57,9 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
5657
returnerr
5758
}
5859

59-
info,err:=os.Stat(local)
60-
iferr!=nil {
61-
returnerr
62-
}
63-
if!info.IsDir() {
64-
returnxerrors.Errorf("%s must be a directory",local)
65-
}
66-
6760
remoteTokens:=strings.SplitN(remote,":",2)
6861
iflen(remoteTokens)!=2 {
69-
returnxerrors.New("remotemisformatted")
62+
returnxerrors.New("remotemalformatted")
7063
}
7164
var (
7265
envName=remoteTokens[0]
@@ -78,6 +71,14 @@ func makeRunSync(init *bool) func(cmd *cobra.Command, args []string) error {
7871
returnerr
7972
}
8073

74+
info,err:=os.Stat(local)
75+
iferr!=nil {
76+
returnerr
77+
}
78+
if!info.IsDir() {
79+
returnsync.SingleFile(ctx,local,remoteDir,env,client)
80+
}
81+
8182
absLocal,err:=filepath.Abs(local)
8283
iferr!=nil {
8384
returnxerrors.Errorf("make abs path out of %s, %s: %w",local,absLocal,err)

‎internal/sync/singlefile.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package sync
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
12+
"cdr.dev/coder-cli/coder-sdk"
13+
"cdr.dev/wsep"
14+
"golang.org/x/xerrors"
15+
"nhooyr.io/websocket"
16+
)
17+
18+
// SingleFile copies the given file into the remote dir or remote path of the given coder.Environment.
19+
funcSingleFile(ctx context.Context,local,remoteDirstring,env*coder.Environment,client*coder.Client)error {
20+
conn,err:=client.DialWsep(ctx,env)
21+
iferr!=nil {
22+
returnxerrors.Errorf("dial remote execer: %w",err)
23+
}
24+
deferfunc() {_=conn.Close(websocket.StatusNormalClosure,"normal closure") }()
25+
26+
ifstrings.HasSuffix(remoteDir,string(filepath.Separator)) {
27+
remoteDir+=filepath.Base(local)
28+
}
29+
30+
execer:=wsep.RemoteExecer(conn)
31+
cmd:=fmt.Sprintf(`[ -d %s ] && cat > %s/%s || cat > %s`,remoteDir,remoteDir,filepath.Base(local),remoteDir)
32+
process,err:=execer.Start(ctx, wsep.Command{
33+
Command:"sh",
34+
Args: []string{"-c",cmd},
35+
Stdin:true,
36+
})
37+
iferr!=nil {
38+
returnxerrors.Errorf("start sync command: %w",err)
39+
}
40+
41+
sourceFile,err:=os.Open(local)
42+
iferr!=nil {
43+
returnxerrors.Errorf("open source file: %w",err)
44+
}
45+
46+
gofunc() {_,_=io.Copy(ioutil.Discard,process.Stdout()) }()
47+
gofunc() {_,_=io.Copy(ioutil.Discard,process.Stderr()) }()
48+
gofunc() {
49+
stdin:=process.Stdin()
50+
deferstdin.Close()
51+
_,_=io.Copy(stdin,sourceFile)
52+
}()
53+
54+
iferr:=process.Wait();err!=nil {
55+
returnxerrors.Errorf("copy process: %w",err)
56+
}
57+
returnnil
58+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp