-
Notifications
You must be signed in to change notification settings - Fork 51
/
version.go
44 lines (41 loc) · 1.24 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package variablefinders
import (
"os"
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
)
// FindVersion finds the version name
func FindVersion(versionFile, branch, buildNumber string) (string, error) {
version := ""
if versionFile != "" {
exists, err := files.FileExists(versionFile)
if err != nil {
return version, errors.Wrapf(err, "failed to check for file %s", versionFile)
}
if exists {
data, err := os.ReadFile(versionFile)
if err != nil {
return version, errors.Wrapf(err, "failed to read version file %s", versionFile)
}
version = strings.TrimSpace(string(data))
} else {
log.Logger().Infof("version file %s does not exist", versionFile)
}
}
if version == "" {
version = os.Getenv("VERSION")
}
if version == "" {
pullNumber := os.Getenv("PULL_NUMBER")
if pullNumber != "" {
return "0.0.0-PR-" + pullNumber + "-" + buildNumber + "-SNAPSHOT", nil
}
if strings.HasPrefix(branch, "PR-") {
return "0.0.0-" + branch + "-" + buildNumber + "-SNAPSHOT", nil
}
log.Logger().Warnf("could not detect version from $VERSION or version file %s. Try supply the command option: --version", versionFile)
}
return version, nil
}