github.com/vmware/govmomi@v0.51.0/object/datastore_path.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package object
     6  
     7  import (
     8  	"fmt"
     9  	"path"
    10  	"strings"
    11  )
    12  
    13  // DatastorePath contains the components of a datastore path.
    14  type DatastorePath struct {
    15  	Datastore string
    16  	Path      string
    17  }
    18  
    19  // FromString parses a datastore path.
    20  // Returns true if the path could be parsed, false otherwise.
    21  func (p *DatastorePath) FromString(s string) bool {
    22  	if s == "" {
    23  		return false
    24  	}
    25  
    26  	s = strings.TrimSpace(s)
    27  
    28  	if !strings.HasPrefix(s, "[") {
    29  		return false
    30  	}
    31  
    32  	s = s[1:]
    33  
    34  	ix := strings.Index(s, "]")
    35  	if ix < 0 {
    36  		return false
    37  	}
    38  
    39  	p.Datastore = s[:ix]
    40  	p.Path = strings.TrimSpace(s[ix+1:])
    41  
    42  	return true
    43  }
    44  
    45  // String formats a datastore path.
    46  func (p *DatastorePath) String() string {
    47  	s := fmt.Sprintf("[%s]", p.Datastore)
    48  
    49  	if p.Path == "" {
    50  		return s
    51  	}
    52  
    53  	return strings.Join([]string{s, p.Path}, " ")
    54  }
    55  
    56  // IsVMDK returns true if Path has a ".vmdk" extension
    57  func (p *DatastorePath) IsVMDK() bool {
    58  	return path.Ext(p.Path) == ".vmdk"
    59  }