From a8fe0d19d794a3b2f0507e18090208ecd5918160 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Tue, 17 Nov 2026 17:25:57 +0100 Subject: [PATCH] fix(copy): split instance paths at the first colon like scp parseWorkspacePath split on every colon and required exactly two parts, so remote paths with a colon (timestamped logs) were rejected and local paths with a colon were parsed as instance names. Split at the first colon and treat an argument as local when a "/" comes before the colon, as scp does. Fixes #537 --- pkg/cmd/copy/copy.go | 29 ++++++++--------------- pkg/cmd/copy/copy_test.go | 50 +++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/pkg/cmd/copy/copy.go b/pkg/cmd/copy/copy.go index ec97a8bd1..d3f8fe432 100644 --- a/pkg/cmd/copy/copy.go +++ b/pkg/cmd/copy/copy.go @@ -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)") @@ -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) diff --git a/pkg/cmd/copy/copy_test.go b/pkg/cmd/copy/copy_test.go index 3767f5e93..3f08e91d3 100644 --- a/pkg/cmd/copy/copy_test.go +++ b/pkg/cmd/copy/copy_test.go @@ -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) } @@ -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) } @@ -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") } }