github.com/simpleiot/simpleiot@v0.18.3/system/parse.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/blang/semver/v4"
     8  )
     9  
    10  func parseVersion(releaseFile []byte, field string) (ver semver.Version, err error) {
    11  	// This regex will parse VERSION_ID=1.2 or VERSION_ID="1.2.3" just as easily
    12  	re := field + `=['"]?([^'"\s]*)`
    13  	reCompiled, err := regexp.Compile(re)
    14  
    15  	if err != nil {
    16  		return semver.Version{}, fmt.Errorf("regex compile failed: %v", err)
    17  	}
    18  
    19  	// Now parse the file to get the version info
    20  	versionInfo := reCompiled.FindSubmatch(releaseFile)
    21  	if versionInfo == nil {
    22  		err = fmt.Errorf("field %v not found", field)
    23  		return
    24  	}
    25  	return semver.ParseTolerant(string(versionInfo[1]))
    26  }