Add start and stop commands

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2021-01-26 18:42:49 -03:00
parent 657e89449a
commit 4ff20bdda8
12 changed files with 153 additions and 11 deletions

View File

@ -64,7 +64,7 @@ func (cs *aciComposeService) Start(ctx context.Context, project *types.Project,
return errdefs.ErrNotImplemented 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 return errdefs.ErrNotImplemented
} }

View File

@ -48,7 +48,7 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, cons
return errdefs.ErrNotImplemented 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 return errdefs.ErrNotImplemented
} }

View File

@ -36,7 +36,7 @@ type Service interface {
// Start executes the equivalent to a `compose start` // Start executes the equivalent to a `compose start`
Start(ctx context.Context, project *types.Project, consumer LogConsumer) error Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
// Stop executes the equivalent to a `compose stop` // 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 executes the equivalent to a `compose up`
Up(ctx context.Context, project *types.Project, options UpOptions) error Up(ctx context.Context, project *types.Project, options UpOptions) error
// Down executes the equivalent to a `compose down` // Down executes the equivalent to a `compose down`

View File

@ -93,6 +93,8 @@ func Command(contextType string) *cobra.Command {
command.AddCommand( command.AddCommand(
upCommand(&opts, contextType), upCommand(&opts, contextType),
downCommand(&opts), downCommand(&opts),
startCommand(&opts),
stopCommand(&opts),
psCommand(&opts), psCommand(&opts),
listCommand(), listCommand(),
logsCommand(&opts), logsCommand(&opts),

75
cli/cmd/compose/start.go Normal file
View File

@ -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
cli/cmd/compose/stop.go Normal file
View File

@ -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
}

View File

@ -117,7 +117,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
fmt.Println("Gracefully stopping...") fmt.Println("Gracefully stopping...")
ctx = context.Background() ctx = context.Background()
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, 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 return err

View File

@ -57,8 +57,8 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, c
return e.compose.Start(ctx, project, consumer) return e.compose.Start(ctx, project, consumer)
} }
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error { func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error {
return e.compose.Stop(ctx, project, consumer) return e.compose.Stop(ctx, project)
} }
func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {

View File

@ -51,7 +51,7 @@ func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consu
return errdefs.ErrNotImplemented 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 return errdefs.ErrNotImplemented
} }

View File

@ -22,6 +22,7 @@ import (
"context" "context"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/api/errdefs" "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` // 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 return errdefs.ErrNotImplemented
} }

View File

@ -116,7 +116,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
w.Event(progress.StoppingEvent(eventName)) w.Event(progress.StoppingEvent(eventName))
err := s.stopContainers(ctx, w, []moby.Container{container}) err := s.stopContainers(ctx, w, []moby.Container{container})
if err != nil { if err != nil {
w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing")) w.Event(progress.ErrorMessageEvent(eventName, "Error while Stopping"))
return err return err
} }
w.Event(progress.RemovingEvent(eventName)) w.Event(progress.RemovingEvent(eventName))

View File

@ -22,14 +22,13 @@ import (
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"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"golang.org/x/sync/errgroup" "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) eg, _ := errgroup.WithContext(ctx)
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)