diff --git a/aci/compose.go b/aci/compose.go index 27a7ab3d1..ab6f56c76 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) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { +func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project) error { return errdefs.ErrNotImplemented } diff --git a/api/client/compose.go b/api/client/compose.go index 6c14ff9b8..df0ee7d23 100644 --- a/api/client/compose.go +++ b/api/client/compose.go @@ -48,7 +48,7 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, cons return errdefs.ErrNotImplemented } -func (c *composeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { +func (c *composeService) Stop(ctx context.Context, project *types.Project) error { return errdefs.ErrNotImplemented } diff --git a/api/compose/api.go b/api/compose/api.go index 079492c1a..fa70c0a80 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -36,7 +36,7 @@ type Service interface { // Start executes the equivalent to a `compose start` Start(ctx context.Context, project *types.Project, consumer LogConsumer) error // Stop executes the equivalent to a `compose stop` - Stop(ctx context.Context, project *types.Project, consumer LogConsumer) error + Stop(ctx context.Context, project *types.Project) error // Up executes the equivalent to a `compose up` Up(ctx context.Context, project *types.Project, options UpOptions) error // Down executes the equivalent to a `compose down` diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index d688971a0..59b03a092 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -93,6 +93,8 @@ func Command(contextType string) *cobra.Command { command.AddCommand( upCommand(&opts, contextType), downCommand(&opts), + startCommand(&opts), + stopCommand(&opts), psCommand(&opts), listCommand(), logsCommand(&opts), diff --git a/cli/cmd/compose/start.go b/cli/cmd/compose/start.go new file mode 100644 index 000000000..670fd7094 --- /dev/null +++ b/cli/cmd/compose/start.go @@ -0,0 +1,75 @@ +/* + 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 compose + +import ( + "context" + "os" + + "github.com/spf13/cobra" + + "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/api/compose" + "github.com/docker/compose-cli/api/progress" + "github.com/docker/compose-cli/cli/formatter" +) + +type startOptions struct { + *projectOptions + Detach bool +} + +func startCommand(p *projectOptions) *cobra.Command { + opts := startOptions{ + projectOptions: p, + } + startCmd := &cobra.Command{ + Use: "start [SERVICE...]", + Short: "Start services", + RunE: func(cmd *cobra.Command, args []string) error { + return runStart(cmd.Context(), opts, args) + }, + } + + startCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background") + return startCmd +} + +func runStart(ctx context.Context, opts startOptions, services []string) error { + c, err := client.NewWithDefaultLocalBackend(ctx) + if err != nil { + return err + } + + var consumer compose.LogConsumer + if !opts.Detach { + consumer = formatter.NewLogConsumer(ctx, os.Stdout) + } + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { + project, err := opts.toProject() + if err != nil { + return "", err + } + + err = filter(project, services) + if err != nil { + return "", err + } + return "", c.ComposeService().Start(ctx, project, consumer) + }) + return err +} diff --git a/cli/cmd/compose/stop.go b/cli/cmd/compose/stop.go new file mode 100644 index 000000000..55e91f37f --- /dev/null +++ b/cli/cmd/compose/stop.go @@ -0,0 +1,65 @@ +/* + 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 compose + +import ( + "context" + + "github.com/spf13/cobra" + + "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/api/progress" +) + +type stopOptions struct { + *projectOptions +} + +func stopCommand(p *projectOptions) *cobra.Command { + opts := stopOptions{ + projectOptions: p, + } + stopCmd := &cobra.Command{ + Use: "stop [SERVICE...]", + Short: "Stop services", + RunE: func(cmd *cobra.Command, args []string) error { + return runStop(cmd.Context(), opts, args) + }, + } + return stopCmd +} + +func runStop(ctx context.Context, opts stopOptions, services []string) error { + c, err := client.NewWithDefaultLocalBackend(ctx) + if err != nil { + return err + } + + _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { + project, err := opts.toProject() + if err != nil { + return "", err + } + + err = filter(project, services) + if err != nil { + return "", err + } + return "", c.ComposeService().Stop(ctx, project) + }) + return err +} diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 089c70f25..8888774ce 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -138,7 +138,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro fmt.Println("Gracefully stopping...") ctx = context.Background() _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().Stop(ctx, project, consumer) + return "", c.ComposeService().Stop(ctx, project) }) } return err diff --git a/ecs/local/compose.go b/ecs/local/compose.go index c4e917dd9..62451e09c 100644 --- a/ecs/local/compose.go +++ b/ecs/local/compose.go @@ -57,8 +57,8 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, c return e.compose.Start(ctx, project, consumer) } -func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { - return e.compose.Stop(ctx, project, consumer) +func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error { + return e.compose.Stop(ctx, project) } func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { diff --git a/ecs/up.go b/ecs/up.go index 5b6325a43..34acbd69f 100644 --- a/ecs/up.go +++ b/ecs/up.go @@ -51,7 +51,7 @@ func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consu return errdefs.ErrNotImplemented } -func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { +func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project) error { return errdefs.ErrNotImplemented } diff --git a/kube/compose.go b/kube/compose.go index 520e389b4..d0be43d94 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -22,6 +22,7 @@ import ( "context" "github.com/compose-spec/compose-go/types" + "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/api/errdefs" @@ -86,7 +87,7 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, cons } // Stop executes the equivalent to a `compose stop` -func (s *composeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { +func (s *composeService) Stop(ctx context.Context, project *types.Project) error { return errdefs.ErrNotImplemented } diff --git a/local/compose/down.go b/local/compose/down.go index 64c857946..47581dda8 100644 --- a/local/compose/down.go +++ b/local/compose/down.go @@ -116,7 +116,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer w.Event(progress.StoppingEvent(eventName)) err := s.stopContainers(ctx, w, []moby.Container{container}) if err != nil { - w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing")) + w.Event(progress.ErrorMessageEvent(eventName, "Error while Stopping")) return err } w.Event(progress.RemovingEvent(eventName)) diff --git a/local/compose/stop.go b/local/compose/stop.go index c8c105b64..9ffd69610 100644 --- a/local/compose/stop.go +++ b/local/compose/stop.go @@ -22,14 +22,13 @@ import ( moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" - "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/progress" "github.com/compose-spec/compose-go/types" "golang.org/x/sync/errgroup" ) -func (s *composeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { +func (s *composeService) Stop(ctx context.Context, project *types.Project) error { eg, _ := errgroup.WithContext(ctx) w := progress.ContextWriter(ctx)