restart.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "time"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose/v2/pkg/api"
  19. )
  20. type restartOptions struct {
  21. *projectOptions
  22. timeout int
  23. }
  24. func restartCommand(p *projectOptions, backend api.Service) *cobra.Command {
  25. opts := restartOptions{
  26. projectOptions: p,
  27. }
  28. restartCmd := &cobra.Command{
  29. Use: "restart",
  30. Short: "Restart containers",
  31. RunE: Adapt(func(ctx context.Context, args []string) error {
  32. return runRestart(ctx, backend, opts, args)
  33. }),
  34. ValidArgsFunction: serviceCompletion(p),
  35. }
  36. flags := restartCmd.Flags()
  37. flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
  38. return restartCmd
  39. }
  40. func runRestart(ctx context.Context, backend api.Service, opts restartOptions, services []string) error {
  41. projectName, err := opts.toProjectName()
  42. if err != nil {
  43. return err
  44. }
  45. timeout := time.Duration(opts.timeout) * time.Second
  46. return backend.Restart(ctx, projectName, api.RestartOptions{
  47. Timeout: &timeout,
  48. Services: services,
  49. })
  50. }