From 244834ff12c10fc4d82879bf9d7559080f3dcd75 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 22 Sep 2021 15:15:33 +0200 Subject: [PATCH] compose exec doesn't need project Signed-off-by: Nicolas De Loof --- cmd/compose/exec.go | 3 +-- pkg/api/api.go | 2 +- pkg/api/proxy.go | 7 ++----- pkg/compose/exec.go | 33 +++++++-------------------------- 4 files changed, 11 insertions(+), 34 deletions(-) diff --git a/cmd/compose/exec.go b/cmd/compose/exec.go index 9d2370475..f5be30985 100644 --- a/cmd/compose/exec.go +++ b/cmd/compose/exec.go @@ -21,7 +21,6 @@ import ( "fmt" "os" - cgo "github.com/compose-spec/compose-go/cli" "github.com/containerd/console" "github.com/docker/cli/cli" "github.com/spf13/cobra" @@ -78,7 +77,7 @@ func execCommand(p *projectOptions, backend api.Service) *cobra.Command { } func runExec(ctx context.Context, backend api.Service, opts execOpts) error { - project, err := opts.toProject(nil, cgo.WithResolvedPaths(true)) + project, err := opts.toProjectName() if err != nil { return err } diff --git a/pkg/api/api.go b/pkg/api/api.go index 1bd1e78f8..992997b8e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -61,7 +61,7 @@ type Service interface { // Remove executes the equivalent to a `compose rm` Remove(ctx context.Context, project *types.Project, options RemoveOptions) error // Exec executes a command in a running service container - Exec(ctx context.Context, project *types.Project, opts RunOptions) (int, error) + Exec(ctx context.Context, project string, opts RunOptions) (int, error) // Copy copies a file/folder between a service container and the local filesystem Copy(ctx context.Context, project *types.Project, opts CopyOptions) error // Pause executes the equivalent to a `compose pause` diff --git a/pkg/api/proxy.go b/pkg/api/proxy.go index 03163a73e..aff81e1f4 100644 --- a/pkg/api/proxy.go +++ b/pkg/api/proxy.go @@ -40,7 +40,7 @@ type ServiceProxy struct { KillFn func(ctx context.Context, project *types.Project, options KillOptions) error RunOneOffContainerFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error) RemoveFn func(ctx context.Context, project *types.Project, options RemoveOptions) error - ExecFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error) + ExecFn func(ctx context.Context, project string, opts RunOptions) (int, error) CopyFn func(ctx context.Context, project *types.Project, opts CopyOptions) error PauseFn func(ctx context.Context, project string, options PauseOptions) error UnPauseFn func(ctx context.Context, project string, options PauseOptions) error @@ -261,13 +261,10 @@ func (s *ServiceProxy) Remove(ctx context.Context, project *types.Project, optio } // Exec implements Service interface -func (s *ServiceProxy) Exec(ctx context.Context, project *types.Project, options RunOptions) (int, error) { +func (s *ServiceProxy) Exec(ctx context.Context, project string, options RunOptions) (int, error) { if s.ExecFn == nil { return 0, ErrNotImplemented } - for _, i := range s.interceptors { - i(ctx, project) - } return s.ExecFn(ctx, project, options) } diff --git a/pkg/compose/exec.go b/pkg/compose/exec.go index 74b14a6e2..6bd4b608f 100644 --- a/pkg/compose/exec.go +++ b/pkg/compose/exec.go @@ -21,7 +21,6 @@ import ( "fmt" "io" - "github.com/compose-spec/compose-go/types" "github.com/docker/cli/cli/streams" moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -31,20 +30,15 @@ import ( "github.com/docker/compose/v2/pkg/api" ) -func (s *composeService) Exec(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) { - service, err := project.GetService(opts.Service) - if err != nil { - return 0, err - } - - container, err := s.getExecTarget(ctx, project, service, opts) +func (s *composeService) Exec(ctx context.Context, project string, opts api.RunOptions) (int, error) { + container, err := s.getExecTarget(ctx, project, opts) if err != nil { return 0, err } exec, err := s.apiClient.ContainerExecCreate(ctx, container.ID, moby.ExecConfig{ Cmd: opts.Command, - Env: s.getExecEnvironment(project, service, opts), + Env: opts.Environment, User: opts.User, Privileged: opts.Privileged, Tty: opts.Tty, @@ -145,11 +139,11 @@ func (s *composeService) interactiveExec(ctx context.Context, opts api.RunOption } } -func (s *composeService) getExecTarget(ctx context.Context, project *types.Project, service types.ServiceConfig, opts api.RunOptions) (moby.Container, error) { +func (s *composeService) getExecTarget(ctx context.Context, projectName string, opts api.RunOptions) (moby.Container, error) { containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ Filters: filters.NewArgs( - projectFilter(project.Name), - serviceFilter(service.Name), + projectFilter(projectName), + serviceFilter(opts.Service), containerNumberFilter(opts.Index), ), }) @@ -157,25 +151,12 @@ func (s *composeService) getExecTarget(ctx context.Context, project *types.Proje return moby.Container{}, err } if len(containers) < 1 { - return moby.Container{}, fmt.Errorf("container %s not running", getContainerName(project.Name, service, opts.Index)) + return moby.Container{}, fmt.Errorf("service %q is not running container #%d", opts.Service, opts.Index) } container := containers[0] return container, nil } -func (s *composeService) getExecEnvironment(project *types.Project, service types.ServiceConfig, opts api.RunOptions) []string { - var env []string - for k, v := range service.Environment.OverrideBy(types.NewMappingWithEquals(opts.Environment)). - Resolve(func(s string) (string, bool) { - v, ok := project.Environment[s] - return v, ok - }). - RemoveEmpty() { - env = append(env, fmt.Sprintf("%s=%s", k, *v)) - } - return env -} - func (s *composeService) getExecExitStatus(ctx context.Context, execID string) (int, error) { resp, err := s.apiClient.ContainerExecInspect(ctx, execID) if err != nil {