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