@@ -49,60 +49,71 @@ func (s *composeService) Copy(ctx context.Context, projectName string, options a
4949
5050 var direction copyDirection
5151 var serviceName string
52+ var copyFunc func (ctx context.Context , containerID string , srcPath string , dstPath string , opts api.CopyOptions ) error
5253 if srcService != "" {
5354 direction |= fromService
5455 serviceName = srcService
56+ copyFunc = s .copyFromContainer
5557
5658 // copying from multiple containers of a services doesn't make sense.
5759 if options .All {
5860 return errors .New ("cannot use the --all flag when copying from a service" )
5961 }
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- }
6462 }
6563 if destService != "" {
6664 direction |= toService
6765 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
6879 }
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 ) {
6993 var containers Containers
7094 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 )
7398 if err != nil {
74- return err
99+ return nil , err
75100 }
76- containers = append (containers , container )
77- } else {
101+ return append (containers , container ), nil
102+ default :
78103 containers , err = s .getContainers (ctx , projectName , oneOffExclude , true , serviceName )
79104 if err != nil {
80- return err
105+ return nil , err
81106 }
82107
83108 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 )
85110 }
86- }
111+ if direction == fromService {
112+ return containers [:1 ], err
87113
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
103116 }
104-
105- return g .Wait ()
106117}
107118
108119func (s * composeService ) copyToContainer (ctx context.Context , containerID string , srcPath string , dstPath string , opts api.CopyOptions ) error {
0 commit comments