diff --git a/aci/compose.go b/aci/compose.go index 94fed7b21..92b5d76c2 100644 --- a/aci/compose.go +++ b/aci/compose.go @@ -64,7 +64,7 @@ func (cs *aciComposeService) Start(ctx context.Context, project *types.Project, return errdefs.ErrNotImplemented } -func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, detach bool) error { +func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { logrus.Debugf("Up on project with name %q", project.Name) if err := autocreateFileshares(ctx, project); err != nil { @@ -102,7 +102,7 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam return nil } -func (cs *aciComposeService) Down(ctx context.Context, projectName string, removeOrphans bool) error { +func (cs *aciComposeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { logrus.Debugf("Down on projectName with name %q", projectName) if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil { @@ -198,6 +198,6 @@ func (cs *aciComposeService) Logs(ctx context.Context, projectName string, consu return errdefs.ErrNotImplemented } -func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { +func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { return nil, errdefs.ErrNotImplemented } diff --git a/api/client/compose.go b/api/client/compose.go index 78578e3cc..aac8d56eb 100644 --- a/api/client/compose.go +++ b/api/client/compose.go @@ -48,11 +48,11 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, cons return errdefs.ErrNotImplemented } -func (c *composeService) Up(context.Context, *types.Project, bool) error { +func (c *composeService) Up(context.Context, *types.Project, compose.UpOptions) error { return errdefs.ErrNotImplemented } -func (c *composeService) Down(context.Context, string, bool) error { +func (c *composeService) Down(context.Context, string, compose.DownOptions) error { return errdefs.ErrNotImplemented } @@ -68,6 +68,6 @@ func (c *composeService) List(context.Context, string) ([]compose.Stack, error) return nil, errdefs.ErrNotImplemented } -func (c *composeService) Convert(context.Context, *types.Project, string) ([]byte, error) { +func (c *composeService) Convert(context.Context, *types.Project, compose.ConvertOptions) ([]byte, error) { return nil, errdefs.ErrNotImplemented } diff --git a/api/compose/api.go b/api/compose/api.go index a1ba9f21c..5687cc019 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -35,9 +35,9 @@ type Service interface { // Start executes the equivalent to a `compose start` Start(ctx context.Context, project *types.Project, consumer LogConsumer) error // Up executes the equivalent to a `compose up` - Up(ctx context.Context, project *types.Project, detach bool) error + Up(ctx context.Context, project *types.Project, options UpOptions) error // Down executes the equivalent to a `compose down` - Down(ctx context.Context, projectName string, removeOrphans bool) error + Down(ctx context.Context, projectName string, options DownOptions) error // Logs executes the equivalent to a `compose logs` Logs(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error // Ps executes the equivalent to a `compose ps` @@ -45,7 +45,25 @@ type Service interface { // List executes the equivalent to a `docker stack ls` List(ctx context.Context, projectName string) ([]Stack, error) // Convert translate compose model into backend's native format - Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) + Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error) +} + +// UpOptions group options of the Up API +type UpOptions struct { + // Detach will create services and return immediately + Detach bool +} + +// DownOptions group options of the Down API +type DownOptions struct { + // RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels + RemoveOrphans bool +} + +// ConvertOptions group options of the Convert API +type ConvertOptions struct { + // Format define the output format used to dump converted application model (json|yaml) + Format string } // PortPublisher hold status about published port diff --git a/cli/cmd/compose/convert.go b/cli/cmd/compose/convert.go index 29207ad56..5128155a0 100644 --- a/cli/cmd/compose/convert.go +++ b/cli/cmd/compose/convert.go @@ -20,6 +20,8 @@ import ( "context" "fmt" + "github.com/docker/compose-cli/api/compose" + "github.com/compose-spec/compose-go/cli" "github.com/spf13/cobra" @@ -61,7 +63,9 @@ func runConvert(ctx context.Context, opts composeOptions) error { return err } - json, err = c.ComposeService().Convert(ctx, project, opts.Format) + json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{ + Format: opts.Format, + }) if err != nil { return err } diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index a4b12f870..05715760f 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -19,6 +19,8 @@ package compose import ( "context" + "github.com/docker/compose-cli/api/compose" + "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" @@ -52,7 +54,9 @@ func runDown(ctx context.Context, opts composeOptions) error { if err != nil { return "", err } - return projectName, c.ComposeService().Down(ctx, projectName, false) + return projectName, c.ComposeService().Down(ctx, projectName, compose.DownOptions{ + RemoveOrphans: false, + }) }) return err } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 4b971581c..a74e352ba 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -68,7 +68,9 @@ func runUp(ctx context.Context, opts composeOptions, services []string) error { } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().Up(ctx, project, opts.Detach) + return "", c.ComposeService().Up(ctx, project, compose.UpOptions{ + Detach: opts.Detach, + }) }) return err } @@ -96,7 +98,7 @@ func runCreateStart(ctx context.Context, opts composeOptions, services []string) fmt.Println("Gracefully stopping...") ctx = context.Background() _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().Down(ctx, project.Name, false) + return "", c.ComposeService().Down(ctx, project.Name, compose.DownOptions{}) }) } return err diff --git a/ecs/cloudformation.go b/ecs/cloudformation.go index 0c52fba2d..f82b0b5f5 100644 --- a/ecs/cloudformation.go +++ b/ecs/cloudformation.go @@ -23,6 +23,8 @@ import ( "regexp" "strings" + "github.com/docker/compose-cli/api/compose" + ecsapi "github.com/aws/aws-sdk-go/service/ecs" "github.com/aws/aws-sdk-go/service/elbv2" cloudmapapi "github.com/aws/aws-sdk-go/service/servicediscovery" @@ -37,13 +39,13 @@ import ( "github.com/compose-spec/compose-go/types" ) -func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { +func (b *ecsAPIService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { template, err := b.convert(ctx, project) if err != nil { return nil, err } - return marshall(template, format) + return marshall(template, options.Format) } func (b *ecsAPIService) convert(ctx context.Context, project *types.Project) (*cloudformation.Template, error) { diff --git a/ecs/down.go b/ecs/down.go index 6ad4dee42..047613adc 100644 --- a/ecs/down.go +++ b/ecs/down.go @@ -19,10 +19,12 @@ package ecs import ( "context" + "github.com/docker/compose-cli/api/compose" + "github.com/docker/compose-cli/progress" ) -func (b *ecsAPIService) Down(ctx context.Context, projectName string, removeOrphans bool) error { +func (b *ecsAPIService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { resources, err := b.aws.ListStackResources(ctx, projectName) if err != nil { return err diff --git a/ecs/local/compose.go b/ecs/local/compose.go index 54a6dcd2e..1dbaa7fe4 100644 --- a/ecs/local/compose.go +++ b/ecs/local/compose.go @@ -55,11 +55,11 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, c return e.compose.Start(ctx, project, consumer) } -func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, detach bool) error { +func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { return errdefs.ErrNotImplemented } -func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { +func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { enhanced, err := e.enhanceForLocalSimulation(project) if err != nil { return nil, err @@ -73,13 +73,13 @@ func (e ecsLocalSimulation) Convert(ctx context.Context, project *types.Project, "secrets": enhanced.Secrets, "configs": enhanced.Configs, } - switch format { + switch options.Format { case "json": return json.MarshalIndent(config, "", " ") case "yaml": return yaml.Marshal(config) default: - return nil, fmt.Errorf("unsupported format %q", format) + return nil, fmt.Errorf("unsupported format %q", options) } } @@ -147,11 +147,12 @@ func (e ecsLocalSimulation) enhanceForLocalSimulation(project *types.Project) (* return project, nil } -func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, removeOrphans bool) error { - return e.compose.Down(ctx, projectName, true) +func (e ecsLocalSimulation) Down(ctx context.Context, projectName string, options compose.DownOptions) error { + options.RemoveOrphans = true + return e.compose.Down(ctx, projectName, options) } -func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options componse.LogOptions) error { +func (e ecsLocalSimulation) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error { return e.compose.Logs(ctx, projectName, consumer, options) } diff --git a/ecs/up.go b/ecs/up.go index 8f3240471..a15d9303a 100644 --- a/ecs/up.go +++ b/ecs/up.go @@ -49,14 +49,14 @@ func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consu return errdefs.ErrNotImplemented } -func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach bool) error { +func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { err := b.aws.CheckRequirements(ctx, b.Region) if err != nil { return err } - template, err := b.Convert(ctx, project, "yaml") + template, err := b.Convert(ctx, project, compose.ConvertOptions{Format: "yaml"}) if err != nil { return err } @@ -82,7 +82,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b return err } } - if detach { + if options.Detach { return nil } signalChan := make(chan os.Signal, 1) @@ -90,7 +90,7 @@ func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach b go func() { <-signalChan fmt.Println("user interrupted deployment. Deleting stack...") - b.Down(ctx, project.Name, false) // nolint:errcheck + b.Down(ctx, project.Name, compose.DownOptions{}) // nolint:errcheck }() err = b.WaitStackCompletion(ctx, project.Name, operation) diff --git a/example/backend.go b/example/backend.go index 39422a5a8..9e585bd39 100644 --- a/example/backend.go +++ b/example/backend.go @@ -158,13 +158,13 @@ func (cs *composeService) Start(ctx context.Context, project *types.Project, con return errdefs.ErrNotImplemented } -func (cs *composeService) Up(ctx context.Context, project *types.Project, detach bool) error { +func (cs *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { fmt.Printf("Up command on project %q", project.Name) return nil } -func (cs *composeService) Down(ctx context.Context, projectName string, removeOrphans bool) error { +func (cs *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { fmt.Printf("Down command on project %q", projectName) return nil } @@ -179,6 +179,6 @@ func (cs *composeService) Logs(ctx context.Context, projectName string, consumer return errdefs.ErrNotImplemented } -func (cs *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { +func (cs *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { return nil, errdefs.ErrNotImplemented } diff --git a/local/compose/compose.go b/local/compose/compose.go index 6d220c649..402f3f0e0 100644 --- a/local/compose/compose.go +++ b/local/compose/compose.go @@ -40,7 +40,7 @@ type composeService struct { apiClient *client.Client } -func (s *composeService) Up(ctx context.Context, project *types.Project, detach bool) error { +func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { return errdefs2.ErrNotImplemented } @@ -54,13 +54,13 @@ func getContainerName(c moby.Container) string { return c.Names[0][1:] } -func (s *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) { - switch format { +func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { + switch options.Format { case "json": return json.MarshalIndent(project, "", " ") case "yaml": return yaml.Marshal(project) default: - return nil, fmt.Errorf("unsupported format %q", format) + return nil, fmt.Errorf("unsupported format %q", options) } } diff --git a/local/compose/down.go b/local/compose/down.go index 7fd285b6e..0227ddb2d 100644 --- a/local/compose/down.go +++ b/local/compose/down.go @@ -21,6 +21,8 @@ import ( "path/filepath" "strings" + "github.com/docker/compose-cli/api/compose" + "github.com/docker/compose-cli/progress" "github.com/compose-spec/compose-go/cli" @@ -30,7 +32,7 @@ import ( "golang.org/x/sync/errgroup" ) -func (s *composeService) Down(ctx context.Context, projectName string, removeOrphans bool) error { +func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { eg, _ := errgroup.WithContext(ctx) w := progress.ContextWriter(ctx) @@ -54,7 +56,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, removeOrp return err }) - if removeOrphans { + if options.RemoveOrphans { err := s.removeContainers(ctx, w, eg, containers) if err != nil { return err diff --git a/server/proxy/compose.go b/server/proxy/compose.go index 348d4a0d0..b3b5f8eec 100644 --- a/server/proxy/compose.go +++ b/server/proxy/compose.go @@ -19,6 +19,8 @@ package proxy import ( "context" + "github.com/docker/compose-cli/api/compose" + "github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/types" @@ -30,7 +32,8 @@ func (p *proxy) Up(ctx context.Context, request *composev1.ComposeUpRequest) (*c if err != nil { return nil, err } - return &composev1.ComposeUpResponse{ProjectName: project.Name}, Client(ctx).ComposeService().Up(ctx, project, true) + err = Client(ctx).ComposeService().Up(ctx, project, compose.UpOptions{Detach: true}) + return &composev1.ComposeUpResponse{ProjectName: project.Name}, err } func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest) (*composev1.ComposeDownResponse, error) { @@ -42,7 +45,8 @@ func (p *proxy) Down(ctx context.Context, request *composev1.ComposeDownRequest) } projectName = project.Name } - return &composev1.ComposeDownResponse{ProjectName: projectName}, Client(ctx).ComposeService().Down(ctx, projectName, false) + err := Client(ctx).ComposeService().Down(ctx, projectName, compose.DownOptions{}) + return &composev1.ComposeDownResponse{ProjectName: projectName}, err } func (p *proxy) Services(ctx context.Context, request *composev1.ComposeServicesRequest) (*composev1.ComposeServicesResponse, error) {