Explorar el Código

Merge pull request #1180 from docker/add-compose-stop

Add start and stop commands
Guillaume Tardif hace 4 años
padre
commit
c9028f9f37

+ 1 - 1
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
 }
 

+ 1 - 1
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
 }
 

+ 1 - 1
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`

+ 2 - 0
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),

+ 75 - 0
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
+}

+ 65 - 0
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
+}

+ 1 - 1
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

+ 2 - 2
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 {

+ 1 - 1
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
 }
 

+ 2 - 1
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
 }
 

+ 1 - 1
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))

+ 1 - 2
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)