mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-02 19:40:19 +08:00
feat: support getting versionInfo in docker (#1673)
* feat: support getting versionInfo in docker * fix: fix build * fix: fix build * fix: fix system
This commit is contained in:
@ -15,8 +15,13 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
@ -140,3 +145,47 @@ func GetVersionInfo() (*VersionInfo, error) {
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func GetVersionInfoFromFile() (*VersionInfo, error) {
|
||||
res := &VersionInfo{
|
||||
Version: "",
|
||||
CommitId: "",
|
||||
CommitOffset: -1,
|
||||
}
|
||||
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
rootPath := path.Dir(path.Dir(filename))
|
||||
file, err := os.Open(path.Join(rootPath, "version_info.txt"))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read file contents line by line
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
// Use regular expressions to match strings
|
||||
re := regexp.MustCompile(`\{([^{}]+)\}`)
|
||||
versionInfo := scanner.Text()
|
||||
matches := re.FindStringSubmatch(versionInfo)
|
||||
if len(matches) > 1 {
|
||||
split := strings.Split(matches[1], " ")
|
||||
version := split[0]
|
||||
commitId := split[1]
|
||||
commitOffset, _ := strconv.Atoi(split[2])
|
||||
res = &VersionInfo{
|
||||
Version: version,
|
||||
CommitId: commitId,
|
||||
CommitOffset: commitOffset,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user