Add all parameter

Signed-off-by: Julien Tant <julien@craftyx.fr>
This commit is contained in:
Julien Tant 2021-05-01 19:24:07 -07:00
parent 8f9ce9d763
commit c2f2196d30
3 changed files with 38 additions and 18 deletions

View File

@ -277,6 +277,7 @@ type PsOptions struct {
type CopyOptions struct { type CopyOptions struct {
Source string Source string
Destination string Destination string
All bool
Index int Index int
FollowLink bool FollowLink bool
CopyUIDGID bool CopyUIDGID bool

View File

@ -32,6 +32,7 @@ type copyOptions struct {
source string source string
destination string destination string
index int index int
all bool
followLink bool followLink bool
copyUIDGID bool copyUIDGID bool
} }
@ -60,7 +61,8 @@ func copyCommand(p *projectOptions, backend compose.Service) *cobra.Command {
} }
flags := copyCmd.Flags() flags := copyCmd.Flags()
flags.IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].") flags.IntVar(&opts.index, "index", 1, "Index of the container if there are multiple instances of a service [default: 1].")
flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.")
flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH") flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)") flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
@ -76,6 +78,7 @@ func runCopy(ctx context.Context, backend compose.Service, opts copyOptions) err
return backend.Copy(ctx, projects, compose.CopyOptions{ return backend.Copy(ctx, projects, compose.CopyOptions{
Source: opts.source, Source: opts.source,
Destination: opts.destination, Destination: opts.destination,
All: opts.all,
Index: opts.index, Index: opts.index,
FollowLink: opts.followLink, FollowLink: opts.followLink,
CopyUIDGID: opts.copyUIDGID, CopyUIDGID: opts.copyUIDGID,

View File

@ -24,6 +24,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"golang.org/x/sync/errgroup"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
@ -51,19 +53,25 @@ func (s *composeService) Copy(ctx context.Context, project *types.Project, opts
if srcService != "" { if srcService != "" {
direction |= fromService direction |= fromService
serviceName = srcService serviceName = srcService
// copying from multiple containers of a services doesn't make sense.
if opts.All {
return errors.New("cannot use the --all flag when copying from a service")
}
} }
if destService != "" { if destService != "" {
direction |= toService direction |= toService
serviceName = destService serviceName = destService
} }
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{ f := filters.NewArgs(
Filters: filters.NewArgs( projectFilter(project.Name),
projectFilter(project.Name), serviceFilter(serviceName),
serviceFilter(serviceName), )
filters.Arg("label", fmt.Sprintf("%s=%d", containerNumberLabel, opts.Index)), if !opts.All {
), f.Add("label", fmt.Sprintf("%s=%d", containerNumberLabel, opts.Index))
}) }
containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{Filters: f})
if err != nil { if err != nil {
return err return err
} }
@ -72,17 +80,25 @@ func (s *composeService) Copy(ctx context.Context, project *types.Project, opts
return fmt.Errorf("service %s not running", serviceName) return fmt.Errorf("service %s not running", serviceName)
} }
containerID := containers[0].ID g := errgroup.Group{}
switch direction { for i := range containers {
case fromService: containerID := containers[i].ID
return s.copyFromContainer(ctx, containerID, srcPath, dstPath, opts)
case toService: g.Go(func() error {
return s.copyToContainer(ctx, containerID, srcPath, dstPath, opts) switch direction {
case acrossServices: case fromService:
return errors.New("copying between services is not supported") return s.copyFromContainer(ctx, containerID, srcPath, dstPath, opts)
default: case toService:
return errors.New("unknown copy direction") return s.copyToContainer(ctx, containerID, srcPath, dstPath, opts)
case acrossServices:
return errors.New("copying between services is not supported")
default:
return errors.New("unknown copy direction")
}
})
} }
return g.Wait()
} }
func (s *composeService) copyToContainer(ctx context.Context, containerID string, srcPath string, dstPath string, opts compose.CopyOptions) error { func (s *composeService) copyToContainer(ctx context.Context, containerID string, srcPath string, dstPath string, opts compose.CopyOptions) error {