Skip to content

Commit 607d890

Browse files
committed
Add support for infered file names
When using "mgrctl cp file server:file" we always had to specify filename. This commit inders the filename in case of "mgrctl cp file server:" Similarly when copying into the directory when path ends with "/".
1 parent 54cc4b2 commit 607d890

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

shared/connection.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ func (c *Connection) Copy(src string, dst string, user string, group string) err
398398
namespacePrefix = namespace + "/"
399399
}
400400

401+
srcPath := strings.Replace(src, "server:", "", 1)
402+
// Assume the same name as the source if we use copy with just "server:""
403+
// Similarly if we copy to the directory "something/"
404+
if strings.HasSuffix(dst, ":") || strings.HasSuffix(dst, "/") {
405+
dst += filepath.Base(srcPath)
406+
}
407+
401408
var commandArgs []string
402409
extraArgs := []string{}
403410
srcExpanded := strings.Replace(src, "server:", namespacePrefix+podName+":", 1)

shared/connection_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,44 @@ func TestHostCopy(t *testing.T) {
166166
}
167167
}
168168

169+
func TestHostInferredCopy(t *testing.T) {
170+
originalRunner := runner
171+
defer func() { runner = originalRunner }()
172+
173+
var capturedCommand string
174+
var capturedArgs []string
175+
176+
runner = func(command string, args ...string) types.Runner {
177+
capturedCommand = command
178+
capturedArgs = args
179+
return &mockRunner{}
180+
}
181+
182+
cnx := NewConnection("host", "", "")
183+
err := cnx.Copy("server:/home/myfile", "/tmp/", "", "")
184+
185+
if err != nil {
186+
t.Errorf("Copy returned error: %v", err)
187+
}
188+
189+
if capturedCommand != "cp" {
190+
t.Errorf("Expected command 'cp', got '%s'", capturedCommand)
191+
}
192+
193+
if capturedArgs[0] != "/home/myfile" || capturedArgs[1] != "/tmp/myfile" {
194+
t.Errorf("Expected args ['/home/myfile', '/tmp/myfile'], got %v", capturedArgs)
195+
}
196+
197+
err = cnx.Copy("/tmp/myanotherfile", "server:", "", "")
198+
if err != nil {
199+
t.Errorf("Copy returned error: %v", err)
200+
}
201+
202+
if capturedArgs[0] != "/tmp/myanotherfile" || capturedArgs[1] != "myanotherfile" {
203+
t.Errorf("Expected args ['/tmp/myanotherfile', 'myanotherfile'], got %v", capturedArgs)
204+
}
205+
}
206+
169207
func TestHostUserExec(t *testing.T) {
170208
originalRunner := runner
171209
defer func() { runner = originalRunner }()

0 commit comments

Comments
 (0)