Skip to content

Commit 3735e3b

Browse files
committed
feat: support multiple source files in mgrctl cp (Fixes #657)
Allow passing multiple source file paths to `mgrctl cp`. The last argument is treated as the destination. All sources must be consistently either local paths or server: prefixed paths; mixing is not allowed. Update the Use string and Long description to reflect the new multi-source behavior. Add validation to reject mixed local and server source paths.
1 parent 5c6fcfe commit 3735e3b

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

mgrctl/cmd/cp/cp.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// SPDX-FileCopyrightText: 2024 SUSE LLC
2+
// SPDX-FileCopyrightText: 2026 YatesMold
23
//
34
// SPDX-License-Identifier: Apache-2.0
45

56
package cp
67

78
import (
9+
"fmt"
10+
"strings"
11+
812
"github.com/spf13/cobra"
913
"github.com/uyuni-project/uyuni-tools/shared"
1014
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
@@ -25,11 +29,12 @@ func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
2529
flags := &flagpole{}
2630

2731
cpCmd := &cobra.Command{
28-
Use: "cp [path/to/source.file] [path/to/destination.file]",
32+
Use: "cp [path/to/source1.file ...] [path/to/destination]",
2933
Short: L("Copy files to and from the containers"),
30-
Long: L(`Takes a source and destination parameters.
31-
One of them can be prefixed with 'server:' to indicate the path is within the server pod.`),
32-
Args: cobra.ExactArgs(2),
34+
Long: L(`Takes at least one source and destination parameters.
35+
Several source files can be passed, the last parameter will be the destination.
36+
Either the source parameters or the destination can be prefixed with 'server:' to indicate the path is within the server pod.`),
37+
Args: cobra.MinimumNArgs(2),
3338
RunE: func(cmd *cobra.Command, args []string) error {
3439
viper, err := utils.ReadConfig(cmd, utils.GlobalConfigFilename, globalFlags.ConfigPath)
3540
if err != nil {
@@ -51,5 +56,21 @@ func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
5156

5257
func run(flags *flagpole, _ *cobra.Command, args []string) error {
5358
cnx := shared.NewConnection(flags.Backend, podman.ServerContainerName, kubernetes.ServerFilter)
54-
return cnx.Copy(args[0], args[1], flags.User, flags.Group)
59+
dst := args[len(args)-1]
60+
srcs := args[:len(args)-1]
61+
62+
// Validate that source paths don't mix local and server paths.
63+
hasServerSrc := strings.HasPrefix(srcs[0], "server:")
64+
for _, src := range srcs[1:] {
65+
if strings.HasPrefix(src, "server:") != hasServerSrc {
66+
return fmt.Errorf(L("cannot mix local and server sources"))
67+
}
68+
}
69+
70+
for _, src := range srcs {
71+
if err := cnx.Copy(src, dst, flags.User, flags.Group); err != nil {
72+
return err
73+
}
74+
}
75+
return nil
5576
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Support multiple source files in mgrctl cp (gh#657)

0 commit comments

Comments
 (0)