acivolume.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/hashicorp/go-multierror"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/aci"
  20. "github.com/docker/compose-cli/api/client"
  21. "github.com/docker/compose-cli/cli/formatter"
  22. "github.com/docker/compose-cli/progress"
  23. )
  24. // ACICommand manage volumes
  25. func ACICommand() *cobra.Command {
  26. cmd := &cobra.Command{
  27. Use: "volume",
  28. Short: "Manages volumes",
  29. }
  30. cmd.AddCommand(
  31. createVolume(),
  32. listVolume(),
  33. rmVolume(),
  34. )
  35. return cmd
  36. }
  37. func createVolume() *cobra.Command {
  38. aciOpts := aci.VolumeCreateOptions{}
  39. cmd := &cobra.Command{
  40. Use: "create --storage-account ACCOUNT VOLUME",
  41. Short: "Creates an Azure file share to use as ACI volume.",
  42. Args: cobra.ExactArgs(1),
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. ctx := cmd.Context()
  45. c, err := client.New(ctx)
  46. if err != nil {
  47. return err
  48. }
  49. result, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
  50. volume, err := c.VolumeService().Create(ctx, args[0], aciOpts)
  51. if err != nil {
  52. return "", err
  53. }
  54. return volume.ID, nil
  55. })
  56. if err != nil {
  57. return err
  58. }
  59. fmt.Println(result)
  60. return nil
  61. },
  62. }
  63. cmd.Flags().StringVar(&aciOpts.Account, "storage-account", "", "Storage account name")
  64. _ = cmd.MarkFlagRequired("storage-account")
  65. return cmd
  66. }
  67. func rmVolume() *cobra.Command {
  68. cmd := &cobra.Command{
  69. Use: "rm [OPTIONS] VOLUME [VOLUME...]",
  70. Short: "Remove one or more volumes.",
  71. Args: cobra.MinimumNArgs(1),
  72. RunE: func(cmd *cobra.Command, args []string) error {
  73. c, err := client.New(cmd.Context())
  74. if err != nil {
  75. return err
  76. }
  77. var errs *multierror.Error
  78. for _, id := range args {
  79. err = c.VolumeService().Delete(cmd.Context(), id, nil)
  80. if err != nil {
  81. errs = multierror.Append(errs, err)
  82. continue
  83. }
  84. fmt.Println(id)
  85. }
  86. formatter.SetMultiErrorFormat(errs)
  87. return errs.ErrorOrNil()
  88. },
  89. }
  90. return cmd
  91. }