@@ -49,60 +49,71 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
49
49
50
50
var direction copyDirection
51
51
var serviceName string
52
+ var copyFunc func (ctx context.Context , containerID string , srcPath string , dstPath string , opts api.CopyOptions ) error
52
53
if srcService != "" {
53
54
direction |= fromService
54
55
serviceName = srcService
56
+ copyFunc = s .copyFromContainer
55
57
56
58
// copying from multiple containers of a services doesn't make sense.
57
59
if options .All {
58
60
return errors .New ("cannot use the --all flag when copying from a service" )
59
61
}
60
- // due to remove of the --all flag, restore the default value to 1 when copying from a service container to the host.
61
- if options .Index == 0 {
62
- options .Index = 1
63
- }
64
62
}
65
63
if destService != "" {
66
64
direction |= toService
67
65
serviceName = destService
66
+ copyFunc = s .copyToContainer
67
+ }
68
+ if direction == acrossServices {
69
+ return errors .New ("copying between services is not supported" )
70
+ }
71
+
72
+ if direction == 0 {
73
+ return errors .New ("unknown copy direction" )
74
+ }
75
+
76
+ containers , err := s .listContainersTargetedForCopy (ctx , projectName , options .Index , direction , serviceName )
77
+ if err != nil {
78
+ return err
68
79
}
80
+
81
+ g := errgroup.Group {}
82
+ for _ , container := range containers {
83
+ containerID := container .ID
84
+ g .Go (func () error {
85
+ return copyFunc (ctx , containerID , srcPath , dstPath , options )
86
+ })
87
+ }
88
+
89
+ return g .Wait ()
90
+ }
91
+
92
+ func (s * composeService ) listContainersTargetedForCopy (ctx context.Context , projectName string , index int , direction copyDirection , serviceName string ) (Containers , error ) {
69
93
var containers Containers
70
94
var err error
71
- if direction == fromService || (direction == toService && options .Index > 0 ) {
72
- container , err := s .getSpecifiedContainer (ctx , projectName , oneOffExclude , true , serviceName , options .Index )
95
+ switch {
96
+ case index > 0 :
97
+ container , err := s .getSpecifiedContainer (ctx , projectName , oneOffExclude , true , serviceName , index )
73
98
if err != nil {
74
- return err
99
+ return nil , err
75
100
}
76
- containers = append (containers , container )
77
- } else {
101
+ return append (containers , container ), nil
102
+ default :
78
103
containers , err = s .getContainers (ctx , projectName , oneOffExclude , true , serviceName )
79
104
if err != nil {
80
- return err
105
+ return nil , err
81
106
}
82
107
83
108
if len (containers ) < 1 {
84
- return fmt .Errorf ("no container found for service %q" , serviceName )
109
+ return nil , fmt .Errorf ("no container found for service %q" , serviceName )
85
110
}
86
- }
111
+ if direction == fromService {
112
+ return containers [:1 ], err
87
113
88
- g := errgroup.Group {}
89
- for _ , container := range containers {
90
- containerID := container .ID
91
- g .Go (func () error {
92
- switch direction {
93
- case fromService :
94
- return s .copyFromContainer (ctx , containerID , srcPath , dstPath , options )
95
- case toService :
96
- return s .copyToContainer (ctx , containerID , srcPath , dstPath , options )
97
- case acrossServices :
98
- return errors .New ("copying between services is not supported" )
99
- default :
100
- return errors .New ("unknown copy direction" )
101
- }
102
- })
114
+ }
115
+ return containers , err
103
116
}
104
-
105
- return g .Wait ()
106
117
}
107
118
108
119
func (s * composeService ) copyToContainer (ctx context.Context , containerID string , srcPath string , dstPath string , opts api.CopyOptions ) error {
0 commit comments