acivolume.go 3.1 KB

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