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

Commit e0344ea

Browse files
authored
Merge pull request #1415 from ulyssessouza/add-restart
Add restart command
2 parents 3366131 + 1926820 commit e0344ea

File tree

17 files changed

+281
-0
lines changed

17 files changed

+281
-0
lines changed

aci/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func (cs *aciComposeService) Start(ctx context.Context, project *types.Project,
6464
return errdefs.ErrNotImplemented
6565
}
6666

67+
func (cs *aciComposeService) Restart(ctx context.Context, project *types.Project, options compose.RestartOptions) error {
68+
return errdefs.ErrNotImplemented
69+
}
70+
6771
func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
6872
return errdefs.ErrNotImplemented
6973
}

api/client/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (c *composeService) Start(ctx context.Context, project *types.Project, opti
4848
return errdefs.ErrNotImplemented
4949
}
5050

51+
func (c *composeService) Restart(ctx context.Context, project *types.Project, options compose.RestartOptions) error {
52+
return errdefs.ErrNotImplemented
53+
}
54+
5155
func (c *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
5256
return errdefs.ErrNotImplemented
5357
}

api/compose/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Service interface {
3838
Create(ctx context.Context, project *types.Project, opts CreateOptions) error
3939
// Start executes the equivalent to a `compose start`
4040
Start(ctx context.Context, project *types.Project, options StartOptions) error
41+
// Restart restarts containers
42+
Restart(ctx context.Context, project *types.Project, options RestartOptions) error
4143
// Stop executes the equivalent to a `compose stop`
4244
Stop(ctx context.Context, project *types.Project, options StopOptions) error
4345
// Up executes the equivalent to a `compose up`
@@ -106,6 +108,12 @@ type StartOptions struct {
106108
Services []string
107109
}
108110

111+
// RestartOptions group options of the Restart API
112+
type RestartOptions struct {
113+
// Timeout override container restart timeout
114+
Timeout *time.Duration
115+
}
116+
109117
// StopOptions group options of the Stop API
110118
type StopOptions struct {
111119
// Timeout override container stop timeout

api/progress/event.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func StartedEvent(ID string) Event {
6868
return NewEvent(ID, Done, "Started")
6969
}
7070

71+
// RestartingEvent creates a new Restarting in progress Event
72+
func RestartingEvent(ID string) Event {
73+
return NewEvent(ID, Working, "Restarting")
74+
}
75+
76+
// RestartedEvent creates a new Restarted in progress Event
77+
func RestartedEvent(ID string) Event {
78+
return NewEvent(ID, Done, "Restarted")
79+
}
80+
7181
// RunningEvent creates a new Running in progress Event
7282
func RunningEvent(ID string) Event {
7383
return NewEvent(ID, Done, "Running")

cli/cmd/compose/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func Command(contextType string) *cobra.Command {
127127
upCommand(&opts, contextType),
128128
downCommand(&opts, contextType),
129129
startCommand(&opts),
130+
restartCommand(&opts),
130131
stopCommand(&opts),
131132
psCommand(&opts),
132133
listCommand(contextType),

cli/cmd/compose/restart.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
"time"
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+
)
29+
30+
type restartOptions struct {
31+
*projectOptions
32+
timeout int
33+
}
34+
35+
func restartCommand(p *projectOptions) *cobra.Command {
36+
opts := restartOptions{
37+
projectOptions: p,
38+
}
39+
restartCmd := &cobra.Command{
40+
Use: "restart",
41+
Short: "Restart containers",
42+
RunE: func(cmd *cobra.Command, args []string) error {
43+
return runRestart(cmd.Context(), opts, args)
44+
},
45+
}
46+
flags := restartCmd.Flags()
47+
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
48+
49+
return restartCmd
50+
}
51+
52+
func runRestart(ctx context.Context, opts restartOptions, services []string) error {
53+
c, err := client.New(ctx)
54+
if err != nil {
55+
return err
56+
}
57+
58+
project, err := opts.toProject(services)
59+
if err != nil {
60+
return err
61+
}
62+
63+
timeout := time.Duration(opts.timeout) * time.Second
64+
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
65+
return "", c.ComposeService().Restart(ctx, project, compose.RestartOptions{
66+
Timeout: &timeout,
67+
})
68+
})
69+
return err
70+
}

docs/reference/compose_restart.md

Whitespace-only changes.

docs/reference/docker_compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ cname:
6262
- docker compose ps
6363
- docker compose pull
6464
- docker compose push
65+
- docker compose restart
6566
- docker compose rm
6667
- docker compose run
6768
- docker compose start
@@ -83,6 +84,7 @@ clink:
8384
- docker_compose_ps.yaml
8485
- docker_compose_pull.yaml
8586
- docker_compose_push.yaml
87+
- docker_compose_restart.yaml
8688
- docker_compose_rm.yaml
8789
- docker_compose_run.yaml
8890
- docker_compose_start.yaml
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
command: docker compose restart
2+
short: Restart containers
3+
long: Restart containers
4+
usage: docker compose restart
5+
pname: docker compose
6+
plink: docker_compose.yaml
7+
options:
8+
- option: timeout
9+
shorthand: t
10+
value_type: int
11+
default_value: "10"
12+
description: Specify a shutdown timeout in seconds
13+
deprecated: false
14+
experimental: false
15+
experimentalcli: false
16+
kubernetes: false
17+
swarm: false
18+
deprecated: false
19+
experimental: false
20+
experimentalcli: false
21+
kubernetes: false
22+
swarm: false
23+

ecs/local/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (e ecsLocalSimulation) Start(ctx context.Context, project *types.Project, o
5757
return e.compose.Start(ctx, project, options)
5858
}
5959

60+
func (e ecsLocalSimulation) Restart(ctx context.Context, project *types.Project, options compose.RestartOptions) error {
61+
return e.compose.Restart(ctx, project, options)
62+
}
63+
6064
func (e ecsLocalSimulation) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
6165
return e.compose.Stop(ctx, project, options)
6266
}

0 commit comments

Comments
 (0)