github.com/goldeneggg/goa@v1.3.1/version/version.go (about)

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  const (
    10  	// Major version number
    11  	Major = 1
    12  	// Minor version number
    13  	Minor = 3
    14  	// Build version number
    15  	Build = 1
    16  )
    17  
    18  // String returns the complete version number.
    19  func String() string {
    20  	return "v" + strconv.Itoa(Major) + "." + strconv.Itoa(Minor) + "." + strconv.Itoa(Build)
    21  }
    22  
    23  // Compatible returns true if Major matches the major version of the given version string.
    24  // It returns an error if the given string is not a valid version string.
    25  func Compatible(v string) (bool, error) {
    26  	if len(v) < 5 {
    27  		return false, fmt.Errorf("invalid version string format %#v", v)
    28  	}
    29  	v = v[1:]
    30  	elems := strings.Split(v, ".")
    31  	if len(elems) != 3 {
    32  		return false, fmt.Errorf("version not of the form Major.Minor.Build %#v", v)
    33  	}
    34  	mj, err := strconv.Atoi(elems[0])
    35  	if err != nil {
    36  		return false, fmt.Errorf("invalid major version number %#v, must be number", elems[0])
    37  	}
    38  	return mj == Major, nil
    39  }