Skip to content
Open
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
29 changes: 10 additions & 19 deletions pkg/cmd/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,8 @@ func runCopyCommand(t *terminal.Terminal, cstore CopyStore, source, dest string,
}

func parseCopyArguments(source, dest string) (workspaceNameOrID, remotePath, localPath string, isUpload bool, err error) {
sourceWorkspace, sourcePath, err := parseWorkspacePath(source)
if err != nil {
return "", "", "", false, err
}

destWorkspace, destPath, err := parseWorkspacePath(dest)
if err != nil {
return "", "", "", false, err
}
sourceWorkspace, sourcePath := parseWorkspacePath(source)
destWorkspace, destPath := parseWorkspacePath(dest)

if (sourceWorkspace == "" && destWorkspace == "") || (sourceWorkspace != "" && destWorkspace != "") {
return "", "", "", false, breverrors.NewValidationError("exactly one of source or destination must be an instance path (format: instance_name:/path)")
Expand Down Expand Up @@ -166,17 +159,15 @@ func isDirectory(path string) bool {
return info.IsDir()
}

func parseWorkspacePath(path string) (workspace, filePath string, err error) {
if !strings.Contains(path, ":") {
return "", path, nil
}

parts := strings.Split(path, ":")
if len(parts) != 2 {
return "", "", breverrors.NewValidationError("invalid instance path format, use instance_name:/path")
// parseWorkspacePath splits "instance:/path" at the first colon, like scp and
// rsync, so the remote path may itself contain colons. As with scp, a colon
// that comes after a "/" is part of a local path (e.g. ./out/12:30:00.log).
func parseWorkspacePath(path string) (workspace, filePath string) {
workspace, filePath, found := strings.Cut(path, ":")
if !found || strings.Contains(workspace, "/") {
return "", path
}

return parts[0], parts[1], nil
return workspace, filePath
}

type commandRunner func(name string, args ...string) ([]byte, error)
Expand Down
50 changes: 38 additions & 12 deletions pkg/cmd/copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ func TestParseCopyArguments_BothRemote(t *testing.T) {
}

func TestParseWorkspacePath_Local(t *testing.T) {
ws, fp, err := parseWorkspacePath("/tmp/local/file")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
ws, fp := parseWorkspacePath("/tmp/local/file")
if ws != "" {
t.Errorf("expected empty workspace, got %s", ws)
}
Expand All @@ -127,10 +124,7 @@ func TestParseWorkspacePath_Local(t *testing.T) {
}

func TestParseWorkspacePath_Remote(t *testing.T) {
ws, fp, err := parseWorkspacePath("my-instance:/remote/path")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
ws, fp := parseWorkspacePath("my-instance:/remote/path")
if ws != "my-instance" {
t.Errorf("expected my-instance, got %s", ws)
}
Expand All @@ -139,10 +133,42 @@ func TestParseWorkspacePath_Remote(t *testing.T) {
}
}

func TestParseWorkspacePath_InvalidMultipleColons(t *testing.T) {
_, _, err := parseWorkspacePath("ws:path:extra")
if err == nil {
t.Fatal("expected error for multiple colons")
func TestParseCopyArguments_ColonInRemotePath(t *testing.T) {
ws, remotePath, localPath, isUpload, err := parseCopyArguments("my-node:/logs/run-12:30:00.log", ".")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ws != "my-node" {
t.Errorf("expected workspace my-node, got %s", ws)
}
if remotePath != "/logs/run-12:30:00.log" {
t.Errorf("expected remotePath /logs/run-12:30:00.log, got %s", remotePath)
}
if localPath != "." {
t.Errorf("expected localPath ., got %s", localPath)
}
if isUpload {
t.Error("expected isUpload=false")
}
}

func TestParseCopyArguments_ColonInLocalPath(t *testing.T) {
// As with scp, a colon after a "/" belongs to a local path.
ws, remotePath, localPath, isUpload, err := parseCopyArguments("./out/run-12:30.log", "my-node:/tmp/dest")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ws != "my-node" {
t.Errorf("expected workspace my-node, got %s", ws)
}
if remotePath != "/tmp/dest" {
t.Errorf("expected remotePath /tmp/dest, got %s", remotePath)
}
if localPath != "./out/run-12:30.log" {
t.Errorf("expected localPath ./out/run-12:30.log, got %s", localPath)
}
if !isUpload {
t.Error("expected isUpload=true")
}
}

Expand Down