datastore_path.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright (c) 2016 VMware, Inc. All Rights Reserved.
  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 object
  14. import (
  15. "fmt"
  16. "path"
  17. "strings"
  18. )
  19. // DatastorePath contains the components of a datastore path.
  20. type DatastorePath struct {
  21. Datastore string
  22. Path string
  23. }
  24. // FromString parses a datastore path.
  25. // Returns true if the path could be parsed, false otherwise.
  26. func (p *DatastorePath) FromString(s string) bool {
  27. if len(s) == 0 {
  28. return false
  29. }
  30. s = strings.TrimSpace(s)
  31. if !strings.HasPrefix(s, "[") {
  32. return false
  33. }
  34. s = s[1:]
  35. ix := strings.Index(s, "]")
  36. if ix < 0 {
  37. return false
  38. }
  39. p.Datastore = s[:ix]
  40. p.Path = strings.TrimSpace(s[ix+1:])
  41. return true
  42. }
  43. // String formats a datastore path.
  44. func (p *DatastorePath) String() string {
  45. s := fmt.Sprintf("[%s]", p.Datastore)
  46. if p.Path == "" {
  47. return s
  48. }
  49. return strings.Join([]string{s, p.Path}, " ")
  50. }
  51. // IsVMDK returns true if Path has a ".vmdk" extension
  52. func (p *DatastorePath) IsVMDK() bool {
  53. return path.Ext(p.Path) == ".vmdk"
  54. }