github.com/vmware/govmomi@v0.43.0/govc/flags/version.go (about)

     1  /*
     2  Copyright (c) 2014-2020 VMware, Inc. All Rights Reserved.
     3  
     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  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flags
    18  
    19  import (
    20  	"strconv"
    21  	"strings"
    22  )
    23  
    24  var (
    25  	BuildVersion = "v0.0.0" // govc-test requires an (arbitrary) version set
    26  	BuildCommit  string
    27  	BuildDate    string
    28  )
    29  
    30  type version []int
    31  
    32  func ParseVersion(s string) (version, error) {
    33  	// remove any trailing "v" version identifier
    34  	s = strings.TrimPrefix(s, "v")
    35  	v := make(version, 0)
    36  
    37  	ds := strings.Split(s, "-")
    38  	ps := strings.Split(ds[0], ".")
    39  	for _, p := range ps {
    40  		i, err := strconv.Atoi(p)
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  
    45  		v = append(v, i)
    46  	}
    47  
    48  	return v, nil
    49  }
    50  
    51  func (v version) Lte(u version) bool {
    52  	lv := len(v)
    53  	lu := len(u)
    54  
    55  	for i := 0; i < lv; i++ {
    56  		// Everything up to here has been equal and v has more elements than u.
    57  		if i >= lu {
    58  			return false
    59  		}
    60  
    61  		// Move to next digit if equal.
    62  		if v[i] == u[i] {
    63  			continue
    64  		}
    65  
    66  		return v[i] < u[i]
    67  	}
    68  
    69  	// Equal.
    70  	return true
    71  }