| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 | /*   Copyright 2020 Docker Compose CLI authors   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.*/package apiimport (	"context"	"github.com/compose-spec/compose-go/types")// ServiceProxy implements Service by delegating to implementation functions. This allows lazy init and per-method overridestype ServiceProxy struct {	BuildFn              func(ctx context.Context, project *types.Project, options BuildOptions) error	PushFn               func(ctx context.Context, project *types.Project, options PushOptions) error	PullFn               func(ctx context.Context, project *types.Project, opts PullOptions) error	CreateFn             func(ctx context.Context, project *types.Project, opts CreateOptions) error	StartFn              func(ctx context.Context, projectName string, options StartOptions) error	RestartFn            func(ctx context.Context, projectName string, options RestartOptions) error	StopFn               func(ctx context.Context, projectName string, options StopOptions) error	UpFn                 func(ctx context.Context, project *types.Project, options UpOptions) error	DownFn               func(ctx context.Context, projectName string, options DownOptions) error	LogsFn               func(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error	PsFn                 func(ctx context.Context, projectName string, options PsOptions) ([]ContainerSummary, error)	ListFn               func(ctx context.Context, options ListOptions) ([]Stack, error)	ConvertFn            func(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)	KillFn               func(ctx context.Context, project string, options KillOptions) error	RunOneOffContainerFn func(ctx context.Context, project *types.Project, opts RunOptions) (int, error)	RemoveFn             func(ctx context.Context, project string, options RemoveOptions) error	ExecFn               func(ctx context.Context, project string, opts RunOptions) (int, error)	CopyFn               func(ctx context.Context, project string, options CopyOptions) error	PauseFn              func(ctx context.Context, project string, options PauseOptions) error	UnPauseFn            func(ctx context.Context, project string, options PauseOptions) error	TopFn                func(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error)	EventsFn             func(ctx context.Context, project string, options EventsOptions) error	PortFn               func(ctx context.Context, project string, service string, port int, options PortOptions) (string, int, error)	ImagesFn             func(ctx context.Context, projectName string, options ImagesOptions) ([]ImageSummary, error)	interceptors         []Interceptor}// NewServiceProxy produces a ServiceProxyfunc NewServiceProxy() *ServiceProxy {	return &ServiceProxy{}}// Interceptor allow to customize the compose types.Project before the actual Service method is executedtype Interceptor func(ctx context.Context, project *types.Project)var _ Service = &ServiceProxy{}// WithService configure proxy to use specified Service as delegatefunc (s *ServiceProxy) WithService(service Service) *ServiceProxy {	s.BuildFn = service.Build	s.PushFn = service.Push	s.PullFn = service.Pull	s.CreateFn = service.Create	s.StartFn = service.Start	s.RestartFn = service.Restart	s.StopFn = service.Stop	s.UpFn = service.Up	s.DownFn = service.Down	s.LogsFn = service.Logs	s.PsFn = service.Ps	s.ListFn = service.List	s.ConvertFn = service.Convert	s.KillFn = service.Kill	s.RunOneOffContainerFn = service.RunOneOffContainer	s.RemoveFn = service.Remove	s.ExecFn = service.Exec	s.CopyFn = service.Copy	s.PauseFn = service.Pause	s.UnPauseFn = service.UnPause	s.TopFn = service.Top	s.EventsFn = service.Events	s.PortFn = service.Port	s.ImagesFn = service.Images	return s}// WithInterceptor configures Interceptor to be applied to Service method executionfunc (s *ServiceProxy) WithInterceptor(interceptors ...Interceptor) *ServiceProxy {	s.interceptors = append(s.interceptors, interceptors...)	return s}// Build implements Service interfacefunc (s *ServiceProxy) Build(ctx context.Context, project *types.Project, options BuildOptions) error {	if s.BuildFn == nil {		return ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.BuildFn(ctx, project, options)}// Push implements Service interfacefunc (s *ServiceProxy) Push(ctx context.Context, project *types.Project, options PushOptions) error {	if s.PushFn == nil {		return ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.PushFn(ctx, project, options)}// Pull implements Service interfacefunc (s *ServiceProxy) Pull(ctx context.Context, project *types.Project, options PullOptions) error {	if s.PullFn == nil {		return ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.PullFn(ctx, project, options)}// Create implements Service interfacefunc (s *ServiceProxy) Create(ctx context.Context, project *types.Project, options CreateOptions) error {	if s.CreateFn == nil {		return ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.CreateFn(ctx, project, options)}// Start implements Service interfacefunc (s *ServiceProxy) Start(ctx context.Context, projectName string, options StartOptions) error {	if s.StartFn == nil {		return ErrNotImplemented	}	return s.StartFn(ctx, projectName, options)}// Restart implements Service interfacefunc (s *ServiceProxy) Restart(ctx context.Context, projectName string, options RestartOptions) error {	if s.RestartFn == nil {		return ErrNotImplemented	}	return s.RestartFn(ctx, projectName, options)}// Stop implements Service interfacefunc (s *ServiceProxy) Stop(ctx context.Context, projectName string, options StopOptions) error {	if s.StopFn == nil {		return ErrNotImplemented	}	return s.StopFn(ctx, projectName, options)}// Up implements Service interfacefunc (s *ServiceProxy) Up(ctx context.Context, project *types.Project, options UpOptions) error {	if s.UpFn == nil {		return ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.UpFn(ctx, project, options)}// Down implements Service interfacefunc (s *ServiceProxy) Down(ctx context.Context, project string, options DownOptions) error {	if s.DownFn == nil {		return ErrNotImplemented	}	return s.DownFn(ctx, project, options)}// Logs implements Service interfacefunc (s *ServiceProxy) Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error {	if s.LogsFn == nil {		return ErrNotImplemented	}	return s.LogsFn(ctx, projectName, consumer, options)}// Ps implements Service interfacefunc (s *ServiceProxy) Ps(ctx context.Context, project string, options PsOptions) ([]ContainerSummary, error) {	if s.PsFn == nil {		return nil, ErrNotImplemented	}	return s.PsFn(ctx, project, options)}// List implements Service interfacefunc (s *ServiceProxy) List(ctx context.Context, options ListOptions) ([]Stack, error) {	if s.ListFn == nil {		return nil, ErrNotImplemented	}	return s.ListFn(ctx, options)}// Convert implements Service interfacefunc (s *ServiceProxy) Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error) {	if s.ConvertFn == nil {		return nil, ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.ConvertFn(ctx, project, options)}// Kill implements Service interfacefunc (s *ServiceProxy) Kill(ctx context.Context, projectName string, options KillOptions) error {	if s.KillFn == nil {		return ErrNotImplemented	}	return s.KillFn(ctx, projectName, options)}// RunOneOffContainer implements Service interfacefunc (s *ServiceProxy) RunOneOffContainer(ctx context.Context, project *types.Project, options RunOptions) (int, error) {	if s.RunOneOffContainerFn == nil {		return 0, ErrNotImplemented	}	for _, i := range s.interceptors {		i(ctx, project)	}	return s.RunOneOffContainerFn(ctx, project, options)}// Remove implements Service interfacefunc (s *ServiceProxy) Remove(ctx context.Context, projectName string, options RemoveOptions) error {	if s.RemoveFn == nil {		return ErrNotImplemented	}	return s.RemoveFn(ctx, projectName, options)}// Exec implements Service interfacefunc (s *ServiceProxy) Exec(ctx context.Context, projectName string, options RunOptions) (int, error) {	if s.ExecFn == nil {		return 0, ErrNotImplemented	}	return s.ExecFn(ctx, projectName, options)}// Copy implements Service interfacefunc (s *ServiceProxy) Copy(ctx context.Context, projectName string, options CopyOptions) error {	if s.CopyFn == nil {		return ErrNotImplemented	}	return s.CopyFn(ctx, projectName, options)}// Pause implements Service interfacefunc (s *ServiceProxy) Pause(ctx context.Context, projectName string, options PauseOptions) error {	if s.PauseFn == nil {		return ErrNotImplemented	}	return s.PauseFn(ctx, projectName, options)}// UnPause implements Service interfacefunc (s *ServiceProxy) UnPause(ctx context.Context, projectName string, options PauseOptions) error {	if s.UnPauseFn == nil {		return ErrNotImplemented	}	return s.UnPauseFn(ctx, projectName, options)}// Top implements Service interfacefunc (s *ServiceProxy) Top(ctx context.Context, project string, services []string) ([]ContainerProcSummary, error) {	if s.TopFn == nil {		return nil, ErrNotImplemented	}	return s.TopFn(ctx, project, services)}// Events implements Service interfacefunc (s *ServiceProxy) Events(ctx context.Context, projectName string, options EventsOptions) error {	if s.EventsFn == nil {		return ErrNotImplemented	}	return s.EventsFn(ctx, projectName, options)}// Port implements Service interfacefunc (s *ServiceProxy) Port(ctx context.Context, projectName string, service string, port int, options PortOptions) (string, int, error) {	if s.PortFn == nil {		return "", 0, ErrNotImplemented	}	return s.PortFn(ctx, projectName, service, port, options)}// Images implements Service interfacefunc (s *ServiceProxy) Images(ctx context.Context, project string, options ImagesOptions) ([]ImageSummary, error) {	if s.ImagesFn == nil {		return nil, ErrNotImplemented	}	return s.ImagesFn(ctx, project, options)}
 |