command.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 volume
  14. import (
  15. "context"
  16. "fmt"
  17. format "github.com/docker/compose-cli/cmd/formatter"
  18. "github.com/docker/compose-cli/aci"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/api/context/store"
  21. "github.com/docker/compose-cli/ecs"
  22. "github.com/docker/compose-cli/pkg/progress"
  23. "github.com/hashicorp/go-multierror"
  24. "github.com/spf13/cobra"
  25. )
  26. // Command manage volumes
  27. func Command(ctype string) *cobra.Command {
  28. cmd := &cobra.Command{
  29. Use: "volume",
  30. Short: "Manages volumes",
  31. }
  32. cmd.AddCommand(
  33. createVolume(ctype),
  34. listVolume(),
  35. rmVolume(),
  36. inspectVolume(),
  37. )
  38. return cmd
  39. }
  40. func createVolume(ctype string) *cobra.Command {
  41. var usage string
  42. var short string
  43. switch ctype {
  44. case store.AciContextType:
  45. usage = "create --storage-account ACCOUNT VOLUME"
  46. short = "Creates an Azure file share to use as ACI volume."
  47. case store.EcsContextType:
  48. usage = "create [OPTIONS] VOLUME"
  49. short = "Creates an EFS filesystem to use as AWS volume."
  50. default:
  51. usage = "create [OPTIONS] VOLUME"
  52. short = "Creates a volume"
  53. }
  54. var opts interface{}
  55. cmd := &cobra.Command{
  56. Use: usage,
  57. Short: short,
  58. Args: cobra.ExactArgs(1),
  59. RunE: func(cmd *cobra.Command, args []string) error {
  60. ctx := cmd.Context()
  61. c, err := client.New(ctx)
  62. if err != nil {
  63. return err
  64. }
  65. result, err := progress.RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  66. volume, err := c.VolumeService().Create(ctx, args[0], opts)
  67. if err != nil {
  68. return "", err
  69. }
  70. return volume.ID, nil
  71. })
  72. if err != nil {
  73. return err
  74. }
  75. fmt.Println(result)
  76. return nil
  77. },
  78. }
  79. switch ctype {
  80. case store.AciContextType:
  81. aciOpts := aci.VolumeCreateOptions{}
  82. cmd.Flags().StringVar(&aciOpts.Account, "storage-account", "", "Storage account name")
  83. _ = cmd.MarkFlagRequired("storage-account")
  84. opts = &aciOpts
  85. case store.EcsContextType:
  86. ecsOpts := ecs.VolumeCreateOptions{}
  87. cmd.Flags().StringVar(&ecsOpts.KmsKeyID, "kms-key", "", "ID of the AWS KMS CMK to be used to protect the encrypted file system")
  88. cmd.Flags().StringVar(&ecsOpts.PerformanceMode, "performance-mode", "", "performance mode of the file system. (generalPurpose|maxIO)")
  89. cmd.Flags().Float64Var(&ecsOpts.ProvisionedThroughputInMibps, "provisioned-throughput", 0, "throughput in MiB/s (1-1024)")
  90. cmd.Flags().StringVar(&ecsOpts.ThroughputMode, "throughput-mode", "", "throughput mode (bursting|provisioned)")
  91. opts = &ecsOpts
  92. }
  93. return cmd
  94. }
  95. func rmVolume() *cobra.Command {
  96. cmd := &cobra.Command{
  97. Use: "rm [OPTIONS] VOLUME [VOLUME...]",
  98. Short: "Remove one or more volumes.",
  99. Args: cobra.MinimumNArgs(1),
  100. RunE: func(cmd *cobra.Command, args []string) error {
  101. c, err := client.New(cmd.Context())
  102. if err != nil {
  103. return err
  104. }
  105. var errs *multierror.Error
  106. for _, id := range args {
  107. err = c.VolumeService().Delete(cmd.Context(), id, nil)
  108. if err != nil {
  109. errs = multierror.Append(errs, err)
  110. continue
  111. }
  112. fmt.Println(id)
  113. }
  114. format.SetMultiErrorFormat(errs)
  115. return errs.ErrorOrNil()
  116. },
  117. }
  118. return cmd
  119. }
  120. func inspectVolume() *cobra.Command {
  121. cmd := &cobra.Command{
  122. Use: "inspect VOLUME [VOLUME...]",
  123. Short: "Inspect one or more volumes.",
  124. Args: cobra.ExactArgs(1),
  125. RunE: func(cmd *cobra.Command, args []string) error {
  126. c, err := client.New(cmd.Context())
  127. if err != nil {
  128. return err
  129. }
  130. v, err := c.VolumeService().Inspect(cmd.Context(), args[0])
  131. if err != nil {
  132. return err
  133. }
  134. outJSON, err := format.ToStandardJSON(v)
  135. if err != nil {
  136. return err
  137. }
  138. fmt.Print(outJSON)
  139. return nil
  140. },
  141. }
  142. return cmd
  143. }