acivolume.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 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 --fileshare FILESHARE",
  41. Short: "Creates an Azure file share to use as ACI volume.",
  42. Args: cobra.ExactArgs(0),
  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. err = progress.Run(ctx, func(ctx context.Context) error {
  50. if _, err := c.VolumeService().Create(ctx, aciOpts); err != nil {
  51. return err
  52. }
  53. return nil
  54. })
  55. if err != nil {
  56. return err
  57. }
  58. fmt.Println(aci.VolumeID(aciOpts.Account, aciOpts.Fileshare))
  59. return nil
  60. },
  61. }
  62. cmd.Flags().StringVar(&aciOpts.Account, "storage-account", "", "Storage account name")
  63. cmd.Flags().StringVar(&aciOpts.Fileshare, "fileshare", "", "Fileshare name")
  64. _ = cmd.MarkFlagRequired("fileshare")
  65. _ = cmd.MarkFlagRequired("storage-account")
  66. return cmd
  67. }
  68. func rmVolume() *cobra.Command {
  69. cmd := &cobra.Command{
  70. Use: "rm [OPTIONS] VOLUME [VOLUME...]",
  71. Short: "Remove one or more volumes.",
  72. Args: cobra.MinimumNArgs(1),
  73. RunE: func(cmd *cobra.Command, args []string) error {
  74. c, err := client.New(cmd.Context())
  75. if err != nil {
  76. return err
  77. }
  78. var errs *multierror.Error
  79. for _, id := range args {
  80. err = c.VolumeService().Delete(cmd.Context(), id, nil)
  81. if err != nil {
  82. errs = multierror.Append(errs, err)
  83. continue
  84. }
  85. fmt.Println(id)
  86. }
  87. formatter.SetMultiErrorFormat(errs)
  88. return errs.ErrorOrNil()
  89. },
  90. }
  91. return cmd
  92. }