Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 4ff20bd

Browse files
committed
Add start and stop commands
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 657e894 commit 4ff20bd

File tree

12 files changed

+153
-11
lines changed

12 files changed

+153
-11
lines changed

aci/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (cs *aciComposeService) Start(ctx context.Context, project *types.Project,
6464
return errdefs.ErrNotImplemented
6565
}
6666

67-
func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
67+
func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project) error {
6868
return errdefs.ErrNotImplemented
6969
}
7070

api/client/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, cons
4848
return errdefs.ErrNotImplemented
4949
}
5050

51-
func (c *composeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
51+
func (c *composeService) Stop(ctx context.Context, project *types.Project) error {
5252
return errdefs.ErrNotImplemented
5353
}
5454

api/compose/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Service interface {
3636
// Start executes the equivalent to a `compose start`
3737
Start(ctx context.Context, project *types.Project, consumer LogConsumer) error
3838
// Stop executes the equivalent to a `compose stop`
39-
Stop(ctx context.Context, project *types.Project, consumer LogConsumer) error
39+
Stop(ctx context.Context, project *types.Project) error
4040
// Up executes the equivalent to a `compose up`
4141
Up(ctx context.Context, project *types.Project, options UpOptions) error
4242
// Down executes the equivalent to a `compose down`

cli/cmd/compose/compose.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func Command(contextType string) *cobra.Command {
9393
command.AddCommand(
9494
upCommand(&opts, contextType),
9595
downCommand(&opts),
96+
startCommand(&opts),
97+
stopCommand(&opts),
9698
psCommand(&opts),
9799
listCommand(),
98100
logsCommand(&opts),

cli/cmd/compose/start.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
25+
"github.com/docker/compose-cli/api/client"
26+
"github.com/docker/compose-cli/api/compose"
27+
"github.com/docker/compose-cli/api/progress"
28+
"github.com/docker/compose-cli/cli/formatter"
29+
)
30+
31+
type startOptions struct {
32+
*projectOptions
33+
Detach bool
34+
}
35+
36+
func startCommand(p *projectOptions) *cobra.Command {
37+
opts := startOptions{
38+
projectOptions: p,
39+
}
40+
startCmd := &cobra.Command{
41+
Use: "start [SERVICE...]",
42+
Short: "Start services",
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
return runStart(cmd.Context(), opts, args)
45+
},
46+
}
47+
48+
startCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
49+
return startCmd
50+
}
51+
52+
func runStart(ctx context.Context, opts startOptions, services []string) error {
53+
c, err := client.NewWithDefaultLocalBackend(ctx)
54+
if err != nil {
55+
return err
56+
}
57+
58+
var consumer compose.LogConsumer
59+
if !opts.Detach {
60+
consumer = formatter.NewLogConsumer(ctx, os.Stdout)
61+
}
62+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
63+
project, err := opts.toProject()
64+
if err != nil {
65+
return "", err
66+
}
67+
68+
err = filter(project, services)
69+
if err != nil {
70+
return "", err
71+
}
72+
return "", c.ComposeService().Start(ctx, project, consumer)
73+
})
74+
return err
75+
}

cli/cmd/compose/stop.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
22+
"github.com/spf13/cobra"
23+
24+
"github.com/docker/compose-cli/api/client"
25+
"github.com/docker/compose-cli/api/progress"
26+
)
27+
28+
type stopOptions struct {
29+
*projectOptions
30+
}
31+
32+
func stopCommand(p *projectOptions) *cobra.Command {
33+
opts := stopOptions{
34+
projectOptions: p,
35+
}
36+
stopCmd := &cobra.Command{
37+
Use: "stop [SERVICE...]",
38+
Short: "Stop services",
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
return runStop(cmd.Context(), opts, args)
41+
},
42+
}
43+
return stopCmd
44+
}
45+
46+
func runStop(ctx context.Context, opts stopOptions, services []string) error {
47+
c, err := client.NewWithDefaultLocalBackend(ctx)
48+
if err != nil {
49+
return err
50+
}
51+
52+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
53+
project, err := opts.toProject()
54+
if err != nil {
55+
return "", err
56+
}
57+
58+
err = filter(project, services)
59+
if err != nil {
60+
return "", err
61+
}
62+
return "", c.ComposeService().Stop(ctx, project)
63+
})
64+
return err
65+
}

cli/cmd/compose/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
117117
fmt.Println("Gracefully stopping...")
118118
ctx = context.Background()
119119
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
120-
return "", c.ComposeService().Stop(ctx, project, consumer)
120+
return "", c.ComposeService().Stop(ctx, project)
121121
})
122122
}
123123
return err

ecs/local/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, c
5757
return e.compose.Start(ctx, project, consumer)
5858
}
5959

60-
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
61-
return e.compose.Stop(ctx, project, consumer)
60+
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project) error {
61+
return e.compose.Stop(ctx, project)
6262
}
6363

6464
func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {

ecs/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (b *ecsAPIService) Start(ctx context.Context, project *types.Project, consu
5151
return errdefs.ErrNotImplemented
5252
}
5353

54-
func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
54+
func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project) error {
5555
return errdefs.ErrNotImplemented
5656
}
5757

kube/compose.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323

2424
"github.com/compose-spec/compose-go/types"
25+
2526
"github.com/docker/compose-cli/api/compose"
2627
"github.com/docker/compose-cli/api/context/store"
2728
"github.com/docker/compose-cli/api/errdefs"
@@ -86,7 +87,7 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, cons
8687
}
8788

8889
// Stop executes the equivalent to a `compose stop`
89-
func (s *composeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
90+
func (s *composeService) Stop(ctx context.Context, project *types.Project) error {
9091
return errdefs.ErrNotImplemented
9192
}
9293

0 commit comments

Comments
 (0)