getSemver uses the regular expression from the requirement to parse the output of a command and extract the version from it. Returns the version or an error if the version string could not be parsed.
(req requirement, out []byte)
| 64 | // output of a command and extract the version from it. Returns the version |
| 65 | // or an error if the version string could not be parsed. |
| 66 | func getSemver(req requirement, out []byte) (*semver.Version, error) { |
| 67 | re := regexp.MustCompile(req.regexStr) |
| 68 | v := re.FindStringSubmatch(string(out)) |
| 69 | if len(v) < 4 { |
| 70 | return nil, &commandError{ |
| 71 | req.command, |
| 72 | "Could not find version number in output", |
| 73 | fmt.Sprintf("Try running %s %s locally and checking it works.", req.command, strings.Join(req.args, " ")), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Default patch version number to 0 if it doesn't exist |
| 78 | if v[3] == "" { |
| 79 | v[3] = "0" |
| 80 | } |
| 81 | |
| 82 | versionString := fmt.Sprintf("%s.%s.%s", v[1], v[2], v[3]) |
| 83 | version, err := semver.NewVersion(versionString) |
| 84 | if err != nil { |
| 85 | return version, err |
| 86 | } |
| 87 | return version, nil |
| 88 | } |
| 89 | |
| 90 | // checkSemver validates that the version of a tool meets the minimum required |
| 91 | // version listed in your requirement. Returns a boolean. |