create.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package volume
  2. /*
  3. Copyright 2020 Docker, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. import (
  15. "fmt"
  16. "io"
  17. "os"
  18. "strings"
  19. "text/tabwriter"
  20. "github.com/docker/compose-cli/aci"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose-cli/api/client"
  23. "github.com/docker/compose-cli/api/volumes"
  24. )
  25. // Command manage volumes
  26. func Command() *cobra.Command {
  27. cmd := &cobra.Command{
  28. Use: "volume",
  29. Short: "Manages volumes",
  30. }
  31. cmd.AddCommand(
  32. createVolume(),
  33. listVolume(),
  34. )
  35. return cmd
  36. }
  37. func createVolume() *cobra.Command {
  38. opts := aci.VolumeCreateOptions{}
  39. cmd := &cobra.Command{
  40. Use: "create",
  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. c, err := client.New(cmd.Context())
  45. if err != nil {
  46. return err
  47. }
  48. id, err := c.VolumeService().Create(cmd.Context(), opts)
  49. if err != nil {
  50. return err
  51. }
  52. fmt.Println(id)
  53. return nil
  54. },
  55. }
  56. cmd.Flags().StringVar(&opts.Account, "storage-account", "", "Storage account name")
  57. cmd.Flags().StringVar(&opts.Fileshare, "fileshare", "", "Fileshare name")
  58. return cmd
  59. }
  60. func listVolume() *cobra.Command {
  61. cmd := &cobra.Command{
  62. Use: "ls",
  63. Short: "list Azure file shares usable as ACI volumes.",
  64. Args: cobra.ExactArgs(0),
  65. RunE: func(cmd *cobra.Command, args []string) error {
  66. c, err := client.New(cmd.Context())
  67. if err != nil {
  68. return err
  69. }
  70. vols, err := c.VolumeService().List(cmd.Context())
  71. if err != nil {
  72. return err
  73. }
  74. printList(os.Stdout, vols)
  75. return nil
  76. },
  77. }
  78. return cmd
  79. }
  80. func printList(out io.Writer, volumes []volumes.Volume) {
  81. printSection(out, func(w io.Writer) {
  82. for _, vol := range volumes {
  83. fmt.Fprintf(w, "%s\t%s\t%s\n", vol.ID, vol.Name, vol.Description) // nolint:errcheck
  84. }
  85. }, "ID", "NAME", "DESCRIPTION")
  86. }
  87. func printSection(out io.Writer, printer func(io.Writer), headers ...string) {
  88. w := tabwriter.NewWriter(out, 20, 1, 3, ' ', 0)
  89. fmt.Fprintln(w, strings.Join(headers, "\t")) // nolint:errcheck
  90. printer(w)
  91. w.Flush() // nolint:errcheck
  92. }