-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathinit.go
More file actions
116 lines (97 loc) · 2.65 KB
/
init.go
File metadata and controls
116 lines (97 loc) · 2.65 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package x
import (
"crypto/sha256"
"fmt"
"io"
"os"
"regexp"
"runtime"
"github.com/golang/glog"
"github.com/dgraph-io/ristretto/v2/z"
)
var (
// These variables are set using -ldflags
dgraphVersion string
dgraphCodename string
gitBranch string
lastCommitSHA string
lastCommitTime string
)
func init() {
// check if any version is set by ldflags. If not so, it should be set as "dev"
if dgraphVersion == "" {
dgraphVersion = "dev"
}
}
// BuildDetails returns a string containing details about the Dgraph binary.
func BuildDetails() string {
licenseInfo := `Licensed under the Apache Public License 2.0`
buf := z.CallocNoRef(1, "X.BuildDetails")
jem := len(buf) > 0
z.Free(buf)
return fmt.Sprintf(`
Dgraph version : %v
Dgraph codename : %v
Dgraph SHA-256 : %x
Commit SHA-1 : %v
Commit timestamp : %v
Branch : %v
Go version : %v
jemalloc enabled : %v
GOMAXPROCS : %v
Num CPUs : %v
For Dgraph official documentation, visit https://dgraph.io/docs.
For discussions about Dgraph , visit https://discuss.dgraph.io.
%s.
© Istari Digital, Inc.
`,
dgraphVersion, dgraphCodename, ExecutableChecksum(), lastCommitSHA, lastCommitTime, gitBranch,
runtime.Version(), jem, runtime.GOMAXPROCS(0), runtime.NumCPU(), licenseInfo)
}
// PrintVersion prints version and other helpful information if --version.
func PrintVersion() {
glog.Infof("\n%s\n", BuildDetails())
}
// Version returns a string containing the dgraphVersion.
func Version() string {
return dgraphVersion
}
// Codename returns a string containing the dgraphCodename.
func Codename() string {
return dgraphCodename
}
// pattern for dev version = min. 7 hex digits of commit-hash.
var versionRe *regexp.Regexp = regexp.MustCompile(`-g[[:xdigit:]]{7,}`)
// DevVersion returns true if the version string contains the above pattern
// e.g.
// 1. v2.0.0-rc1-127-gd20a768b3 => dev version
// 2. v2.0.0 => prod version
func DevVersion() (matched bool) {
return versionRe.MatchString(dgraphVersion)
}
// ExecutableChecksum returns a byte slice containing the SHA256 checksum of the executable.
// It returns a nil slice if there's an error trying to calculate the checksum.
func ExecutableChecksum() []byte {
execPath, err := os.Executable()
if err != nil {
return nil
}
execFile, err := os.Open(execPath)
if err != nil {
return nil
}
defer func() {
if err := execFile.Close(); err != nil {
glog.Warningf("error closing file: %v", err)
}
}()
h := sha256.New()
if _, err := io.Copy(h, execFile); err != nil {
return nil
}
return h.Sum(nil)
}