volumes.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // +build local
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  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. package local
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/docker/docker/api/types"
  19. "github.com/docker/docker/api/types/filters"
  20. "github.com/docker/docker/api/types/volume"
  21. "github.com/docker/docker/client"
  22. "github.com/docker/compose-cli/api/volumes"
  23. )
  24. type volumeService struct {
  25. apiClient *client.Client
  26. }
  27. func (vs *volumeService) List(ctx context.Context) ([]volumes.Volume, error) {
  28. l, err := vs.apiClient.VolumeList(ctx, filters.NewArgs())
  29. if err != nil {
  30. return []volumes.Volume{}, err
  31. }
  32. res := []volumes.Volume{}
  33. for _, v := range l.Volumes {
  34. res = append(res, volumes.Volume{
  35. ID: v.Name,
  36. Description: description(v),
  37. })
  38. }
  39. return res, nil
  40. }
  41. func (vs *volumeService) Create(ctx context.Context, name string, options interface{}) (volumes.Volume, error) {
  42. v, err := vs.apiClient.VolumeCreate(ctx, volume.VolumeCreateBody{
  43. Driver: "local",
  44. DriverOpts: nil,
  45. Labels: nil,
  46. Name: name,
  47. })
  48. if err != nil {
  49. return volumes.Volume{}, err
  50. }
  51. return volumes.Volume{ID: name, Description: description(&v)}, nil
  52. }
  53. func (vs *volumeService) Delete(ctx context.Context, volumeID string, options interface{}) error {
  54. if err := vs.apiClient.VolumeRemove(ctx, volumeID, false); err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. func (vs *volumeService) Inspect(ctx context.Context, volumeID string) (volumes.Volume, error) {
  60. v, err := vs.apiClient.VolumeInspect(ctx, volumeID)
  61. if err != nil {
  62. return volumes.Volume{}, err
  63. }
  64. return volumes.Volume{ID: volumeID, Description: description(&v)}, nil
  65. }
  66. func description(v *types.Volume) string {
  67. return fmt.Sprintf("Created %s", v.CreatedAt)
  68. }