compose exec doesn't need project

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-22 15:15:33 +02:00 committed by Nicolas De loof
parent 9b055c31fe
commit 244834ff12
4 changed files with 11 additions and 34 deletions

View File

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"os" "os"
cgo "github.com/compose-spec/compose-go/cli"
"github.com/containerd/console" "github.com/containerd/console"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/spf13/cobra" "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 { 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 { if err != nil {
return err return err
} }

View File

@ -61,7 +61,7 @@ type Service interface {
// Remove executes the equivalent to a `compose rm` // Remove executes the equivalent to a `compose rm`
Remove(ctx context.Context, project *types.Project, options RemoveOptions) error Remove(ctx context.Context, project *types.Project, options RemoveOptions) error
// Exec executes a command in a running service container // 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 copies a file/folder between a service container and the local filesystem
Copy(ctx context.Context, project *types.Project, opts CopyOptions) error Copy(ctx context.Context, project *types.Project, opts CopyOptions) error
// Pause executes the equivalent to a `compose pause` // Pause executes the equivalent to a `compose pause`

View File

@ -40,7 +40,7 @@ type ServiceProxy struct {
KillFn func(ctx context.Context, project *types.Project, options KillOptions) error KillFn func(ctx context.Context, project *types.Project, options KillOptions) error
RunOneOffContainerFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error) RunOneOffContainerFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
RemoveFn func(ctx context.Context, project *types.Project, options RemoveOptions) 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 CopyFn func(ctx context.Context, project *types.Project, opts CopyOptions) error
PauseFn func(ctx context.Context, project string, options PauseOptions) error PauseFn func(ctx context.Context, project string, options PauseOptions) error
UnPauseFn 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 // 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 { if s.ExecFn == nil {
return 0, ErrNotImplemented return 0, ErrNotImplemented
} }
for _, i := range s.interceptors {
i(ctx, project)
}
return s.ExecFn(ctx, project, options) return s.ExecFn(ctx, project, options)
} }

View File

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"io" "io"
"github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/streams"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
@ -31,20 +30,15 @@ import (
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
) )
func (s *composeService) Exec(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) { func (s *composeService) Exec(ctx context.Context, project string, opts api.RunOptions) (int, error) {
service, err := project.GetService(opts.Service) container, err := s.getExecTarget(ctx, project, opts)
if err != nil {
return 0, err
}
container, err := s.getExecTarget(ctx, project, service, opts)
if err != nil { if err != nil {
return 0, err return 0, err
} }
exec, err := s.apiClient.ContainerExecCreate(ctx, container.ID, moby.ExecConfig{ exec, err := s.apiClient.ContainerExecCreate(ctx, container.ID, moby.ExecConfig{
Cmd: opts.Command, Cmd: opts.Command,
Env: s.getExecEnvironment(project, service, opts), Env: opts.Environment,
User: opts.User, User: opts.User,
Privileged: opts.Privileged, Privileged: opts.Privileged,
Tty: opts.Tty, 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{ containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: filters.NewArgs( Filters: filters.NewArgs(
projectFilter(project.Name), projectFilter(projectName),
serviceFilter(service.Name), serviceFilter(opts.Service),
containerNumberFilter(opts.Index), containerNumberFilter(opts.Index),
), ),
}) })
@ -157,25 +151,12 @@ func (s *composeService) getExecTarget(ctx context.Context, project *types.Proje
return moby.Container{}, err return moby.Container{}, err
} }
if len(containers) < 1 { 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] container := containers[0]
return container, nil 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) { func (s *composeService) getExecExitStatus(ctx context.Context, execID string) (int, error) {
resp, err := s.apiClient.ContainerExecInspect(ctx, execID) resp, err := s.apiClient.ContainerExecInspect(ctx, execID)
if err != nil { if err != nil {