start.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 cmd
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/docker/compose-cli/errdefs"
  18. "github.com/pkg/errors"
  19. "github.com/spf13/cobra"
  20. "github.com/hashicorp/go-multierror"
  21. "github.com/docker/compose-cli/client"
  22. )
  23. // StartCommand starts containers
  24. func StartCommand() *cobra.Command {
  25. cmd := &cobra.Command{
  26. Use: "start",
  27. Short: "Start one or more stopped containers",
  28. Args: cobra.MinimumNArgs(1),
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. return runStart(cmd.Context(), args)
  31. },
  32. }
  33. return cmd
  34. }
  35. func runStart(ctx context.Context, args []string) error {
  36. c, err := client.New(ctx)
  37. if err != nil {
  38. return errors.Wrap(err, "cannot connect to backend")
  39. }
  40. var errs *multierror.Error
  41. for _, id := range args {
  42. err := c.ContainerService().Start(ctx, id)
  43. if err != nil {
  44. if errdefs.IsNotFoundError(err) {
  45. errs = multierror.Append(errs, fmt.Errorf("container %s not found", id))
  46. } else {
  47. errs = multierror.Append(errs, err)
  48. }
  49. continue
  50. }
  51. fmt.Println(id)
  52. }
  53. if errs != nil {
  54. errs.ErrorFormat = formatErrors
  55. }
  56. return errs.ErrorOrNil()
  57. }