mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 13:48:09 +08:00
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gdebug
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/gogf/gf/v2/encoding/ghash"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
// BinVersion returns the version of current running binary.
|
|
// It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary.
|
|
func BinVersion() string {
|
|
if binaryVersion == "" {
|
|
binaryContent, _ := ioutil.ReadFile(selfPath)
|
|
binaryVersion = strconv.FormatInt(
|
|
int64(ghash.BKDR(binaryContent)),
|
|
36,
|
|
)
|
|
}
|
|
return binaryVersion
|
|
}
|
|
|
|
// BinVersionMd5 returns the version of current running binary.
|
|
// It uses MD5 algorithm to calculate the unique version of the binary.
|
|
func BinVersionMd5() string {
|
|
if binaryVersionMd5 == "" {
|
|
binaryVersionMd5, _ = md5File(selfPath)
|
|
}
|
|
return binaryVersionMd5
|
|
}
|
|
|
|
// md5File encrypts file content of `path` using MD5 algorithms.
|
|
func md5File(path string) (encrypt string, err error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
err = gerror.Wrapf(err, `os.Open failed for name "%s"`, path)
|
|
return "", err
|
|
}
|
|
defer f.Close()
|
|
h := md5.New()
|
|
_, err = io.Copy(h, f)
|
|
if err != nil {
|
|
err = gerror.Wrap(err, `io.Copy failed`)
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
|
}
|