github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/versioncheck/check.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package versioncheck provides utility wrappers for go-version, allowing the
    16  // constraints to be used as global variables.
    17  package versioncheck
    18  
    19  import (
    20  	"fmt"
    21  
    22  	go_version "github.com/hashicorp/go-version"
    23  )
    24  
    25  // MustCompile wraps go-version.NewConstraint, panicing when an error is
    26  // returns (this occurs when the constraint cannot be parsed).
    27  // It is intended to be use similar to re.MustCompile, to ensure unparseable
    28  // constraints are caught in testing.
    29  func MustCompile(constraint string) go_version.Constraints {
    30  	verCheck, err := Compile(constraint)
    31  	if err != nil {
    32  		panic(fmt.Errorf("cannot compile go-version constraint '%s' %s", constraint, err))
    33  	}
    34  	return verCheck
    35  }
    36  
    37  // Compile trivially wraps go-version.NewConstraint, returning the constraint
    38  // and error
    39  func Compile(constraint string) (go_version.Constraints, error) {
    40  	return go_version.NewConstraint(constraint)
    41  }