volumes.go 2.2 KB

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