volumes.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "fmt"
  17. "slices"
  18. "github.com/docker/cli/cli/command"
  19. "github.com/docker/cli/cli/command/formatter"
  20. "github.com/docker/cli/cli/flags"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose/v5/pkg/api"
  23. "github.com/docker/compose/v5/pkg/compose"
  24. )
  25. type volumesOptions struct {
  26. *ProjectOptions
  27. Quiet bool
  28. Format string
  29. }
  30. func volumesCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  31. options := volumesOptions{
  32. ProjectOptions: p,
  33. }
  34. cmd := &cobra.Command{
  35. Use: "volumes [OPTIONS] [SERVICE...]",
  36. Short: "List volumes",
  37. RunE: Adapt(func(ctx context.Context, args []string) error {
  38. return runVol(ctx, dockerCli, backendOptions, args, options)
  39. }),
  40. ValidArgsFunction: completeServiceNames(dockerCli, p),
  41. }
  42. cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", false, "Only display volume names")
  43. cmd.Flags().StringVar(&options.Format, "format", "table", flags.FormatHelp)
  44. return cmd
  45. }
  46. func runVol(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, services []string, options volumesOptions) error {
  47. project, name, err := options.projectOrName(ctx, dockerCli, services...)
  48. if err != nil {
  49. return err
  50. }
  51. if project != nil {
  52. names := project.ServiceNames()
  53. for _, service := range services {
  54. if !slices.Contains(names, service) {
  55. return fmt.Errorf("no such service: %s", service)
  56. }
  57. }
  58. }
  59. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  60. if err != nil {
  61. return err
  62. }
  63. volumes, err := backend.Volumes(ctx, name, api.VolumesOptions{
  64. Services: services,
  65. })
  66. if err != nil {
  67. return err
  68. }
  69. if options.Quiet {
  70. for _, v := range volumes {
  71. _, _ = fmt.Fprintln(dockerCli.Out(), v.Name)
  72. }
  73. return nil
  74. }
  75. volumeCtx := formatter.Context{
  76. Output: dockerCli.Out(),
  77. Format: formatter.NewVolumeFormat(options.Format, options.Quiet),
  78. }
  79. return formatter.VolumeWrite(volumeCtx, volumes)
  80. }